Magazine |
| | Community |
| | Workshop |
| | Tools & Samples |
| | Training |
| | Site Info |
|
|
||||||||
|
XSL patterns are constructed using the operators and special characters shown in the following table.
/ | Child operator; selects immediate children of the left-side collection. When this path operator appears at the start of the pattern, it indicates that children should be selected from the root node. |
// | Recursive descent; searches for the specified element at any depth. When this path operator appears at the start of the pattern, it indicates recursive descent from the root node. |
. | Indicates the current context. |
* | Wildcard; selects all elements regardless of the element name. |
@ | Attribute; prefix for an attribute name. |
: | Namespace separator; separates the namespace prefix from the element or attribute name. |
! | Applies the specified method to the reference node. |
( ) | Groups operations to explicitly establish precedence. |
[ ] | Applies a filter pattern. |
[ ] | Subscript operator. Used for indexing within a collection. |
(Note that this table does not list Boolean and conditional expressions and does not list set operators. These are treated in subsequent topics.)
Precedence order (from highest precedence to lowest) is defined as indicated in the following table.
1. | ( ) | Grouping |
2. | [ ] | Filter pattern |
3. | / and // | Path operators |
These operators and special characters are described in detail throughout this reference. Other topics discuss set operators.
The collection of elements of a certain type can be determined using the path operators (/ or //). These operators take as their arguments a "left side" collection on which to perform the pattern-matching operation, and a "right side" collection indicating which elements to select. The child operator (/) selects from immediate children of the left-side collection, while the descendant operator (//) selects from arbitrary descendants of the left-side collection. In effect, the // can be considered a substitute for one or more levels of hierarchy.
Note that the path operators change the context as the pattern-matching operation is performed. By stringing path operators together, users can traverse the document tree.
Examples
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
An element can be referenced without using its name by substituting the wildcard (*) collection. The * collection returns all elements that are children of the current context, regardless of the tag name.
Examples
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 all elements with the specialty attribute. Note that this example uses filters and attributes.
*[$any$ @specialty]
The XSL pattern-matching notation denotes attribute names with the @ symbol. Attributes and child elements are treated impartially, and capabilities are equivalent between the two types wherever possible.
Note Attributes cannot contain child elements, so syntax errors occur when path operators are applied to attributes. In addition, you cannot apply an index to attributes because, by definition, no order is defined for attributes.
Examples
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[$any$ @style]
Find the style attribute for all book elements:
book/@style
All attributes of an element can be returned using @*. This is potentially useful for applications that treat attributes as fields in a record.
Examples
Find all attributes of the current element context:
@*
See Also
Does this content meet your programming needs? Write us!
© 1998 Microsoft Corporation. All rights reserved. Terms of use.