Blog - Title

Generating an XPath expression to find a LINQ to XML Node

Generating an XPath expression to find a LINQ to XML Node

  • Comments 5

In a number of places in the docs, I present code that finds nodes in the XML tree. Sometimes there are easy ways to describe the results of a query, but sometimes I wanted to describe the results of a query by specifically identifying exactly which nodes are selected by a query. Having a string that specifically identifies a node makes it easy to write sample code that selects specific nodes and then shows the exact results.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC
Well, we already have a syntax that allows us to identify a specific node in an XML tree: XPath

Further, there are extension methods in System.Xml.XPath that allow us to evaluate an XPath expression, returning the node(s) that the expression selects.

So, I wrote a method, GetXPath, implemented as an extension method on System.Xml.Linq.XObject, that returns an XPath expression that identifies the node in the XML tree. The implementation is fairly complete - for instance, it generates an XPath expression that contains namespace prefixes when the nodes are in a namespace.

The extension methods in System.Xml.XPath allow us to validate that the XPath expressions that we generate select the exact same node as was used to generate the XPath expression.

I also wrote another useful axis method, DescendantXObjects, which returns an IEnumerable<XObject> that contains all child nodes, and all attributes of any nodes.

Then, I wrote a method, DumpXPaths, that iterates through the descendant XObjects and prints the XPath for every node to the console. This method also validates that the node returned by evaluating the XPath expression is the same node as was used to generate the XPath expression. It also validates that one and only one node is returned when evaluating the XPath expression. For example, the following code creates a simple XML tree, and calls DumpXPaths:

XDocument root = XDocument.Parse(@"<Root AnAttribute='att-value'>
  <?xml-stylesheet type='text/xsl' href='hello.xsl'?>
  <Child1 AnotherAttribute='abc'>text</Child1>
  <!--This is a comment.-->
</Root>");
DumpXPaths(root);

This code produces the following output:

.

/Root

/Root/@AnAttribute

/Root/processing-instruction()

/Root/Child1

/Root/Child1/@AnotherAttribute

/Root/Child1/text()

/Root/comment()

The DumpXPaths method can also take an XmlNamespaceManager, which allows the code that validates the XPath expression to validate expressions that contain namespace prefixes. 

The XPath expressions generated by this method work when evaluating in the context of an XDocument, not an XElement. If you parse into an XElement, the root node is the XElement, but if you parse into an XDocument, the root XElement node is a child of the XDocument. The generated XPath expressions reflect this.

If the GetXPath method returns null, then the method did not generate an XPath expression to select the node. This is, AFAIK, only true for white space text nodes that are children of a document; such nodes are not part of the XPath object model, so it's not possible to generate an XPath expression to select them.

Here is the entire working program to show the XPath expressions for every node in an XML tree. You can get the PurchaseOrders.xml document from the documentation, or you can change the code to dump the nodes for your own XML tree:

using System;

using System.Diagnostics;

using System.Collections;

using System.Collections.Generic;

using System.Text;

using System.Linq;

using System.Xml;

using System.Xml.Linq;

using System.Xml.XPath;

 

namespace LinqToXmlExample

{

    public static class MyExtensions

    {

        private static string GetQName(XElement xe)

        {

            string prefix = xe.GetPrefixOfNamespace(xe.Name.Namespace);

            if (xe.Name.Namespace == XNamespace.Blank || prefix == null)

                return xe.Name.LocalName.ToString();

            else

                return prefix + ":" + xe.Name.LocalName.ToString();

        }

 

        private static string GetQName(XAttribute xa)

        {

            string prefix =

                xa.Parent.GetPrefixOfNamespace(xa.Name.Namespace);

            if (xa.Name.Namespace == XNamespace.Blank || prefix == null)

                return xa.Name.ToString();

            else

                return prefix + ":" + xa.Name.LocalName;

        }

 

        private static string NameWithPredicate(XElement el)

        {

            if (el.Parent != null && el.Parent.Elements(el.Name).Count() != 1)

                return GetQName(el) + "[" +

                    (el.ElementsBeforeSelf(el.Name).Count() + 1) + "]";

            else

                return GetQName(el);

        }

 

        public static string StrCat<T>(this IEnumerable<T> source,

            string separator)

        {

            return source.Aggregate(new StringBuilder(),

                       (sb, i) => sb

                           .Append(i.ToString())

                           .Append(separator),

                       s => s.ToString());

        }

 

        public static string GetXPath(this XObject xobj)

        {

            if (xobj.Parent == null)

            {

                XDocument doc = xobj as XDocument;

                if (doc != null)

                    return ".";

                XElement el = xobj as XElement;

                if (el != null)

                    return "/" + NameWithPredicate(el);

                XText xt = xobj as XText;

                if (xt != null)

                    return null;

                    //

                    //the following doesn't work because the XPath data

                    //model doesn't include white space text nodes that

                    //are children of the document.

                    //

                    //return

                    //    "/" +

                    //    (

                    //        xt

                    //        .Document

                    //        .Nodes()

                    //        .OfType<XText>()

                    //        .Count() != 1 ?

                    //        "text()[" +

                    //        (xt

                    //        .NodesBeforeSelf()

                    //        .OfType<XText>()

                    //        .Count() + 1) + "]" :

                    //        "text()"

                    //    );

                    //

                XComment com = xobj as XComment;

                if (com != null)

                    return

                        "/" +

                        (

                            com

                            .Document

                            .Nodes()

                            .OfType<XComment>()

                            .Count() != 1 ?

                            "comment()[" +

                            (com

                            .NodesBeforeSelf()

                            .OfType<XComment>()

                            .Count() + 1) +

                            "]" :

                            "comment()"

                        );

                XProcessingInstruction pi = xobj as XProcessingInstruction;

                if (pi != null)

                    return

                        "/" +

                        (

                            pi.Document.Nodes()

                            .OfType<XProcessingInstruction>()

                            .Count() != 1 ?

                            "processing-instruction()[" +

                            (pi

                            .NodesBeforeSelf()

                            .OfType<XProcessingInstruction>()

                            .Count() + 1) +

                            "]" :

                            "processing-instruction()"

                        );

                return null;

            }

            else

            {

                XElement el = xobj as XElement;

                if (el != null)

                {

                    return

                        "/" +

                        el

                        .Ancestors()

                        .InDocumentOrder()

                        .Select(e => NameWithPredicate(e))

                        .StrCat("/") +

                        NameWithPredicate(el);

                }

                XAttribute at = xobj as XAttribute;

                if (at != null)

                    return

                        "/" +

                        at

                        .Parent

                        .AncestorsAndSelf()

                        .InDocumentOrder()

                        .Select(e => NameWithPredicate(e))

                        .StrCat("/") +

                        "@" + GetQName(at);

                XComment com = xobj as XComment;

                if (com != null)

                    return

                        "/" +

                        com

                        .Parent

                        .AncestorsAndSelf()

                        .InDocumentOrder()

                        .Select(e => NameWithPredicate(e))

                        .StrCat("/") +

                        (

                            com

                            .Parent

                            .Nodes()

                            .OfType<XComment>()

                            .Count() != 1 ?

                            "comment()[" +

                            (com

                            .NodesBeforeSelf()

                            .OfType<XComment>()

                            .Count() + 1) + "]" :

                            "comment()"

                        );

                XCData cd = xobj as XCData;

                if (cd != null)

                    return

                        "/" +

                        cd

                        .Parent

                        .AncestorsAndSelf()

                        .InDocumentOrder()

                        .Select(e => NameWithPredicate(e))

                        .StrCat("/") +

                        (

                            cd

                            .Parent

                            .Nodes()

                            .OfType<XText>()

                            .Count() != 1 ?

                            "text()[" +

                            (cd

                            .NodesBeforeSelf()

                            .OfType<XText>()

                            .Count() + 1) + "]" :

                            "text()"

                        );

                XText tx = xobj as XText;

                if (tx != null)

                    return

                        "/" +

                        tx

                        .Parent

                        .AncestorsAndSelf()

                        .InDocumentOrder()

                        .Select(e => NameWithPredicate(e))

                        .StrCat("/") +

                        (

                            tx

                            .Parent

                            .Nodes()

                            .OfType<XText>()

                            .Count() != 1 ?

                            "text()[" +

                            (tx

                            .NodesBeforeSelf()

                            .OfType<XText>()

                            .Count() + 1) + "]" :

                            "text()"

                        );

                XProcessingInstruction pi = xobj as XProcessingInstruction;

                if (pi != null)

                    return

                        "/" +

                        pi

                        .Parent

                        .AncestorsAndSelf()

                        .InDocumentOrder()

                        .Select(e => NameWithPredicate(e))

                        .StrCat("/") +

                        (

                            pi

                            .Parent

                            .Nodes()

                            .OfType<XProcessingInstruction>()

                            .Count() != 1 ?

                            "processing-instruction()[" +

                            (pi

                            .NodesBeforeSelf()

                            .OfType<XProcessingInstruction>()

                            .Count() + 1) + "]" :

                            "processing-instruction()"

                        );

                return null;

            }

        }

 

        public static IEnumerable<XObject> DescendantXObjects(this XObject source)

        {

            yield return source;

            XElement el = source as XElement;

            if (el != null)

                foreach (XAttribute att in el.Attributes())

                    if (!att.IsNamespaceDeclaration)

                        yield return att;

            XContainer con = source as XContainer;

            if (con != null)

                foreach (XNode child in con.Nodes())

                    foreach (XObject s in child.DescendantXObjects())

                        yield return s;

        }

    }

 

    class Program

    {

        static void DumpXPaths(XDocument doc)

        {

            DumpXPaths(doc, null);

        }

 

        static void DumpXPaths(XDocument doc,

            XmlNamespaceManager namespaceManager)

        {

            foreach (XObject n in doc.DescendantXObjects())

            {

                string xpath = n.GetXPath();

                if (xpath != null)

                {

                    Console.WriteLine("{0}", xpath);

                    IEnumerable xo;

                    if (namespaceManager == null)

                        xo = (IEnumerable)doc.XPathEvaluate(xpath);

                    else

                        xo = (IEnumerable)doc.XPathEvaluate(xpath,

                            namespaceManager);

                    if (n.GetType() == typeof(XElement))

                    {

                        XElement rnode =

                            xo

                            .Cast<XElement>()

                            .FirstOrDefault();

                        Debug.Assert(

                            xo

                            .Cast<object>()

                            .Count() == 1 ||

                            rnode == n,

                            "Invalid XPath evaluation");

                    }

                    else if (n.GetType() == typeof(XAttribute))

                    {

                        XAttribute rnode =

                            xo

                            .Cast<XAttribute>()

                            .FirstOrDefault();

                        Debug.Assert(

                            xo

                            .Cast<object>()

                            .Count() == 1 ||

                            rnode == n,

                            "Invalid XPath evaluation");

                    }

                    else if (n.GetType() == typeof(XComment))

                    {

                        XComment rnode =

                            xo

                            .Cast<XComment>()

                            .FirstOrDefault();

                        Debug.Assert(

                            xo

                            .Cast<object>()

                            .Count() == 1 ||

                            rnode == n,

                            "Invalid XPath evaluation");

                    }

                    else if (n.GetType() == typeof(XProcessingInstruction))

                    {

                        XProcessingInstruction rnode =

                            xo

                            .Cast<XProcessingInstruction>()

                            .FirstOrDefault();

                        Debug.Assert(

                            xo

                            .Cast<object>()

                            .Count() == 1 ||

                            rnode == n,

                            "Invalid XPath evaluation");

                    }

                    else if (n.GetType() == typeof(XText))

                    {

                        XText rnode =

                            xo

                            .Cast<XText>()

                            .FirstOrDefault();

                        Debug.Assert(

                            xo

                            .Cast<object>()

                            .Count() == 1 ||

                            rnode == n,

                            "Invalid XPath evaluation");

                    }

                }

            }

        }

 

        static void Main(string[] args)

        {

            XDocument root = XDocument.Parse(

                @"<Root AnAttribute='att-value'>

                    <?xml-stylesheet type='text/xsl' href='hello.xsl'?>

                    <Child1 AnotherAttribute='abc'>text</Child1>

                    <!--This is a comment.-->

                  </Root>");

            DumpXPaths(root);

            Console.WriteLine("---------------");

 

            XmlReader reader = XmlReader.Create("PurchaseOrders.xml");

            XDocument purchaseOrders = XDocument.Load(reader);

            XmlNameTable nameTable = reader.NameTable;

            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(nameTable);

            namespaceManager.AddNamespace("aw", "www.adventureworks.com");

            DumpXPaths(purchaseOrders, namespaceManager);

        }

    }

}

 

Leave a Comment
  • Please add 4 and 8 and type the answer here:
  • Post
  • Not sure why to use Linq to Xml for this. It can be done with a pretty short and more elegant Xslt stylesheet

    <?xml version="1.0"?>

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="/">

    <xsl:value-of select="'/&#xA;'" />

    <xsl:apply-templates select="node()" />

    </xsl:template>

    <xsl:template match="node()" priority="1" >

    <xsl:param name="parent-XPath" />

    <xsl:variable name="current-XPath"

    select="concat($parent-XPath, '/', name())" />

    <xsl:value-of select="concat($current-XPath, '&#xA;')" />

    <xsl:if test="boolean(node() | @*)">

    <xsl:apply-templates select="node() | @*">

    <xsl:with-param name="parent-XPath" select="$current-XPath" />

    </xsl:apply-templates>

    </xsl:if>

    </xsl:template>

    <xsl:template match="@*">

    <xsl:param name="parent-XPath" />

    <xsl:value-of select="concat($parent-XPath, '/@', name(), '&#xA;')" />

    </xsl:template>

    <xsl:template match="text()" priority="2">

    <xsl:param name="parent-XPath" />

    <xsl:value-of select="concat($parent-XPath, '/text()', '&#xA;')" />

    </xsl:template>

    <xsl:template match="comment()" priority="2">

    <xsl:param name="parent-XPath" />

    <xsl:value-of select="concat($parent-XPath, '/comment()', '&#xA;')" />

    </xsl:template>

    <xsl:template match="processing-instruction()" priority="2">

    <xsl:param name="parent-XPath" />

    <xsl:value-of select="concat($parent-XPath, '/processing-instruction()', '&#xA;')" />

    </xsl:template>

    </xsl:stylesheet>

    Cheers

    Pawel

  • One reason is perf. I'm pretty sure the LINQ to XML query will execute much faster than the XSLT transform. Of course, XSLT is useful in many scenarios, and may be more applicable than LINQ to XML in many of them.

  • XSLT is also not a structured procedural language, and has many limitations, one of which is performance

  • I agree with Pawel.  I think elegance AND readability of the code is more important here.  yeah, sure you can argue that LINQ it's milliseconds faster, but look at that code...

    as for XSLT being "slower" (which i think is debatable)...it's not dog slow if you're going to use XsltCompiledTransform object.  

    and I wonder what's the connection between XSLT's performance to it being "not a structured procedural language."

  • Actually, the point is not regarding elegance or readability of the code, nor is the point perf. The point is that when using LINQ to XML, in many cases, you assemble an XML tree, and you want to identify all of the nodes in the tree.  How do you do that?  You need a string or some means to identify each node.  So the appropriate string is an XPath expression.  It would be rather slow to fire off an XSLT transform to generate an XPath expression that you are using just identify a node.

Page 1 of 1 (5 items)