17
The learning site: http://www.w3schools.com/ xpath / xpath _syntax.asp http://nwalsh.com/docs/ tutorials/xsl/xsl/ slides.html

The learning site: /xpath_syntax.asp xsl/xsl/slides.html

Embed Size (px)

Citation preview

Page 1: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

The learning site:http://www.w3schools.com/xpath/xpath_syntax.asphttp://nwalsh.com/docs/tutorials/xsl/xsl/slides.html

Page 2: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html
Page 3: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

<?xml version="1.0" encoding="ISO-8859-1"?>

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

Page 4: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

<?xml version="1.0" encoding="ISO-8859-1"?>

<!-- Edited with XML Spy v4.2 -->

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

Page 5: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

<?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 align="left">Title</th> <th align="left">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>

Page 6: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

What is XPath?

1. XPath is a syntax for defining parts of an XML document 2. XPath uses paths to define XML elements 3. XPath defines a library of standard functions 4. XPath is a major element in XSLT 5. XPath is not written in XML 6 .XPath is a W3C Standard

Page 7: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

XPath Example

<?xml version="1.0" encoding="ISO-8859-1"?>

<catalog>

<cd country="USA">

<title>Empire Burlesque</title>

<artist>Bob Dylan</artist>

<price>10.90</price>

</cd>

<cd country="UK">

<title>Hide your heart</title>

<artist>Bonnie Tyler</artist>

<price>9.90</price>

</cd>

<cd country="USA">

<title>Greatest Hits</title>

<artist>Dolly Parton</artist>

<price>9.90</price>

</cd>

</catalog>

Page 8: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

Location steps

• an axis (specifies the tree relationship between the nodes selected by the location step and the current node)

• a node test (specifies the node type and expanded-name of the nodes selected by the location step)

• zero or more predicates (use expressions to further refine the set of nodes selected by the location step)

Page 9: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

Axes

Page 10: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

ancestor Ancestors of the current node

ancestor-or-self - Ancestors, including the current node

attribute - Attributes of the current node (abbreviated "@")

child - Children of the current node (the default axis)

descendant - Descendants of the current node

descendant-or-self - Descendants, including the current node (abbreviated "//")

following / following-sibling - Elements which occur after the current node, in document order

preceding / preceding-sibling - Elements which occur before the current node, in document order (returned in reverse-document order)

namespace - The namespace nodes of the current node

parent - The parent of the current node (abbreviated "..")

self - The current node (abbreviated ".")

Page 11: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

child::cd -Selects all cd elements that are children of the current node (if the current node has no cd children, it will select an empty node-set)

attribute::src - Selects the src attribute of the current node (if the current node has no src attribute, it will select an empty node-set)

child::* - Selects all child elements of the current nodeattribute::* - Selects all attributes of the current nodechild::text() - Selects the text node children of the current nodechild::node() - Selects all the children of the current nodedescendant::cd - Selects all the cd element descendants of the current nodeancestor::cd - Selects all cd ancestors of the current nodeancestor-or-self::cd - Selects all cd ancestors of the current node and, if the current node is a cd element, the current node as well.

child::*/child::price - Selects all price grandchildren of the current node

/ - Selects the document root

Page 12: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

name - Matches <name> element nodes

* - Matches any element node

namespace:name - Matches <name> element nodes in the specified namespace

namespace:* - Matches any element node in the specified namespace

comment() - Matches comment nodes

text() - Matches text nodes

processing-instruction() - Matches processing instructions

processing-instruction('target’) - Matches processing instructions with the specified target

node() - Matches any node

Node Tests

Page 13: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

Predicatesnodetest[1] - Matches the first node

nodetest[position()=last()] - Matches the last node

nodetest[position() mod 2 = 0] - Matches even nodes

element[@id='foo'] - Matches the element(s) with id attributes whos value is "foo"

element[not(@id)] - Matches elements that don't have an id attribute

author[firstname="Norman"] - Match <author> elements that have <firstname> children with the content "Norman".

author[normalize-space(firstname)="Norman"] -

Match "Norman" without regard to leading and trailing space.

Predicate expressions can be more-or-less arbitrarily complex.

Page 14: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

- .self

@ - attribute (cd[@type="classic"] is short for child::cd[attribute::type="classic"])

- ..parent (../cd is short for parent::node()/child::cd)

- ////cd is short for descendant-or-self::node()/child::cd

Page 15: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

/catalog

/catalog/cd

/catalog/cd/price

/catalog/cd[price>10.80]

XPath Example(cont)

Page 16: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

XPath Example(cont)

/catalog/cd/price

/- represents an absolute path to an element

// - all elements in the document that fulfill the criteria will be selected

//cd

/catalog/cd/*

/catalog/*/price

/*/*/price

//*

/catalog/cd[1]

/catalog/cd[last()]

/catalog/cd[price]

/catalog/cd[price=10.90]

Page 17: The learning site:  /xpath_syntax.asp  xsl/xsl/slides.html

/catalog/cd[price=10.90]/price

/catalog/cd/title | /catalog/cd/artist

//@country

//cd[@country]

//cd[@*]

//cd[@country='UK']

XPath Example(cont)