Microsoft XML 2.5 SDK


 

XSL Pattern Syntax

[This is preliminary documentation and subject to change.]

XSL Patterns can be used as a general-purpose query notation for addressing and filtering the elements and text of XML documents. XSL Patterns are supported in XSL and in the Document Object Model.

The Microsoft® Internet Explorer 5 XSL processor extends the basic syntax in the XSL working draft to support more powerful queries. These extensions have been proposed to the W3C at XQL Proposal.

This topic and the following topics introduce the syntax of XSL Patterns:

The XSL Pattern notation is declarative, rather than procedural. Each pattern describes the types of nodes to match using a notation that indicates the hierarchical relationship between the nodes. For example, the pattern "book/author" means find author elements contained in book elements.

The notation enables you to query the XML document tree, which represents the contents of the XML document as a hierarchy of nodes. The syntax mimics the URI (universal resource identifier) directory navigation syntax, but instead of specifying navigation through a logical hierarchy of directories, the navigation is through a hierarchy of nodes in the XML tree.

For example, the following URI means find the background.jpg file within the images directory:

images/background.jpg

Similarly, in an XSL Pattern, the following means find the collection of book elements within bookstore elements:

bookstore/book

Context

All XSL Pattern queries occur within a particular context, that is, at the level of a particular node within the tree representation.

A "context" is the single node against which the pattern matching operates. To understand the concept of context, consider a tree containing nodes. Asking for all nodes named X from the root of the tree returns one set of results, while asking for those nodes from a branch in the tree returns a different set of results. The result of a query depends upon the context against which it is executed.

XSL Pattern queries can match specific patterns at one particular context, retrieve the results, and perform additional operations relative to the context of the returned nodes. This notation gives XSL Pattern queries extraordinary flexibility in searching throughout the document tree.

When using patterns with the Microsoft XML DOM programming interfaces, the context is the node object whose selectNodes or selectSingleNode method is called.

A pattern prefixed with a period and forward slash (./) explicitly uses the current context as the context for the query.

A pattern prefixed with a forward slash (/) uses the root of the document tree as the context for the query.

A pattern can use the // operator to indicate a search that can include zero or more levels of hierarchy. When this operator appears at the beginning of the pattern, the context is relative to the root of the document. The .// prefix indicates that the context starts at the level in the hierarchy indicated by the current context.

Note that element names can include the period character (.), and these can be used in patterns just as any other name.

Examples

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 "bookstore" element at the root of this document:

/bookstore

Find the root element of this document:

/*

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[@style = /bookstore/@specialty]

In addition to this notation for representing hierarchical relationships among nodes, the implementation provided with Microsoft Internet Explorer 5 also supports Boolean, Comparison, and Set Expressions, Set Operations, Filters and Filter Patterns, Collections, and XSL Pattern Methods.

XSL Patterns also support namespaces and data types. In this release, namespace prefixes can be included in patterns so that the matching operations can check for specific namespace prefixes. The string returned by the nodeName method includes the namespace prefix.

This reference describes the XSL Pattern syntax as implemented in Internet Explorer 5. Samples throughout this documentation are summarized in XSL Pattern Examples, and refer to the data shown in Sample Data.