180 likes | 268 Vues
Learn about XSLT, a powerful technology for converting XML documents into various formats, including HTML. Understand the history, syntax, and key features of XSLT, along with practical examples and lessons from past projects. Discover how XSLT templates work, with a focus on XPath expressions, built-in functions, and advanced programming capabilities. See how XSLT was used in a real-world project involving patent forms and the importance of a well-defined schema. Master XSLT for efficient transformation of XML data!
E N D
XSLT Doo Soon Kim
Short tragic story Once upon a time…. There was a handsome prince... He was traveling in Egypt, … There, he met a beautiful woman, Cleopatra!!!
He fell in love with her He wanted to express his love to her. Eoifun;vun fds;ih sj;fai j;fsodij “ fisa;oinv fie fis;j ;oasidj f;asj fdasj; fiosj Hi..I love you....Sweetheart….blah..blah….
What is XSLT? • Extensible Stylesheet Language Transformation • Convert XML documents into other XML documents, into HTML documents or into almost anything you like • defined by XML
History XSL XSLT XPath July, 1999 April, 1999 became Candidate Recommendation November, 2000 became Candidate Recommendation November, 2000 became Recommendation November, 1999
Use case #1 <name> <first_name /> <last_name /><name><job /><age /> translator(interpreter) <name /><occupation /> schema B schema A
Use case #2 Catalog.html Catalog2HTML.xsl Catalog.wml Catalog.xml Catalog2WML.xsl Catalog2RTF.xsl Catalog.rtf
Schema, XML, XSLT, XPATH • Schema : Defines the structure of XML. (grammar) • XML document: follows the schema. (sentence) • XSLT : converts a xml document of one schmeta into the other. (translation) • XPATH : address specific parts of an XML document. (a specific word)
Schema, XSLT, XPATH <card type="simple"> <name>Doo Soon Kim</name> <title>PhD, CS Dept.</title> <email>dskim@cs.utexas.edu</email> <phone>(512) 123-4567</phone> </card> Schema : Top level is card. Card element has 4 children nodes – name, title, email, phone XSLT : Converts it into HTML format (or other formats) XPATH : card/name => name element card[@type] => the type attribute of card ...
Examples • example
<card type="simple"> <name>Doo Soon Kim</name> <title>PhD, CS Dept.</title> <email>dskim@cs.utexas.edu</email> <phone>(512) 123-4567</phone> </card> <html xmlns="http://www.w3.org/1999/xhtml"><title>business card</title> <body><h1>Doo Soon Kim</h1><h3><i>PhD, CS Dept.</i></h3> <p>email: <a href="mailto:dskim@cs.utexas.edu"><tt>dskim@cs.utexas.edu</tt></a></p> <p>phone: (512)123-4567</p> </body></html>
Simple examples <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns="http://www.w3.org/1999/xhtml"> <xsl:template match="card[@type='simple']"> <html xmlns="http://www.w3.org/1999/xhtml"> <title>business card</title><body> <xsl:apply-templates select="name"/> <xsl:apply-templates select="title"/> <xsl:apply-templates select="email"/> <xsl:apply-templates select="phone"/> </body></html> </xsl:template> <xsl:template match="card/name"> <h1><xsl:value-of select="text()"/></h1> </xsl:template> <xsl:template match="email"> <p>email: <a href="mailto:{text()}"><tt> <xsl:value-of select="text()"/> </tt></a></p> </xsl:template> ... </xsl:stylesheet> (ref: http://www.brics.dk/~amoeller/XML/xslt-4.1.html)
Understanding a Template • XSLT is a list of XSL templates • XSL Template : If elements are satisfied by match part in a template, replace them with the body of the template • The match patterndetermines where this template applies • Match pattern is defined by XPATH
Examples XPATH <xsl:template match=“title"> <H1><xsl:value-of select="text()"/></H1> </xsl:template> If match holds, do this part! <title> Lord of Ring </title> <H1> Lord of Ring </H1> <title> <H1> Lord of Ring Lord of Ring
More of XSLT • Templates can have programming features inside its bodyIf-then-else, variable, loop, function ... • Provides lots of built-in primitive functions.string manipulator, mathematical functions, ...
Lesson from past project • XFDL(eXtensible Form Description Language) • Built XFDL Editor/Viewer • Intellectual Property Office wanted to provide a patent-form service (over 150) over the internet • Manages patent-related documents as SGML • Key problem : Converting XFDL into SGML patent DTD • Sounds good.....But......
Project failed!! • XFDL : form description language. display-oriented like HTMLPatent DTD : content-oriented. Content-oriented -> Display-oriented : EasyDisplay-oriented -> Content-oriented : Hard • We ended up making different 150 XSLT. • Lesson : You should have a well-defined schema first!
References • XSLT Processors : Saxon, XT, iXSLT, Xalan-Java, Xalan-C++, Sablotron, MSXML(ex. msxml <xsl file> <xml file>) • W3C Recommendation(http://www.w3.org/TR/xslt) • XSLT Editors(http://www.xslt.com/resources.html)