Top Banner
Li Tak Sing COMPS311F
39

COMPS311F

Mar 18, 2016

Download

Documents

FAITH

COMPS311F. Li Tak Sing. XPath. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: COMPS311F

Li Tak Sing

COMPS311F

Page 2: COMPS311F

XPathXPath 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.

Page 3: COMPS311F

<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>

Page 4: COMPS311F

NodesEverything 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.

Page 5: COMPS311F

XPath expressionsXPath expression

Meaning

/ The beginning slash stands for the root node which is the document element.

/catalog It stands for the catalog element. /catalog/product It stands for the product element which is the

child node of the catalog element. product Note that there is no beginning slash in this

expression. That means we are not starting from the root node but from the current node. This expression is only meaningful if the current node has a child node called product. The expression is meaningful when catalog is our current node. This expression refers to its child product element.

. A single dot stands for the current node whatever that may be.

.. The double dot stands for the parent node of the current node.

/catalog/product/@id

This expression uses the @ symbol to refer to the id attribute.

Page 6: COMPS311F

XPathIf 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.

Page 7: COMPS311F

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]

Page 8: COMPS311F

PredicatesA 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].

Page 9: COMPS311F

AxesXPath 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

Page 10: COMPS311F

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.

Page 11: COMPS311F

XSLT NamespaceKeep 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>

Page 12: COMPS311F

Referring to an XSLT fileThe 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.

Page 13: COMPS311F

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>

Page 14: COMPS311F

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

</cd>

Page 15: COMPS311F

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

</cd>

Page 16: COMPS311F

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

</cd> </catalog>

Page 17: COMPS311F

<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>

Page 18: COMPS311F

<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.

Page 19: COMPS311F

<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>

Page 20: COMPS311F

<xsl:apply-templates>Opening the XML document referring to

this XSLT gives the following result.

Page 21: COMPS311F

<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.

Page 22: COMPS311F

<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>

Page 23: COMPS311F

<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>

Page 24: COMPS311F

<xsl:apply-templates><xsl:template match="artist"> Artist:

<xsl:value-of select="." /> <br></br> </xsl:template> </xsl:stylesheet>

Page 25: COMPS311F

<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.

Page 26: COMPS311F

<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.

Page 27: COMPS311F

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.

Page 28: COMPS311F

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.

Page 29: COMPS311F
Page 30: COMPS311F

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.

Page 31: COMPS311F

<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.

Page 32: COMPS311F

<?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>

Page 33: COMPS311F

<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>

Page 34: COMPS311F

<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>

Page 35: COMPS311F

<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>

Page 36: COMPS311F

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.

Page 37: COMPS311F
Page 38: COMPS311F

<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>

Page 39: COMPS311F