180 likes | 301 Vues
Dive into the world of XSLT to learn how to convert XML documents into different formats using templates and XPath. Understand the history, use cases, and key features of XSLT through a short tragic story involving a prince and Cleopatra. Learn about XFDL, schema definitions, and the importance of having a well-defined schema before transformation.
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)