1 / 25

XSLT Part Two

XSLT Part Two. What is XSLT. An XSLT processor compares the elements and other nodes in the source tree to the template rule patterns in a stylesheet. When one matches it writes the template to the result tree. Further serialization may occur so the result tree is in HTML or text format.

metea
Télécharger la présentation

XSLT Part Two

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. XSLT Part Two

  2. What is XSLT • An XSLT processor compares the elements and other nodes in the source tree to the template rule patterns in a stylesheet. When one matches it writes the template to the result tree. • Further serialization may occur so the result tree is in HTML or text format. • XSLT does not require a DTD or schema, but can work with them.

  3. Xsl:stylesheet and xsl:transform • They are synonyms for one another • There are no differences between the two elements; they both mean the same thing to an XSLT processor • All XSLT elements are in the http://www.w3.org/1999/XSL/Transform namespace. If even one character is wrong, the stylesheet processor will output the stylesheet itself. This is not a bug, it’s intentional. • IE5 and IE5.5 have poor support for xslt. Make sure you’re using IE6 • The namespace is customarily mapped to the xsl prefix

  4. Stylesheet Processors • An XSLT processor is a piece of software that reads an XSLT stylesheet, reads a source document and builds a result tree by applying the instructions in the stylesheet to the information in the result tree. • Can be built into the Web browser, a Web server, or a stand alone program run from the command line.

  5. Text/xsl Mime Type • Not a registered IAMA mime type • Used only by Microsoft • Non MS browsers should use “application/xml” mime type

  6. Templates • Templates tells the XSLT processor how to transform the node for output. • A Template uses an XPath expression to match a node or nodes in your XML document and let you specify what you want to do with the matched data. • The first match should point to the document element or a child of the root. • An xsl:template element is a template rule that contains a template! • It is NOT a template

  7. Templates Part Two • <xsl:template match=“person”> A person</xsl:template> indicates that EVERY time a person is found the text “A person” should be written to the browser window. • The markup you output must be well formed. This means all elements, even if they are HTML must have quoted attributes and end tags.

  8. Creating a Template • The following template will replace the XML document with a completely unrelated HTML document due to the fact that the template matches the document root. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> <title>My Useless Page</title> </head> <body> <h3>We included no information from the XML document in this page!</h3> </body> </html> </xsl:template> </xsl:stylesheet>

  9. Template’s Results We included no information from the XML document in this page! • This is because when the XSLT processor reads the document, the first node it sees is the root node. Since the rule matches the root node, the XSLT processor replaces the root with the HTML document.

  10. XSL: ApplyTemplates • If you use the xsl:template element then all other templates that are contained within the document will be ignored unless you include the <xsl:apply-templates/> element inside of the template definition. • You can apply templates to the children of a node that you’ve matched with <xsl:apply-templates/> • <xsl:apply-templates/> indicates that there are other templates to use on the child nodes of the current node.

  11. Xsl:ApplyTemplates Part 2 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="books"> <xsl:apply-templates/> </xsl:template> <xsl:template match="book"> <p> <xsl:value-of select="author"/> </p> </xsl:template> </xsl:stylesheet> • The books template would not have been automaticallly accessed if there were a template pointing to the document element using <xsl:template match=“/"> unless there was an <xsl:apply-templates/> included in the template.

  12. Xsl: apply-templates Part 3 • By default an XSLT processor reads the source document from top to bottom starting with the root. • Template rules are activated in the order in which they match elements encountered during the traversal. This means that template rules for parents are activated before template rules for children. • A template can change the traversal order by specifying that an element should be processed while in the processing another element. • It can even prevent further elements from being processed. • We have prevented elements from being processed by using xsl:template and xsl:value-of • Xsl:value-of allows you to display content from an XML document. • By default the XSLT processor never reaches attribute nodes.

  13. XSL:ApplyTemplates Part 4 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> <title>The Books Table</title> </head> <body> <table border="2" bgcolor="gray" style="color:white"> <tr> <th>Title</th> <th>Author</th> <th>Price</th> <th>Pages</th> </tr> <!--Apply Templates here to display all the book info --> <xsl:apply-templates/> </table> </body> </html> </xsl:template> <xsl:template match="book"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="author"/></td> <td><xsl:value-of select="price"/></td> <td><xsl:value-of select="@pages"/></td> </tr> </xsl:template> </xsl:stylesheet>

  14. Xsl:Value-Of • Element that calculates the string value of an XPath expression and inserts it into the output. • The value of an element is its text content after removing the mark up tags and resolving character entities. • The value of the select attribute is the name of the element or attribute whose value is going to be displayed. • See page 142 in the text.

  15. Xsl:Value-Of Part Two • If you want to display every element and attribute value, you can use the following match value: <xsl:template match=“text()|@*”> <xsl:value-of select=“.”/> </xsl:template> • The text() node test is an XPath pattern matching all text nodes. This returns the node’s text • @* is an XPath pattern matching all attribute nodes. This returns the attribute values. • The vertical bar combines the two patterns.

  16. Modes • Sometimes the same input content needs to appear multiple times in the output document, formatted according to a different template each time. • Xsl:apply-templates and xsl:template have a mode attribute that connect different template rules to different uses. • A mode attribute on an xsl:template element identifies in which mode that template rule should be activated. • An xsl:apply-templates element with a mode attribute only activates template rules with matching mode attributes.

  17. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="books"> <html> <head> <title>Information About Books</title> </head> <body> <h2 align="center">Book Table of Contents</h2> <ul> <xsl:apply-templates select="book" mode="toc"/> </ul> <h2 align="center">Book Data</h2> <xsl:apply-templates select="book"/> </body> </html> </xsl:template> <xsl:template match="book" mode="toc"> <li><xsl:value-of select="title"/> - <xsl:value-of select="author"></xsl:value-of></li> </xsl:template> <xsl:template match="book"> <p> Title: <xsl:value-of select="title"/> <br/> Author: <xsl:value-of select="author"/> <br/> Price: <xsl:value-of select="price"/> <br/> Pages: <xsl:value-of select="@pages"/> <br/> ISBN: <xsl:value-of select="@ISBN"/> <br/> Type: <xsl:value-of select="@type"/> <br/> </p> </xsl:template> </xsl:stylesheet>

  18. About the Previous Slide • The book elements are processed twice. • However, each uses a different template based on whether or not a mode attribute is set. • The template rule in TOC mode displays the books in an unordered list. • The modeless template rule displays all of the information about a book in paragraph form. • Using the Select attribute of the apply-templates element allows us to focus directly on processing book elements rather than recursively looking for all elements that have templates.

  19. <xsl: for-each> • Required when you have multiple child elements with the same name. For instance, if an address book has two address elements for EACH address entry. • In this case you’d want to use <xsl:for-each> to access all of the address elements.

  20. Understanding the Match and Select Attributes • Both can be assigned XPath expressions • Match is used with xsl:template • Select is used with xsl:value-of, xsl:apply-templates, xsl:for-each, xsl:copy-of, and xsl:sort XSLT elements • Match attribute is restricted to using the current node or direct child nodes. This restriction was made to make implementing XSLT processors easier. • No restrictions on XPath expressions you can assign to the Select attribute

  21. Using xsl:copy to Copy Nodes • Lets you copy a node from the source tree to the result tree. • Shallow copying occurs. This mean no descendants or attributes are copied. • You can copy attributes using code similar to the following: <xsl:copy> <xsl:for-each select="@*"> <xsl:copy /> </xsl:for-each> <xsl:apply-templates/> </xsl:copy> • This example copies all elements from the source document to the result document <xsl:template match=“*”> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template>

  22. The <xsl:copy-of> Element • Lets you make a deep copy of nodes • The node and all of its attributes are copied • Has one attribute, select, which is mandatory • Select specifies the node or node-set you want copied • An empty element that contains no content Select = “*” copies the entire document Select =“books/book” will copy all of the books

  23. XML To XML Conversions Use <xsl:element> and <xsl:attribute> <?xml version="1.0" encoding="UTF-8"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml"/> <xsl:template match="books"> <xsl:element name="bookcollection"> <xsl:apply-templates/> </xsl:element> </xsl:template> <xsl:template match="book"> <xsl:element name="aBook"> <xsl:attribute name="ISBN"> <xsl:value-of select="@ISBN"/> </xsl:attribute> <xsl:element name="pages"> <xsl:value-of select="@pages"/> </xsl:element> <xsl:element name="booktype"> <xsl:value-of select="@type"/> </xsl:element> <xsl:element name="title"> <xsl:value-of select="title"/> </xsl:element> <xsl:element name="author"> <xsl:value-of select="author"/> </xsl:element> <xsl:element name="price"> <xsl:value-of select="price"/> </xsl:element> </xsl:element> </xsl:template> </xsl:transform>

  24. The Result! <?xml version="1.0" encoding="UTF-8"?> <bookcollection> <aBook> <ISBN>1893115860</ISBN> <pages>357</pages> <booktype>soft</booktype> <title>A Programmer's Introduction to C#</title> <author>Eric Gunnerson</author> <price>34.95</price> </aBook> <aBook> <ISBN>189311595X</ISBN> <pages>380</pages> <booktype>hard</booktype> <title>Cryptography in C &amp; C++</title> <author>Michael Welschenbach</author> <price>12.22</price> </aBook> <aBook> <ISBN>1893115763</ISBN> <pages>400</pages> <booktype>soft</booktype> <title>C++ for VB Programmers</title> <author>Jonathan Morrison</author> <price>49.95</price> </aBook> </bookcollection>

  25. Displaying Messages • You can cause the XSLT processor to display a message and optionally end processing the stylesheet using <xsl:message> • If you set the terminate attribute to “yes” processing will be terminated

More Related