Microsoft XML 2.5 SDK


 

Introduction to the Syntax of XSL Patterns

[This is preliminary documentation and subject to change.]

XML documents represent a hierarchy or tree of nodes, similar to the hierarchy of directories and files found in a file system. This similarity is the basis for the similarity of syntax between URLs and XSL Patterns. Some analogous features are compared in the following table.

File System (URLs) XSL Patterns
Hierarchy comprised of directories and files in a file system. Hierarchy comprised of elements and other nodes in an XML document.
Files at each level have unique names—URLs always identify a single file. Element names at each level might not be unique—XSL Patterns identify a set of all the matching elements.
URLs are evaluated relative to a particular directory—called the "current directory". XSL Patterns are evaluated relative to a particular node—called the "context" for the query.

The following XML data represents a simple hierarchy, which will be used to demonstrate some of the basic query facilities of XSL Patterns.

<authors>
  <author>
    <name>Victor Hugo</name>
    <nationality>French</nationality>
  </author>
  <author period="classical">
    <name>Sophocles</name>
    <nationality>Greek</nationality>
  </author>
  <author>
    <name>Leo Tolstoy</name>
    <nationality>Russian</nationality>
  </author>
  <author>
    <name>Alexander Pushkin</name>
    <nationality>Russian</nationality>
  </author>
  <author period="classical">
    <name>Plato</name>
    <nationality>Greek</nationality>
  </author>
</authors>

XSL Patterns can use any node in the XML document as the context for a query. In an XSL style sheet, the context for a query is the source node being currently processed by an <xsl:template> or <xsl:for-each> element. When using XSL Patterns directly from the DOM, you define the context by performing the query from a particular node. For more information on how a context is defined, see How the DOM Defines the Context for XSL Pattern Queries and How XSL Defines the Context for XSL Pattern Queries. The examples in this topic use the context of the root of the XML document.

A basic XSL Pattern describes a path through the XML hierarchy with a slash-separated list of child element names. Starting from the document root, the following Pattern traverses down through the hierarchy of the sample XML document above to the "name" elements.

authors/author/name

The XSL Pattern identifies all the elements that match the path. Since XSL always has the potential for identifying a set of nodes in a tree, it can be used as a simple query mechanism.

Besides describing a path down a known hierarchy, XSL Patterns can include wildcards for describing unknown elements. An element of any name is represented by "*".

authors/*/name

The above query also identifies all the name elements, but doesn't require them to be children of an author element. Here is another example that finds both name and nationality elements.

authors/author/*

Branches on the path can also be specified, using square brackets. The following query describes a branch on the author element, indicating that only author elements with nationality children should be considered.

authors/author[nationality]/name

This becomes even more useful for our sample data when comparisons are added. The following query returns the names of Russian authors. Note that comparisons can be used only within brackets.

authors/author[nationality='Russian']/name

Attributes are indicated in a query by preceding the name of the attribute with "@". The attribute can be tested as a branch off the main path, or the query can identify attribute nodes. The following examples return authors from the classical period, and just the two period attributes, respectively.

authors/author[@period="classical"]
authors/author/@period

There are many more features available in XSL Patterns. The complete list supported by Microsoft® Internet Explorer 5 is described in the XSL Patterns Reference.