Microsoft HomeproductssearchsupportshopWrite Us   Microsoft Home
Magazine
 |  Community
 |  Workshop
 |  Tools & Samples
 |  Training
 |  Site Info

Workshop  |  XML (Extensible Markup Language)

XML Tutorial
Lesson 9: Using XSL Patterns to Retrieve Nodes


November 4, 1998

What is an XSL pattern?

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 Non-MS link (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.

How do I retrieve XML nodes with XSL patterns?

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.

Try it!

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!

Back to topBack to top

© 1998 Microsoft Corporation. All rights reserved. Terms of use.

 

Magazine Home
Ask Jane
DHTML Dude
Extreme XML
For Starters
More or Hess
Servin' It Up
Site Lights
Web Men Talking
Member Community Home
Benefits: Freebies & Discounts
Benefits: Promote Your Site
Benefits: Connect with Your Peers
Benefits at a Glance
Online Special-Interest Groups
Your Membership
SBN Stores
Join Now
Workshop Home
Essentials
Content & Component Delivery
Component Development
Data Access & Databases
Design
DHTML, HTML & CSS
Extensible Markup Language (XML)
Languages & Development Tools
Messaging & Collaboration
Networking, Protocols & Data Formats
Reusing Browser Technology
Security & Cryptography
Server Technologies
Streaming & Interactive Media
Web Content Management
Workshop Index
Tools & Samples Home
Tools
Samples, Headers, Libs
Images
Sounds
Style Sheets
Web Fonts
Training Home
SBN Live Seminars
SBN Live Chats
Courses
Peer Support
CD-ROM Training
Books & Training Kits
Certification
SBN Home
New to SBN?
What's New on SBN
Site Map
Site Search
Glossary
Write Us
About This Site