Magazine |
| | Community |
| | Workshop |
| | Tools & Samples |
| | Training |
| | Site Info |
|
|
||||||||
|
The XSL pattern-matching syntax can be used as a general-purpose query notation for addressing and filtering the elements and text of XML documents. The pattern-matching syntax is supported in XSL and in the Document Object Model.
The Microsoft XSL processor extends the basic syntax in the XSL working draft to support more powerful pattern-matching operations. These extensions have been proposed to the XSL Working Group at http://www.w3.org/Style/XSL/Group/1998/09/XQL-proposal.html (W3C members only) and are currently under discussion.
This topic and the following topics introduce the XSL Pattern-Matching Syntax:
The XSL pattern-matching 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 the XSL pattern-matching syntax, the following means find the collection of book elements within bookstore elements:
bookstore/book
All pattern-matching operations 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 pattern-matching operation depends upon the context against which it is executed.
Pattern-matching operations 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 the XSL pattern-matching operations 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 pattern-matching operation.
A pattern prefixed with a forward slash (/) uses the root of the document tree as the context for the pattern-matching operation.
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[/bookstore/@specialty = @style]
In addition to this notation for representing hierarchical relationships among nodes, the implementation provided with Microsoft® Internet Explorer 5 Beta also supports Boolean and comparison expressions, set operations, filters, collections, and XSL Pattern-Matching Methods.
The pattern-matching syntax also supports 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-matching syntax as implemented in the Internet Explorer 5 Beta. Samples throughout this documentation are summarized in Examples, and refer to the data shown in Sample Data.
See Also
Does this content meet your programming needs? Write us!
© 1998 Microsoft Corporation. All rights reserved. Terms of use.