Microsoft XML Core Services (MSXML) 4.0 - XPath Reference

XPath Examples

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.

Find all author elements within the current context:

./author

Note that the above is equivalent to the following:

author

Find all first.name elements:

first.name

Find the document element (bookstore) of this document:

/bookstore

Find all author elements that are children of the current context node (for example, /bookstore/book):

//author

Find all bookstores where the value of the specialty attribute is equal to "textbooks":

/bookstore[@specialty = "textbooks"]

Find all books where the value of the style attribute on the book is equal to the value of the specialty attribute of the bookstore element at the root of the document:

book[/bookstore/@specialty = @style]

Find all first-name elements within an author element:

author/first-name

In the above example, note that the author children of the current context are found, and then first-name children are found relative to the context of the author elements.

Find all title elements one or more levels deep in the bookstore (arbitrary descendants):

bookstore//title

Note that the above is different from the following pattern, which finds all title elements that are grandchildren of bookstore elements:

bookstore/*/title

Find emph elements anywhere inside book excerpts, anywhere inside the bookstore:

bookstore//book/excerpt//emph

Find all titles 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):

.//title

Find all element children of author elements:

author/*

Find all last names that are grandchildren of books:

book/*/last-name

Find the grandchildren elements of the current context:

*/*

Find the book element from the "my" namespace:

my:book

Find all elements from the "my" namespace:

my:*

Find all elements with the specialty attribute:

*[@specialty]

Find the style attribute of the current element context:

@style

Find the exchange attribute on price elements within the current context:

price/@exchange

The following example does not return anything, because attributes do not contain element children. It is allowed by the XML Path Language (XPath) grammar, but is not strictly valid:

price/@exchange/total

Find all books with style attributes:

book[@style]

Find the style attribute for all book elements:

book/@style

Find all attributes of the current element context:

@*

Find all attributes from the "my" namespace (this does not include unqualified attributes on elements from the "my" namespace):

@my:*

Find all first-name elements (the following examples are equivalent):

./first-name
first-name

Find all unqualified book elements:

book

Find the first author element:

author[1]

Find the third author element that has a first-name:

author[first-name][3]

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

<x>
  <y/>
  <y/>
</x>
<x>
  <y/>
  <y/>
</x>

Find the first y from each x, for the above data:

x/y[1]
x/y[position() = 1]

Find the first y from the entire set of y elements within x elements:

(x/y)[1]

Find the second y from the first x:

x[1]/y[2]

Find the last book:

book[last()]

Find the last author for each book:

book/author[last()]

Find the last author from the entire set of authors of books:

(book/author)[last()]

Find all books that contain at least one excerpt element:

book[excerpt]

Find all titles of books that contain at least one excerpt element:

book[excerpt]/title

Find all authors of books where the book contains at least one excerpt and the author has at least one degree:

book[excerpt]/author[degree]

Find all books that have authors with at least one degree:

book[author/degree]

Find all books that have an excerpt and a title:

book[excerpt][title]

Find all author elements that contain at least one degree and one award:

author[degree and award]

Find all author elements that contain at least one degree or award and at least one publication:

author[(degree or award) and publication]

Find all author elements that contain at least one degree element and that contain no publication elements:

author[degree and not(publication)]

Find all author elements that contain publication elements but do not contain either degree elements or award elements:

author[not(degree or award) and publication]

Find all author elements that contain a last-name element with the value Bob:

author[last-name = "Bob"]

Find all author elements where the first last-name is Bob:

author[last-name[1] = "Bob"]
author[last-name = "Bob"]

Find all degrees where the from attribute is not equal to "Harvard":

degree[@from != "Harvard"]

Find all authors where the last name is the same as the /guest/last-name element:

author[last-name = /guest/last-name]

The above example assumes there is only one last-name; for more information, see Set Operations.

Find all authors whose text is "Matthew Bob":

author[. = "Matthew Bob"]

Find all author elements whose last name is "Bob" and whose price is > 50:

author[last-name = "Bob" and price &gt; 50]

Note that the above example assumes there is only one last-name and price for an author. For more information, see Set Operations.

Find all authors whose last name begins with "M" or greater:

author[last-name &gt; "M"]

Find all authors whose last name begins with "M" or greater, when an author can have several last names in the schema:

author[not(last-name &lt; "M")]

Note that in the above example, you cannot use author[all last-name &gt;= "M"], because all is not supported by XPath.

Find the first three books (1, 2, 3):

book[position() &lt;= 3]

Find all author elements where any one of the last names is Bob:

author[last-name = "Bob"]

Find all author elements where none of the last-name elements is Bob:

author[not(last-name = "Bob")]

Find all author elements containing a first-name child whose text is "Bob":

author[first-name = "Bob"]

Note that the above example and the following examples assume there is only one first-name child for an author.

Find all author elements containing any child element whose text is "Bob":

author[* = "Bob"]

Find author elements equal to "Joe Bob":

author[last-name = "Bob" and first-name = "Joe"]

Find the intl attribute equal to "Canada":

price[@intl = "Canada"]

Find the first 3 degrees:

degree[position() &lt; 3]

Find the second text node in each p element in the current context:

p/text()[2]

Find the nearest book ancestor of the current element:

ancestor::book[1]

Find the nearest ancestor book that has an author element:

ancestor::book[author][1]

Find the nearest ancestor author element that is contained in a book element:

ancestor::author[parent::book][1]

Example of Unions ( | )

This example demonstrates unions. The following XPath expression finds the green and blue nodes in the XML file:

x | y/x

XML File (data1.xml)

<?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 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:for-each>
</xsl:template>

</xsl:stylesheet>

Formatted Output

greenbluebluegreen

Processor Output

<?xml version="1.0" encoding="UTF-16"?>greenbluebluegreen

See Also

Sample XML File for XPath Syntax (inventory.xml) | Location Path Examples