Magazine |
| | Community |
| | Workshop |
| | Tools & Samples |
| | Training |
| | Site Info |
|
|
||||||||
|
November 4, 1998
The Microsoft XSL processor extends the basic syntax in the XSL working draft to support more powerful pattern-matching and querying 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.
An XSL pattern is a string that identifies a specific XML node at a specific location in the XML tree. They can be used in XSL to match XML nodes with desired formatting directives or in the XML object model to retrieve a node list containing specific types of nodes.
The selectNodes() method on the XML node can be passed an XSL pattern as a parameter. Based on the context of the node, this pattern is then used to locate matching nodes and return those nodes as a nodelist.
Take the following XML document:
<authors> <author> <name>Victor Hugo</name> <nationality>French</nationality> </author> <author> <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> <name>Plato</name> <nationality>Greek</nationality> </author> </authors>
To retrieve all Russian authors, you could use the follwing code:
xmldoc.documentElement.selectNodes("author[nationality='Russian']")
This code retrieves all of the root node's child nodes with the node name of "author", which, in turn, have a child node with the node name of "nationality" with the value of "Russian."
You could also retrieve all name nodes:
xmldoc.documentElement.selectNodes("//name")
The pattern "//name" says retrieve all "name" nodes regardless of their position in the tree. The "/" symbol is used to denote a single level in the tree; the "//" symbol to denote all descendant levels in the tree. Remember that the context of this particular call is the root node (the document element). Consequently, if we were to use the following code:
xmldoc.documentElement.selectNodes("/name")no nodes would be returned, because the root node "authors" has no child nodes with the node name of "name".
The XSL pattern syntax is described in greater detail in the XSL Pattern-Matching Syntax section of the Workshop.
Retrieval of these nodes by walking the tree would take a substantial amount of code. With the selectNodes method and XSL patterns, nodes can be retrieved through a single line of code.
For an example of how to use XSL pattern matching within an Web application, see the Book Finder demo in the XML samples area.
The Visual Basic application in the XML samples area allows you to load an XML file and retrieve nodes through XSL pattern matching. Use this application to become familiar with the XSL pattern syntax and to use selectNodes within different contexts.
Did you find this article useful? Gripes? Compliments? Suggestions for other articles? Write us!
© 1998 Microsoft Corporation. All rights reserved. Terms of use.