Tuesday, September 22, 2009

Xpath Example


This topic reviews the syntax examples that appear throughout the XPath Reference. All are based on the Sample XML File for XPath Syntax (inventory.xml). For an example of using an XPath expression in a test file, see "Example of Unions ( | )", at the bottom of this topic.





Expression Refers to

Xml CopyCode imageCopy Code
./author

All <author> elements within the current context. Note that this is equivalent to the expression in the next row.

Xml Copy imageCopy Code
author

All <author> elements within the current context.

Xml Copy imageCopy Code
first.name

All <first.name> elements within the current context.

Xml CopyCode imageCopy Code
/bookstore

The document element (<bookstore>) of this document.

Xml CopyCode imageCopy Code
//author

All <author> elements in the document.

Xml CopyCode imageCopy Code
book[/bookstore/@specialty = @style]

All <book> elements whose style attribute value is equal to the specialty attribute value of the <bookstore> element at the root of the document.

Xml CopyCode imageCopy Code
author/first-name

All <first-name> elements that are children of an <author> element.

Xml CopyCode imageCopy Code
bookstore//title

All <title> elements one or more levels deep in the <bookstore> element (arbitrary descendants). Note that this is different from the expression in the next row.

Xml CopyCode imageCopy Code
bookstore/*/title

All <title> elements that are grandchildren of <bookstore> elements.

Xml CopyCode imageCopy Code
bookstore//book/excerpt//emph

All <emph> elements anywhere inside <excerpt> children of <book> elements, anywhere inside the <bookstore> element.

Xml CopyCode imageCopy Code
.//title

All <title> elements one or more levels deep in the current context. Note that this situation is essentially the only one in which the period notation is required.

Xml CopyCode imageCopy Code
author/*

All elements that are the children of <author> elements.

Xml CopyCode imageCopy Code
book/*/last-name

All <last-name> elements that are grandchildren of <book> elements.

Xml CopyCode imageCopy Code
*/*

All grandchildren elements of the current context.

Xml CopyCode imageCopy Code
*[@specialty]

All elements with the specialty attribute.

Xml CopyCode imageCopy Code
@style

The style attribute of the current context.

Xml CopyCode imageCopy Code
price/@exchange

The exchange attribute on <price> elements within the current context.

Xml CopyCode imageCopy Code
price/@exchange/total

Returns an empty node set, because attributes do not contain element children. This expression is allowed by the XML Path Language (XPath) grammar, but is not strictly valid.

Xml CopyCode imageCopy Code
book[@style]

All <book> elements with style attributes, of the current context.

Xml Copy imageCopy Code
book/@style

The style attribute for all <book> elements of the current context.

Xml CopyCode imageCopy Code
@*

All attributes of the current element context.

Xml CopyCode imageCopy Code
./first-name

All <first-name> elements in the current context node. Note that this is equivalent to the expression in the next row.

Xml CopyCode imageCopy Code
first-name

All <first-name> elements in the current context node.

Xml CopyCode imageCopy Code
author[1]

The first <author> element in the current context node.

Xml CopyCode imageCopy Code
author[first-name][3]

The third <author> element that has a <first-name> child.

Xml Copy imageCopy Code
my:book

The <book> element from the my namespace.

Xml CopyCode imageCopy Code
my:*

All elements from the my namespace.

Xml CopyCode imageCopy Code
@my:*

All attributes from the my namespace (this does not include unqualified attributes on elements from the my namespace).

Note that indexes are relative to the parent. Consider the following data:

Xml CopyCode imageCopy Code
<x>  <y/>  <y/></x><x>  <y/>  <y/></x>
Expression Refers to
Xml CopyCode imageCopy Code
x/y[1]

The first <y> child of each <x>. This is equivalent to the expression in the next row.

Xml CopyCode imageCopy Code
x/y[position() = 1]

The first <y> child of each <x>.

Xml Copy imageCopy Code
(x/y)[1]

The first <y> from the entire set of <y> children of <x> elements.

Xml CopyCode imageCopy Code
x[1]/y[2]

The second <y> child of the first <x>.

The remaining examples refer to the Sample XML file for XPath.

Expression Refers to

book[last()]

The last <book> element of the current context node.

Xml CopyCode imageCopy Code
book/author[last()]

The last <author> child of each <book> element of the current context node.

Xml CopyCode imageCopy Code
(book/author)[last()]

The last <author> element from the entire set of <author> children of <book> elements of the current context node.

Xml CopyCode imageCopy Code
book[excerpt]

All <book> elements that contain at least one <excerpt> element child.

Xml CopyCode imageCopy Code
book[excerpt]/title

All <title> elements that are children of <book> elements that also contain at least one <excerpt> element child.

Xml CopyCode imageCopy Code
book[excerpt]/author[degree]

All <author> elements that contain at least one <degree> element child, and that are children of <book> elements that also contain at least one <excerpt> element.

Xml CopyCode imageCopy Code
book[author/degree]

All <book> elements that contain <author> children that in turn contain at least one <degree> child.

Xml CopyCode imageCopy Code
author[degree][award]

All <author> elements that contain at least one <degree> element child and at least one <award> element child.

Xml Copy imageCopy Code
author[degree and award]

All <author> elements that contain at least one <degree> element child and at least one <award> element child.

Xml CopyCode imageCopy Code
author[(degree or award) and publication]

All <author> elements that contain at least one <degree> or <award> and at least one <publication> as the children

Xml CopyCode imageCopy Code
author[degree and not(publication)]

All <author> elements that contain at least one <degree> element child and that contain no <publication> element children.

Xml CopyCode imageCopy Code
author[not(degree or award) and publication]

All <author> elements that contain at least one <publication> element child and contain neither <degree> nor <award> element children.

author[last-name = "Bob"]

All <author> elements that contain at least one <last-name> element child with the value Bob.

Xml CopyCode imageCopy Code
author[last-name[1] = "Bob"]

All <author> elements where the first <last-name> child element has the value Bob. Note that this is equivalent to the expression in the next row.

Xml CopyCode imageCopy Code
author[last-name [position()=1]= "Bob"]

All <author> elements where the first <last-name> child element has the value Bob.

Xml CopyCode imageCopy Code
degree[@from != "Harvard"]

All <degree> elements where the from attribute is not equal to "Harvard".

Xml CopyCode imageCopy Code
author[. = "Matthew Bob"]

All <author> elements whose value is Matthew Bob.

Xml CopyCode imageCopy Code
author[last-name = "Bob" and ../price &gt; 50]

All <author> elements that contain a <last-name> child element whose value is Bob, and a <price> sibling element whose value is greater than 50.

Xml CopyCode imageCopy Code
book[position() &lt;= 3]

The first three books (1, 2, 3).

Xml CopyCode imageCopy Code
author[not(last-name = "Bob")]

All <author> elements that do no contain <last-name> child elements with the value Bob.

Xml CopyCode imageCopy Code
author[first-name = "Bob"]

All <author> elements that have at least one <first-name> child with the value Bob.

Xml CopyCode imageCopy Code
author[* = "Bob"]

all author elements containing any child element whose value is Bob.

Xml CopyCode imageCopy Code
author[last-name = "Bob" and first-name = "Joe"]

All <author> elements that has a <last-name> child element with the value Bob and a <first-name> child element with the value Joe.

Xml CopyCode imageCopy Code
price[@intl = "Canada"]

All <price> elements in the context node which have an intl attribute equal to "Canada".

Xml CopyCode imageCopy Code
degree[position() &lt; 3]

The first two <degree> elements that are children of the context node.

Xml CopyCode imageCopy Code
p/text()[2]

The second text node in each <p> element in the context node.

Xml CopyCode imageCopy Code
ancestor::book[1]

The nearest <book> ancestor of the context node.

Xml CopyCode imageCopy Code
ancestor::book[author][1]

The nearest <book> ancestor of the context node and this <book> element has an <author> element as its child.

Xml CopyCode imageCopy Code
ancestor::author[parent::book][1]

The nearest <author> ancestor in the current context and this <author> element is a child of a <book> element.

Example of Unions ( | )

To demonstrate the union operation, we use the following XPath expression:

x | y/x

selects all the <x> elements whose values are green or blue in the following XML file:

XML File (data1.xml)

Xml CopyCode imageCopy Code
<?xml version='1.0'?><?xml-stylesheet type="text/xsl" href="union.xsl"?><root>   <x>green</x>   <y>      <x>blue</x>      <x>blue</x>   </y>   <z>      <x>red</x>      <x>red</x>   </z>   <x>green</x></root>

XSLT File (union.xsl)

Xml CopyCode imageCopy Code
<?xml version='1.0'?><xsl:stylesheet version="1.0"      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="root">   <xsl:for-each select="x | y/x">      <xsl:value-of select="."/>,      <xsl:if test="not(position()=last())">,</xsl:if>   </xsl:for-each></xsl:template> </xsl:stylesheet>

Formatted Output

green,blue,blue,green

Processor Output

<?xml version="1.0" encoding="UTF-16"?>green,blue,blue,green

No comments: