XPathNavigator and XSLT

Information about working with .NET.

 

XPathNavigator and XSLT
Dale Michalk

It's a sure bet that Windows developers will be dealing with Microsoft's .NET initiative for a long time to come. That means you have to get your ducks in a row about .NET pretty quickly. This tip, excerpted from InformIT, discusses the interaction between function XpathNavigator and XSLT.


XpathNavigator supports a robust set of navigational methods that are more comprehensive than those of XmlDocument and XmlReader. This list shows that we can move backward and forward to any node of any type of the document with ease.

  • MoveTo
  • MoveToAttribute
  • MoveToFirst
  • MoveToFirstAttribute
  • MoveToFirstChild
  • MoveToFirstNamespace
  • MoveToID
  • MoveToNamespace
  • MoveToNext
  • MoveToNextAttribute
  • MoveToNextNamespace
  • MoveToParent
  • MoveToPrevious
  • MoveToRoot

We bring back our venerable purchase order document from previous articles to demonstrate the XPathNavigator API. I added a few extra items to the order document shown in Listing 1 to provide a better demonstration of XPathNavigator's capabilities.

Listing 1: PO.xml Purchase Order

<?xml version="1.0" encoding="utf-8" ?>
<po:PurchaseOrder xmlns:po="http://michalk.com/
XmlDOM/PO.xsd" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Number>1001</Number>
<OrderDate>8/12/01</OrderDate>
<BillToAddress>
   <Street>101 Main Street</Street>
   <City>Charlotte</City>
   <State>NC</State>
   <ZipCode>28273</ZipCode>
</BillToAddress>
<ShipToAddress>
   <Street>101 Main Street</Street>
   <City>Charlotte</City>
   <State>NC</State>
   <ZipCode>28273</ZipCode>
</ShipToAddress>
<LineItem Name="Computer Desk" 
Description="Wood desk for computer" SKU="12345A123" Price="499.99" Qty="1" />
<LineItem Name="Monitor" 
Description="Computer Monitor 19-Inch" SKU="19A123" Price="299.99" Qty="1" />
<LineItem Name="Computer" 
Description="Pentium 4 Computer" SKU="411111" 
Price="999.99" Qty="1" />
<LineItem Name="Mouse" 
Description="Computer Mouse" SKU="233B1" 
Price="49.99" Qty="1" />
</po:PurchaseOrder>

Our first sample program in Listing 2 navigates through all the elements, attribute and text nodes of the documents displaying pertinent information about them. It starts out by loading the PO.xml file from disk into an XPathDocument and executes MoveToRoot to move the cursor to the root node of the document. From there it passes XPathNavigator to the recursive WalkNode static method of our demo class. This method displays the current node that the XPathNavigator cursor points to and walks through all the attributes to display them. It also recursively calls WalkNode for each child node to continue the process recursively. Attributes are accessed with the MoveToNextAttribute method calls. Child nodes are accessed using the MoveToFirstChild method first to position the first child and MoveToNext to cycle through the rest of the siblings. The MoveToParent call is used to jump back up to the parent of the attribute or child node. The output from the console is shown in Listing 3.

Listing 2: XPathNavigator Navigation Demo Code

using System;
using System.Xml;
using System.Xml.XPath;

namespace XmlDotNet
{

public class XPathNavDemo
{

   public static void Main()
   {
     // load the PO
     XPathDocument doc = new XPathDocument("PO.xml");

     // retrieve the navigator
     XPathNavigator nav = doc.CreateNavigator();
        
     // move navigator cursor to root node of document
     nav.MoveToRoot();

     // recursively walk the nodes of the root node
     WalkNode(nav, 0);        
        
   }

   public static void WalkNode(XPathNavigator nav, int depth)
   {
     // display the node information in a pretty format
     DisplayNode(nav, depth);
        
     // process child attribute nodes
     if (nav.HasAttributes)
     {
          
        // loop through all the attributes of the 
        // current node
        // and display them
        while(nav.MoveToNextAttribute())
          DisplayNode(nav, depth+1);

        // move back to the parent element node of the
        // attribute nodes
        nav.MoveToParent();
     }

     // process child element nodes
     if (nav.HasChildren)
     {
        // move to the first child of the 
        // current node
        nav.MoveToFirstChild();
             
        // recursively display the first child node
        // and its subnodes
        WalkNode(nav, depth+1);

        // loop through the rest of the siblings
        // recursively
        while (nav.MoveToNext())
          WalkNode(nav, depth+1);

        // move back to the original parent node
        nav.MoveToParent();
     }
   }

   // push the text over to the right to make printing pretty
   public static void PrintDepth(int depth)
   {
     for (int i=0; i < depth; i++)
        Console.Write("  ");
   }

   // display node information
   public static void DisplayNode(XPathNavigator nav, int depth)
   {
     // push the text over to the right to make 
     // printing pretty
     PrintDepth(depth);
               
     // display a node value for only text and 
     // attribute nodes
     string val = "";
     if (nav.NodeType == XPathNodeType.Attribute ||
        nav.NodeType == XPathNodeType.Text)
     {
        val = nav.Value;
     }
     Console.WriteLine("Name:{0} Type:{1} Value:{2}",
          nav.Name, nav.NodeType, val);
   }   

}
}

Listing 3: XPathNavigator Navigation Demo Results

Name: Type:Root Value:
  Name:po:PurchaseOrder Type:Element Value:
    Name:Number Type:Element Value:
      Name: Type:Text Value:1001
    Name:OrderDate Type:Element Value:
      Name: Type:Text Value:8/12/01
    Name:BillToAddress Type:Element Value:
      Name:Street Type:Element Value:
        Name: Type:Text Value:101 Main Street
      Name:City Type:Element Value:
        Name: Type:Text Value:Charlotte
      Name:State Type:Element Value:
        Name: Type:Text Value:NC
      Name:ZipCode Type:Element Value:
        Name: Type:Text Value:28273
    Name:ShipToAddress Type:Element Value:
      Name:Street Type:Element Value:
        Name: Type:Text Value:101 Main Street
      Name:City Type:Element Value:
        Name: Type:Text Value:Charlotte
      Name:State Type:Element Value:
        Name: Type:Text Value:NC
      Name:ZipCode Type:Element Value:
        Name: Type:Text Value:28273
    Name:LineItem Type:Element Value:
      Name:Name Type:Attribute Value:Computer Desk
      Name:Description Type:Attribute 
         Value:Wood desk for computer
      Name:SKU Type:Attribute Value:12345A123
      Name:Price Type:Attribute Value:499.99
      Name:Qty Type:Attribute Value:1
    Name:LineItem Type:Element Value:
      Name:Name Type:Attribute Value:Monitor
      Name:Description Type:Attribute 
         Value:Computer Monitor 19-Inch
      Name:SKU Type:Attribute Value:19A123
      Name:Price Type:Attribute Value:299.99
      Name:Qty Type:Attribute Value:1
    Name:LineItem Type:Element Value:
      Name:Name Type:Attribute Value:Computer
      Name:Description Type:Attribute 
         Value:Pentium 4 Computer
      Name:SKU Type:Attribute Value:411111
      Name:Price Type:Attribute Value:999.99
      Name:Qty Type:Attribute Value:1
    Name:LineItem Type:Element Value:
      Name:Name Type:Attribute Value:Mouse
      Name:Description Type:Attribute 
         Value:Computer Mouse
      Name:SKU Type:Attribute Value:233B1
      Name:Price Type:Attribute Value:49.99
      Name:Qty Type:Attribute Value:1

To read the article from which this tip is excerpted, click over to InformIT. You have to register there, but the registration is free.


This was first published in July 2002

Dig deeper on .NET Framework development with XML and XAML

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

This Content Component encountered an error
Close