Home

XPath Axes

     

XPath Keywords

An axis is a technique of locating a node or a series of nodes using a path. To address this issue, the XPath language provides some keywords that can be used in the expressions. As we are going to see, some keywords are optional and some other keywords are necessary or useful. If you decide to include one of those keywords in your XPath expression, you must use only a valid keyword. If you use a keyword that is not recognized, the compiler will throw an XPathException exception.

When used, an axis keyword is followed by ::, followed by the name of a node or an operator.

Child Nodes

The child keyword is used to indicate that a child node must be accessed. The child keyword is followed by ::. If you want to see all child nodes, follow :: with *. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/child::*");

        foreach (XmlNode xnVideo in xnlVideos)
            MessageBox.Show(xnVideo.InnerXml,
                            "Video Collection",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);

        return 0;
    }
}

That code would produce all nodes that are direct children of video. If a child node includes its own child nodes, the node and all its children would be considered as one. To get only specific child nodes, follow :: by the name of the child node to access. Here is an example that accesses the director child nodes:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/child::director");

        foreach (XmlNode xnVideo in xnlVideos)
            MessageBox.Show(xnVideo.InnerXml,
                            "Video Collection",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);

        return 0;
    }
}

In the same way, you can access any child node by preceding it with the child keyword. In most cases, you can omit the child keyword. For example this:

XmlNodeList xnlVideos = xeVideo.SelectNodes(".//video/*/actor");

Is the same as:

using System;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    ListBox lbxDirectors;

    public Exercise()
    {
        lbxDirectors = new ListBox();
        lbxDirectors.Location = new Point(12, 12);
        Controls.Add(lbxDirectors);

        Text = "Video Collection";

        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes(".//video/*/child::actor");

        foreach (XmlNode xnVideo in xnlVideos)
            lbxDirectors.Items.Add(xnVideo.InnerText);
    }

    [STAThread]
    public static int Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
	Application.Run(new Exercise());

        return 0;
    }
}

You can also apply any of the rules we have used so far. For example, the following code will produce the child nodes of the second video if the child node is named cast-members:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise : Form
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video[2]/child::cast-members");

        foreach (XmlNode xnVideo in xnlVideos)
            MessageBox.Show(xnVideo.OuterXml,
                            "Video Collection",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);

        return 0;
    }
}

This would produce:

 

Accessing Specific Nodes

Accessing Specific Nodes

Parent Nodes

The parent keyword is used to get the parent of an element. Here is an example:

using System;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    ListBox lbxDirectors;

    public Exercise()
    {
        lbxDirectors = new ListBox();
        lbxDirectors.Size = new System.Drawing.Size(100, 80);
        lbxDirectors.Location = new Point(12, 12);
        Controls.Add(lbxDirectors);

        Text = "Video Collection";

        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes("//parent::director");

        foreach (XmlNode xnVideo in xnlVideos)
            lbxDirectors.Items.Add(xnVideo.InnerText);
    }

    [STAThread]
    public static int Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Exercise());

        return 0;
    }
}
 
 
 

The Ancestors of a Node

The ancestors of a node are its parent and grand-parent, up to the root of the XML file. To get the ancestors of a node, precede its name with the ancestor keyword. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes("//ancestor::video");

        foreach (XmlNode xnVideo in xnlVideos)
            MessageBox.Show(xnVideo.InnerXml,
                            "Video Collection",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);

        return 0;
    }
}

The Descendants of a Node

The descendants of a node are its child(ren) and grand-child(ren), down to the last grand-child of that node. To get the descendants of a node, precede its name with the descendant keyword.

The Previous Sibling of a Node

The previous sibling of a node is a node that comes before it in the tree while both nodes are on the same level. To get the collection of nodes that come before a certain node, precede its name with the preceding keyword. Here is an example:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes("//preceding::categories");

        foreach (XmlNode xnVideo in xnlVideos)
            MessageBox.Show(xnVideo.OuterXml,
                            "Video Collection",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);

        return 0;
    }
}

If your expression indludes the preceding keyword, the result would include the node itself and the next node of the same name in the same tree level. If you want to exclude the node itself from the result, in other words if you want to get only the next sibliing of the same level, use the preceding-sibling keyword.

The Next Sibling a Node

The following keyword is used to get the next node(s) that come after the referenced one but of the same tree level. Consider the following expression:

using System;
using System.Xml;
using System.Windows.Forms;

public class Exercise : Form
{
    public static int Main()
    {
        XmlDocument xdVideos = new XmlDocument();

        xdVideos.Load("../../Videos.xml");

        XmlElement xeVideo = xdVideos.DocumentElement;
        XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/following::director");

        foreach (XmlNode xnVideo in xnlVideos)
            MessageBox.Show(xnVideo.OuterXml,
                            "Video Collection",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);

        return 0;
    }
}

This code would access the director nodes, excluding the first one.

 
 
   
 

Home Copyright © 2014, FunctionX