1 / 39

COMPS311F

COMPS311F. Li Tak Sing. XPath.

faith
Télécharger la présentation

COMPS311F

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. COMPS311F Li Tak Sing

  2. XPath • XPath is a simple language that allows you to write expressions to refer to different parts of an XML document. We will learn XSLT shortly which enables us to transform XML documents to other XML documents, HTML documents or text. But XSLT uses XPath expressions which are what we need to learn first. Our XPath examples will be based on the XML document below.

  3. <catalog> <product id="mug"> <price>16.00</price> <description>Coffee mug</description> </product> <product id="glass"> <price>25.00</price> <description>Beer glass</description> </product> </catalog>

  4. Nodes • Everything in an XML document is an XPath node. The most used nodes are element nodes, for example the catalogue, product, price and description above. XPath borrows its terminology from family trees. The catalogue node is the parent of the product node. The price node and description node are siblings which share the same ancestors of product and catalogue. The terms children and descendants have the obvious meaning. The table below summarizes the different XPath expressions we refer to above.

  5. XPath expressions

  6. XPath • If you have used command prompts on Windows or Unix, path expressions are not new to you. In an XSLT specification, you use XPath expressions to refer to specific parts of the XML document. This allows you to perform specific transformations selectively.

  7. Predicates • Predicates are used to filter the nodes that match an XPath expression. A predicate is enclosed in a pair of square brackets placed after an XPath expression. Numeric predicates are predicates that evaluate to integers. For example, we use the following expression to refer to the first product child of catalog which is the product with id mug. /catalog/product[1] • Likewise, we use the next expression to refer to the second product element with id glass. /catalog/product[2]

  8. Predicates • A predicate can also evaluate to a Boolean value. The following expression matches all the product elements priced less than fifteen dollars. /catalog/product [price < 15] • There are many operators and functions to help you build XPath predicates. For example, the following predicate makes use of the last( ) function. The expression refers to the second last product element. /catalog/product[last( ) -1].

  9. Axes • XPath provides 13 axes to help you select a set of nodes in relation to the current node, for example, descendant, following, ancestor, preceding, parent, self, attribute, etc. Axes are used with double colon before node names. Many uses of axes can be substituted with other mechanisms that you have learned. For example, the following two expressions are equivalent. The first expression makes use of the attribute axis./catalog/product/attribute::id /catalog/product/@id

  10. Extensible Style Language Transformation (XSLT) • XSLT is itself defined in XML syntax. An XSLT file takes the input from an XML file and transforms it into another file which could be XML, HTML or text. XSLT is more like a declarative language such as SQL than a conventional procedural language such as C. A good thing about XSLT is that all you need to run it is a Web browser. It is supported by all major Web browsers in the market including Mozilla Firefox, Microsoft Internet Explorer, Google Chrome, Opera, and Safari.

  11. XSLT Namespace • Keep in mind that XSLT files need to have the following root element. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> • This must be closed by a matching close tag at the end of the file as follows. </xsl:stylesheet>

  12. Referring to an XSLT file • The following is an XML document named cdcatalog.xml adapted from http://www.w3schools.com/xsl/. Its second processing instruction refers to an XSLT file called cdcatalogue.xslt stored in the same directory. When you open cdcatalog.xml from a Web browser, the transformations in cdcatalog.xslt will be invoked automatically. Both xsl and xslt are legitimate extensions for XSLT files. By default, Liquid XML Studio uses xslt.

  13. Referring to an XSLT file <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="cdcatalog.xslt"?> <catalog> <cd> <title>The Freewheelin'</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1963</year> </cd>

  14. <cd> <title>One night only</title> <artist>Bee Gees</artist> <country>UK</country> <company>Polydor</company> <price>10.90</price> <year>1998</year> </cd>

  15. <cd> <title>Maggie May</title> <artist>Rod Stewart</artist> <country>UK</country> <company>Pickwick</company> <price>8.50</price> <year>1990</year> </cd>

  16. <cd> <title>Romanza</title> <artist>Andrea Bocelli</artist> <country>EU</country> <company>Polydor</company> <price>10.80</price> <year>1996</year> </cd> </catalog>

  17. <xsl:template> • Here is our first version of the cdcatalog.xslt which contains a template. Templates are the basic building blocks of XSLT. We will build this file incrementally until it has the desired functionalities. <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="cd"> Hello </xsl:template> </xsl:stylesheet>

  18. <xsl:template> • The XSLT file has one xsl:template. From now on, we may simply call it template. It has the match attribute with the XPath expression value cd. By default, the current node is the root element catalog. It has four child elements of cd. There are four CDs in the XML file to match this template four times. Therefore Hello appears four times in the output as you can see in the screen shot below.

  19. <xsl:apply-templates> • Even if you have multiple templates defined in an XSLT file, only the top-level template with the match attribute closest to the root will be executed automatically. <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> My CD Collection </xsl:template> <xsl:template match="/catalog/cd/title"> Title </xsl:template> </xsl:stylesheet>

  20. <xsl:apply-templates> • Opening the XML document referring to this XSLT gives the following result.

  21. <xsl:apply-templates> • If a template is applied, the string inside the template will appear in the output. The two templates try to produce My CD Collection and Title. But we can only see the first string in the output. Clearly, the second template with match="/catalog/cd/title" is never executed. If we want to execute templates for nodes at various levels, we should start from the root and call xsl:apply-templates explicitly to apply the templates for the lower-level nodes. Consider our next XSLT stylesheet. Keep in mind that it does not matter what order you write the template rules.

  22. <xsl:apply-templates> <?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> <body> <h2>My CD Collection</h2> <xsl:apply-templates select="catalog/cd"/> </body> </html> </xsl:template>

  23. <xsl:apply-templates> <xsl:template match="catalog/cd"> <p> <xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/> </p> </xsl:template> <xsl:template match="/catalog/cd/title"> Title: <xsl:value-of select="." /> <br></br> </xsl:template>

  24. <xsl:apply-templates> <xsl:template match="artist"> Artist: <xsl:value-of select="." /> <br></br> </xsl:template> </xsl:stylesheet>

  25. <xsl:apply-templates> • The XSLT stylesheet has four templates. Each template has a match attribute which decides the kind of nodes that it will handle. The template with match value / handles the root element. This template produces a few HTML tags including a level-2 heading My CD Collection. The root template is the only one that will be applied automatically. Other templates must be manually applied with xsl:apply-template tags. For example, our root template invokes xsl:apply-templates for catalog/cd. The current node is / and the select value is "catalog/cd". Concatenating the two, we have an XPath expression "/catalog/cd". Do we have nodes matching this XPath expression in the document? Yes. Do we have a template defined that can match this XPath expression? Yes, again. The invocation succeeds. If at least one of the answers to the two questions is negative, the invocation will not happen.

  26. <xsl:apply-templates> • The second template matches "catalog/cd". It invokes xsl:apply-templates for title and artist. The result of the two calls are enclosed in a pair of paragraph tags <p> and </p>. The current node is "/catalog/cd". The first apply-templates has select value "title". Appending the value to the current node, we have an XPath expression "/catalog/cd/title". The nodes in the XML document that match this XPath expression will be used for the execution of the third template in the XSLT file.

  27. The apply-templates for "artist" yields the XPath expression "/catalog/cd/artist". This XPath expression will also match the template with select value "artist". Note that the select value of just "artist" works as well as a more detailed XPath expression "/catalog/cd/artist". The minor difference between the short and the long expressions is that the later requires node artist to be a child of /catalog/cd. Since we do not have other nodes with child node artist, the two expressions make no difference to us.

  28. The last two templates in our XSLT stylesheet contain the following. In case you don’t remember <br> is the line break tag in HTML. Title: <xsl:value-of select="." /> <br></br> Artist: <xsl:value-of select="." /> <br></br> • The HTML code can be seen on a Web browser as follows in the screen shot below. The two occurrences of the <xsl:value-of select="." /> tag return the title and the singer of the CD depending on the current node in the template.

  29. In our construction of the XSLT stylesheet, we define an automatically applied template for the root node. In that template, we call apply-templates to navigate down the child nodes and output HTML or XML code. The select attribute in apply-templates, the match attribute in templates and the nodes in the XML document being processed must agree.

  30. <xsl:apply-templates> without select attribute • In the previous example, we selectively applied templates to title and artist. However, if we omit the select attribute, the Web browser will try to match all child nodes of the current node to a template as can be seen below.

  31. <?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> <body> <h2>My CD Collection</h2> <xsl:apply-templates select="catalog/cd"/> </body> </html> </xsl:template>

  32. <xsl:apply-templates> without select attribute <xsl:template match="catalog/cd"> <p> <xsl:apply-templates /> </p> </xsl:template> <xsl:template match="title"> Title: <xsl:value-of select="." /> <br></br> </xsl:template>

  33. <xsl:template match="artist"> Artist: <xsl:value-of select="." /> <br></br> </xsl:template> <xsl:template match="country"> Country: <xsl:value-of select="." /> <br></br> </xsl:template> <xsl:template match="company"> Company: <xsl:value-of select="." /> <br></br> </xsl:template>

  34. <xsl:template match="year"> Year: <xsl:value-of select="." /> <br></br> </xsl:template> <xsl:template match="price"> Price: <xsl:value-of select="." /> <br></br> </xsl:template> </xsl:stylesheet>

  35. Pay attention to the second xsl:template with match attribute value "catalog/cd". It contains an xsl:apply-templates element without the select attribute. With no select attributes, the xsl:apply-templates attempts to find an appropriate template for its nodes. This XSLT stylesheet gives the following output as can be seen in the screen shot below.

  36. <xsl:if> • However, suppose we are only interested in displaying CDs that cost over 10 dollars. In that case we can modify the second template for "catalog/cd" as follows. <xsl:template match="catalog/cd"> <xsl:if test="price &gt; 10"> <p> <xsl:apply-templates /> </p> </xsl:if> </xsl:template>

More Related