Click to return to the XML (Extensible Markup Language) home page    
Sample Data     XSL Pattern Syntax    
Web Workshop  |  XML (Extensible Markup Language)

XSL Pattern Examples


This topic recaps the examples that appear throughout this documentation. All are based on the provided Sample Data.

Find all author elements within the current context:

./author

Note that this is equivalent to:

author

Find all first.name elements:

first.name

Find the root element (bookstore) of this document:

/bookstore

Find all author elements anywhere within the current document:

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

author/first-name

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

bookstore//title

Note that this 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 where 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. Note that this example uses filters and attributes.

*[@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 is not 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. These examples are equivalent:

./first-name
first-name

Find all unqualified book elements:

book

For example, the following finds the first author element:

author[0]

The following finds the third author element that has a first-name:

author[first-name][2]

Note that indexes are relative to the parent. In other words, consider the following data:

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

Find the first y from each x:

x/y[0]
x/y[index() = 0] 

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

(x/y)[0]

Find the first y from the first x:

x[0]/y[0]

Find the last book:

book[end()]

Find the last author for each book:

book/author[end()]

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

(book/author)[end()]

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[$any$ last-name = "Bob"]
author[$any$ last-name $eq$ "Bob"] 

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

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

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

degree[@from != "Harvard"]
degree[@from $ne$ "Harvard"] 

Find all authors where the last name is the same as the /guest/last-name element (note that this assumes there is only one last-name; see Set Operations):

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

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 (note that this assumes there is only one last-name and price for an author; see Set Operations):

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

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

author[last-name $ge$ "M"]

When an author can have several last names in the schema (such as Clemens and Twain), use the following patterns:

author[$any$ last-name $ge$ "M"]
author[$all$ last-name $ge$ "M"] 

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

author[last-name $ige$ "M"]

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

book[index() $le$ 2]

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

author[$any$ last-name = "Bob"]

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

author[$all$ last-name != "Bob"]

Find all author elements containing a first-name child whose text is "Bob" (note that this and following samples assume there is only one first-name child for an author):

author[first-name!value() = "Bob"]

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

author[*!value() = "Bob"]

Find all books where the publication date is before January 1, 1995 (note that these samples assume there is only one publication date for a book):

books[pub_date < date("1995-01-01")] 

Find all books where the publication date is before the date value stored in the attribute first:

books[pub_date < date(@first)] 

The following examples are equivalent:

author[last-name!value() = "Bob" and first-name!value() = "Joe"]
author[last-name = "Bob" and first-name = "Joe"] 

The following examples are equivalent:

price[@intl!value() = "canada"]
price[@intl = "canada"] 

Find the first 3 degrees:

degree[index() $lt$ 3]

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

p/textnode()[1]

Find the nearest book ancestor of the current element:

ancestor(book)

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

ancestor(book/author)

See Also

Sample Data



Back to topBack to top

Did you find this topic useful? Suggestions for other topics? Write us!

© 1999 Microsoft Corporation. All rights reserved. Terms of use.