1 / 61

XSL

XSL. eXtensible Stylesheet Language. เนื้อหา. Extensible Stylesheet Language (XSL) XSL Structure XSL Formatting Object XSL Transformation Language ; XSLT. Extensible Style Language (XSL). XSL is a W3C Recommendation 16 November 1999 XSL or CSS is essential for XML presentation

hasana
Download Presentation

XSL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XSL eXtensible Stylesheet Language

  2. เนื้อหา • Extensible Stylesheet Language (XSL) • XSL Structure • XSL Formatting Object • XSL Transformation Language ; XSLT

  3. Extensible Style Language (XSL) • XSL is a W3C Recommendation 16 November 1999 • XSL or CSS is essential for XML presentation • XSL far more powerful and complicated than CSS • XSL permits • element reordering • selection of source elements • text generation • embedded ECMAScript • extensibility

  4. CSS - The Style Sheet of HTML • HTML uses predefined tags • the meanings of these tags are well understood • <p> element defines a paragraph • <h1> element defines a heading • browser knows how to display these elements. • Adding display style characteristics to HTML elements with CSS • Simple and easy process • Telling the browser to display each element using a special font or color, is easy to do and easy for a browser to understand. 

  5. XSL - The Style Sheet of XML • XML does not use predefined tags • the meanings of these tags are not understood • <table> could mean an HTML table or a piece of furniture. • the browser does not know how to display an XML document. • To display XML documents • necessary to have a mechanism to describe how the document should be displayed • One of these mechanisms is CSS • BUT XSL (the eXtensible Stylesheet Language) is the preferred style sheet language of XML (and XSL is far more sophisticated than the CSS used by HTML)

  6. XSL - More than a Style Sheet • XSL consists of 3 parts: • a method for transforming XML documents • a method for defining XML parts and patterns • a method for formatting XML documents • think of XSL as a language that can : • transform XML into HTML • filter and sort XML data • address parts of an XML document • format XML data based on the data value, like displaying negative numbers in red • output XML data to different devices, like screen, paper or voice.

  7. XSL • XSL actually consists of 3 languages: • XSLT is a language to transform XML • XPath is a language to define XML parts or patterns • XSL Formatting Objects is a language to define XML display • XSLT • a language for transforming XML documents into other types of documents, or into other XML documents. • XPath • a language for addressing parts of an XML document. XPath was designed to be used by XSLT.

  8. “XSL” XSL-FO XSLT XPath XSL Standards

  9. XSLT - XSL Transformations • XSLT is • the most important part of the XSL Standard • the part of XSL that is used to transform an XML document into another XML document, or another type of document. • XSLT can be • used to transform an XML document into a format that is recognizable to a browser (ie. HTML, by transforming each XML element into an HTML element)

  10. XSLT Defined • Language for specifying how an XML document is to be displayed in a browser (formatting) and what is displayed (data) • Language for specifying how an XML document will be transformed -> XML, CSV, PDF, etc • Cryptic, unforgiving, and painful to work with • But…it’s logical, powerful, and fast

  11. XSLT • XSLT: Extensible Stylesheet Language for Transformations • Converting XML <?xml version="1.0" encoding="UTF-8"?> <company> <employees> <employee department="Dev"> <name>Bill Gates</name> <income>$670,000</income> </employee> </employees> </company> • To HTML, XML, CSV, PDF, etc.. <p>Dear Bill Gates,</p> <p>We regret to inform you that the Dev department can no longer afford your $670,000 services.</p> <hr width="100%" />

  12. XSLT - XSL Transformations • XSLT can • add completely new elements into the output file, or remove elements. • rearrange and sort the elements, and test and make decisions about which elements to display, and a lot more • the transformation process • XSL uses XSLT to transform an XML source tree into an XML result tree (or an XML source document into an XML result document)

  13. CSS uses Rules • XSL uses TEMPLATES to describe how to transform XML • CSS uses Rules • uses one or more rules to define the output of HTML elements • usse a selector to associate the rule with an HTML element • The p selector in this CSS rule tells that a <p> element should be displayed using a font named arial: p { font-family: arial }

  14. XSL - Templates • XSL uses one or more templates to define how to transform XML elements • uses a match attribute to associate the template with an XML element • also uses the match attribute to define a template for a whole branch of the XML document, like when we use match="/" to define the whole document.

  15. XSLT Rules • rules are indicated by xsl:template elements • pattern is specified using an XPath expression in the match attribute value • template is contents of xsl:template element • example: <xsl:template match="periodic_table"> <html> <xsl:apply-templates/> </html> </xsl:template> • template matches periodic_table elements • <html> and </html> are literal result elements • <xsl:apply-templates/> is an instruction to apply templates to all children of matched element

  16. XSLT Processing Model • processor reads an XML document and an XSLT stylesheet • processing starts at root of document • a single node is processed by • finding the template rule with the best matching pattern • once found, executing the contents of the template (often selecting more nodes to process) • if not found, proceeding with list of child nodes • a node list is processed by processing each node in order • process continues recursively until no new source nodes selected

  17. XML Parsers • “Wrap” XML documents with programmatic interface • Turn XML tags into objects in memory • Ensure XML documents “follow the rules” • Well-formed (required) • Valid (adheres to DTD or XML Schema spec) • Optional setting • Validation is turned off by default • Perform XSLT transformations

  18. Flavors of XML Parsers • DOM • Tree-based API (nodes, elements, attributes) • Represents the document’s logical structure • Allows accessing, traversing, modifying, and generating XML documents • Loads entire XML document into memory • SAX • Event-based callback API • Does not load entire XML document into memory • Can be much faster for specific element lookups

  19. XSLT Building Blocks • Root stylesheet element with namespace designator <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> • Output type element <xsl:output method="html" /> (could also use "xml", "text", etc) • XSLT template elements <xsl:template match="/"> <!-- Process the document root --> <html> <body> <!-- Process all employee elements --> <xsl:apply-templates select="//employee" /> </body> </html> </xsl:template>

  20. XSLT Building Blocks (cont.) • Applying XSLT templates recursively <xsl:template match="//employee"> <!-- Process all employee elements --> <p>Dear <xsl:value-of select="name" />,</p> <p>We regret to inform you that the <xsl:value-of select="@department"/> department can no longer afford your <xsl:value-of select="income"/> services.</p> <hr width="100%" /> </xsl:template> • Selecting the value of an XML element <xsl:value-of select="name" /> • Selecting the value of an XML attribute <xsl:value-of select="@department" />

  21. Periodic Table Example <?xml version="1.0"?> <periodic_table> <atom phase="gas"> <name>Hydrogen</name> <symbol>H</symbol> <atomic_number>1</atomic_number> <atomic_weight>1.00794</atomic_weight> <boiling_point units="Kelvin">20.28</boiling_point> <melting_point units="Kelvin">13.81</melting_point> <density units="grams/cubic centimeter"><!-- At 300K -->0.0899</density> </atom> <atom phase="gas"> <name>Helium</name> <symbol>He</symbol> <atomic_number>2</atomic_number> <atomic_weight>4.0026</atomic_weight> <boiling_point units="Kelvin">4.216</boiling_point> <melting_point units="Kelvin">0.95</melting_point> <density units="grams/cubic centimeter"><!-- At 300K -->0.1785</density> </atom> </periodic_table>

  22. Example: names of atoms • applying XSLT program comprising 2 previous rules <xsl:template match="periodic_table"> <html> <xsl:apply-templates/> </html> </xsl:template> <xsl:template match="atom"> <p> <xsl:value-of select="name"/> </p> </xsl:template> • to periodic table yields <html> <p>Hydrogen</p> <p>Helium</p> </html>

  23. CD Catalog Example <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd> … </catalog>

  24. XSL - Templates • a template to output the XML CD Catalog : <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <table border="1"> <tr> <th>Title</th> <th>Artist</th> </tr> <tr> <td>.</td> <td>.</td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet>

  25. XSL <xsl:value-of> Element • previous sample, no data was copied from the XML document to the output • use <xsl:value-of> element to select XML elements into the output stream of the XSL transformation: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <table border="1"> <tr> <th>Title</th> <th>Artist</th> </tr> <tr> <td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet>

  26. XSL <xsl:for-each> element • previous example, only one line of data was copied from the XML document to the output. • use <xsl:for-each> element to select every XML element into the output stream of the XSL transformation: <?xml version="1.0"?> <xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><body> <table border="1"> <tr> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body></html> </xsl:template> </xsl:stylesheet>

  27. <xsl:for-each> element <?xml version="1.0"?> <xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><body> <table border="1"> <tr> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body></html> </xsl:template> </xsl:stylesheet>

  28. Filtering the Output • We can filter the output from an XML file by adding a criterion to the select attribute in the <xsl:for-each> element. <xsl:for-each select="catalog/cd[artist='Bob Dylan']"> • Legal filter operators are: • = (equal) • != (not equal) • &lt; less than • &gt; greater than

  29. Filtering the Output <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd[artist='Bob Dylan']"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template></xsl:stylesheet>

  30. <xsl:sort> element <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:sort select="artist"/> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template></xsl:stylesheet> </xsl:stylesheet>

  31. <xsl:if> Element • <xsl:if> element contains a template that will be applied only if a specified condition is true. • Where to put the IF condition • To put a conditional if test against the content of the file, simply add an <xsl:if> element to your XSL document like this: <xsl:if test="price &gt; 10"> some output ... </xsl:if> • The value of the required test attribute contains the expression to be evaluated.

  32. <xsl:if> element <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:if test="price &gt; 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>

  33. <xsl:choose> element • <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests. • To insert a conditional choose test against the content of the XML file, simply add the <xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to XSL document : <xsl:choose> <xsl:when test="price &gt; 10"> ... some code ... </xsl:when> <xsl:otherwise> ... some code .... </xsl:otherwise> </xsl:choose>

  34. <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0“xmlns:xsl="http://www.w3.org/1999/XSL/Transform“> <xsl:template match="/"> <html><body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price &gt; 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/> </td> </xsl:when> <xsl:otherwise> <td> <xsl:value-of select="artist"/> </td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body></html> </xsl:template></xsl:stylesheet>

  35. <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <xsl:choose> <xsl:when test="price &gt; 10"> <td bgcolor="#ff00ff"><xsl:value-of select="artist"/></td> </xsl:when> <xsl:when test="price &gt; 9 and price &lt;= 10"> <td bgcolor="#cccccc"><xsl:value-of select="artist"/></td> </xsl:when> <xsl:otherwise> <td><xsl:value-of select="artist"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body></html> </xsl:template></xsl:stylesheet>

  36. <xsl:apply-templates> element • <xsl:apply-templates> element • applies a template rule to the current element or to the current element's child nodes. • applies a template rule to the current element or to the current element's child nodes. • If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. • We can use the select attribute to specify the order in which the child nodes are processed.

  37. <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><body> <h2>My CD Collection</h2> <xsl:apply-templates/> </body></html> </xsl:template> <xsl:template match="cd"> <p><xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/> </p> </xsl:template> <xsl:template match="title"> Title: <span style="color:#ff0000"> <xsl:value-of select="."/> </span> <br /> </xsl:template> <xsl:template match="artist"> Artist: <span style="color:#00ff00"> <xsl:value-of select="."/> </span> <br /> </xsl:template> </xsl:stylesheet>

  38. Link Style Sheet to XML document • Then you add an XSL Style Sheet reference to your XML document: <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> … </catalog> • an XSL compliant browser, like Internet Explorer 5.0 or later, will nicely transform XML into HTML

  39. XSLT Internet Explorer 5 Reference • Internet Explorer 5 supports 20 XSLT Elements. • The implementation is not 100% compatible with the W3C standard. • Internet Explorer 6 is Better • Internet Explorer 6 fully supports the official W3C XSL Recommendation. • The XML Parser 3.0 - built onto Internet Explorer 6.0 and Windows XP - is based on both the W3C XSLT 1.0 and the W3C XPath 1.0 Recommendations.

  40. Internet Explorer 5 • XSL in Internet Explorer 5 is not compatible with the official W3C XSL Recommendation. • When IE 5.0 was released in March 1999, the XSL standard was still a W3C Working Draft. • the final W3C XSL Recommendation is different from the Working Draft, the support for XSL in IE 5 is not 100% compatible with the official XSL standard. • This restriction applies for both Internet Explorer 5.0 and 5.5.

  41. Internet Explorer 6 • Internet Explorer 6 fully supports the official W3C XSL Recommendation. • The XML Parser 3.0 - built onto Internet Explorer 6.0 and Windows XP - is based on both the W3C XSLT 1.0 and the W3C XPath 1.0 Recommendations.

  42. Internet Explorer 5.0 XSLT Elements

  43. Internet Explorer 5.0 XSLT Elements

  44. Internet Explorer 5.0 XSLT Elements • The XSLT elements from the W3C Recommendation (XSLT Version 1.0)

  45. Ex - BookCatalogue.xml <?xml version="1.0"?> <!DOCTYPE BookCatalogue SYSTEM "file://localhost/xml-course/xsl/BookCatalogue.dtd"> <BookCatalogue> <Book> <Title>My Life and Times</Title> <Author>Paul McCartney</Author> <Date>July, 1998</Date> <ISBN>94303-12021-43892</ISBN> <Publisher>McMillin Publishing</Publisher> </Book> <Book> <Title>Illusions The Adventures of a Reluctant Messiah</Title> <Author>Richard Bach</Author> <Date>1977</Date> <ISBN>0-440-34319-4</ISBN> <Publisher>Dell Publishing Co.</Publisher> </Book> <Book> <Title>The First and Last Freedom</Title> <Author>J. Krishnamurti</Author> <Date>1954</Date> <ISBN>0-06-064831-7</ISBN> <Publisher>Harper &amp; Row</Publisher> </Book> </BookCatalogue>

  46. First Example • This example will show how the XSL Processor parses your xml document and uses the template rules defined in your xsl document to do something for each thing that is found in the xml document

  47. Terminology • In BookCatalogue.xml we have : • <BookCatalogue> • <Book> • <Title>My Life and Times</Title> • <Author>Paul McCartney</Author> • <Date>July, 1998</Date> • <ISBN>94303-12021-43892</ISBN> • <Publisher>McMillin Publishing</Publisher> • </Book> • ... • </BookCatalogue> • “Book is a child element of the BookCatalogue element. Title, Author, Date, ISBN, and Publisher are children elements of the Book element.”

  48. Creating a Stylesheet - Step 1 • Draw a tree diagram of your XML document

  49. Document / PI <?xml version=“1.0”?> DocumentType <!DOCTYPE BookCatalogue ...> Element BookCatalogue Element Book Element Book Element Book ... ... Element ISBN Element Publisher Element Author Element Date Element Title Text McMillin Publishing Text 94303-12021-43892 Text July, 1998 Text My Life ... Text Paul McCartney

  50. Creating a Stylesheet - Step 2 • Create a template rule for every type of node in your tree* • e.g., create one template rule for all the <Book> nodes, not one template rule for each of the nodes. * Except for the PI and DOCTYPE nodes. The current spec does not allow you to have a template rule for the DOCTYPE node. We will look at creating template rules for PI nodes in a later slide.

More Related