开发者

How do I use XPath with a default namespace with no prefix?

开发者 https://www.devze.com 2022-12-25 05:43 出处:网络
What is the XPath (in C# API to XDocument.XPat开发者_Go百科hSelectElements(xpath, nsman) if it matters) to query all MyNodes from this document?

What is the XPath (in C# API to XDocument.XPat开发者_Go百科hSelectElements(xpath, nsman) if it matters) to query all MyNodes from this document?

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <MyNode xmlns="lcmp" attr="true">
    <subnode />
  </MyNode>
</configuration>
  • I tried /configuration/MyNode which is wrong because it ignores the namespace.
  • I tried /configuration/lcmp:MyNode which is wrong because lcmp is the URI, not the prefix.
  • I tried /configuration/{lcmp}MyNode which failed because Additional information: '/configuration/{lcmp}MyNode' has an invalid token.

EDIT: I can't use mgr.AddNamespace("df", "lcmp"); as some of the answerers have suggested. That requires that the XML parsing program know all the namespaces I plan to use ahead of time. Since this is meant to be applicable to any source file, I don't know which namespaces to manually add prefixes for. It seems like {my uri} is the XPath syntax, but Microsoft didn't bother implementing that... true?


The configuration element is in the unnamed namespace, and the MyNode is bound to the lcmp namespace without a namespace prefix.

This XPATH statement will allow you to address the MyNode element without having declared the lcmp namespace or use a namespace prefix in your XPATH:

/configuration/*[namespace-uri()='lcmp' and local-name()='MyNode']

It matches any element that is a child of configuration and then uses a predicate filer with namespace-uri() and local-name() functions to restrict it to the MyNode element.

If you don't know which namespace-uri's will be used for the elements, then you can make the XPATH more generic and just match on the local-name():

/configuration/*[local-name()='MyNode']

However, you run the risk of matching different elements in different vocabularies(bound to different namespace-uri's) that happen to use the same name.


You need to use an XmlNamespaceManager as follows:

   XDocument doc = XDocument.Load(@"..\..\XMLFile1.xml");
   XmlNamespaceManager mgr = new XmlNamespaceManager(new NameTable());
   mgr.AddNamespace("df", "lcmp");
   foreach (XElement myNode in doc.XPathSelectElements("configuration/df:MyNode", mgr))
   {
       Console.WriteLine(myNode.Attribute("attr").Value);
   }


XPath is (deliberately) not designed for for the case where you want to use the same XPath expression for some unknown namespaces that only live in the XML document. You are expected to know the namespace ahead of time, declare the namespace to the XPath processor, and use the name in your expression. The answers by Martin and Dan show how to do this in C#.

The reason for this difficulty is best expressed in the XML namespaces spec:

We envision applications of Extensible Markup Language (XML) where a single XML document may contain elements and attributes (here referred to as a "markup vocabulary") that are defined for and used by multiple software modules. One motivation for this is modularity: if such a markup vocabulary exists which is well-understood and for which there is useful software available, it is better to re-use this markup rather than re-invent it.

Such documents, containing multiple markup vocabularies, pose problems of recognition and collision. Software modules need to be able to recognize the elements and attributes which they are designed to process, even in the face of "collisions" occurring when markup intended for some other software package uses the same element name or attribute name.

These considerations require that document constructs should have names constructed so as to avoid clashes between names from different markup vocabularies. This specification describes a mechanism, XML namespaces, which accomplishes this by assigning expanded names to elements and attributes.

That is, namespaces are supposed to be used to make sure you know what your document is talking about: is that <head> element talking about the preamble to an XHTML document or somebodies head in an AnatomyML document? You are never "supposed" to be agnostic about the namespace and it's pretty much the first thing you ought to define in any XML vocabulary.

It should be possible to do what you want, but I don't think it can be done in a single XPath expression. First of all you need to rummage around in the document and extract all the namespaceURIs, then add these to the namespace manager and then run the actual XPath expression you want (and you need to know something about the distribution of namespaces in the document at this point, or you have a lot of expressions to run). I think you are probably best using something other than XPath (e.g. a DOM or SAX-like API) to find the namespaceURIs, but you could also explore the XPath namespace-axis (in XPath 1.0), use the namespace-uri-from-QName function (in XPath 2.0) or use expressions like Oleg's "configuration/*[local-name() = 'MyNode']". Anyway, I think your best bet is to try and avoid writing namespace agnostic XPath! Why do you not know your namespace ahead of time? How are you going to avoid matching things you don't intend to match?

Edit - you know the namespaceURI?

So it turns out that your question confused us all. Apparently you know the namespace URI, but you don't know the namespace prefix that's used in the XML document. Indeed, in this case no namespace prefix is used and the URI becomes the default namspace where it is defined. The key thing to know is that the chosen prefix (or lack of a prefix) is irrelevant to your XPath expression (and XML parsing in general). The prefix / xmlns attribute is just one way to associate a node with a namespace URI when the document is expressed as text. You may want to take a look at this answer, where I try and clarify namespace prefixes.

You should try to think of the XML document in the same way the parser thinks of it - each node has a namespace URI and a local name. The namespace prefix / inheritance rules just saves typing the URI out lots of times. One way to write this down is in Clark notation: that is, you write {http://www.example.com/namespace/example}LocalNodeName, but this notation is usually just used for documentation - XPath knows nothing about this notation.

Instead, XPath uses its own namespace prefixes.Something like /ns1:root/ns2:node. But these are completely separate from and nothing to do with any prefixes that may be used in the original XML document. Any XPath implementation will have a way to map it's own prefixes with namespace URIs. For the C# implementation you use an XmlNamespaceManager, in Perl you provide a hash, xmllint takes command line arguments... So all you need to do is create some arbitrary prefix for the namespace URI you know, and use this prefix in the XPath expression. It doesn't matter what prefix you use, in XML you just care about the combination of the URI and the localName.

The other thing to remember (it's often a surprise) is that XPath doesn't do namespace inheritance. You need to add a prefix for every that has a namespace, irrespective of whether the namespace comes from inheritance, an xmlns attribute, or a namespace prefix. Also, although you should always think in terms of URIs and localNames, there are also ways to access the prefix from an XML document. It's rare to have to use these.


Here's an example of how to make the namespace available to the XPath expression in the XPathSelectElements extension method:

using System;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml;
namespace XPathExpt
{
 class Program
 {
   static void Main(string[] args)
   {
     XElement cfg = XElement.Parse(
       @"<configuration>
          <MyNode xmlns=""lcmp"" attr=""true"">
            <subnode />
          </MyNode>
         </configuration>");
     XmlNameTable nameTable = new NameTable();
     var nsMgr = new XmlNamespaceManager(nameTable);
     // Tell the namespace manager about the namespace
     // of interest (lcmp), and give it a prefix (pfx) that we'll
     // use to refer to it in XPath expressions. 
     // Note that the prefix choice is pretty arbitrary at 
     // this point.
     nsMgr.AddNamespace("pfx", "lcmp");
     foreach (var el in cfg.XPathSelectElements("//pfx:MyNode", nsMgr))
     {
         Console.WriteLine("Found element named {0}", el.Name);
     }
   }
 }
}


Example with Xpath 2.0 + a library :

using Wmhelp.XPath2;

doc.XPath2SelectElements("/*:configuration/*:MyNode");

See :

XPath and XSLT 2.0 for .NET?


I like @mads-hansen, his answer, so well that I wrote these general-purpose utility-class members:

    /// <summary>
    /// Gets the <see cref="XNode" /> into a <c>local-name()</c>, XPath-predicate query.
    /// </summary>
    /// <param name="childElementName">Name of the child element.</param>
    /// <returns></returns>
    public static string GetLocalNameXPathQuery(string childElementName)
    {
        return GetLocalNameXPathQuery(namespacePrefixOrUri: null, childElementName: childElementName, childAttributeName: null);
    }

    /// <summary>
    /// Gets the <see cref="XNode" /> into a <c>local-name()</c>, XPath-predicate query.
    /// </summary>
    /// <param name="namespacePrefixOrUri">The namespace prefix or URI.</param>
    /// <param name="childElementName">Name of the child element.</param>
    /// <returns></returns>
    public static string GetLocalNameXPathQuery(string namespacePrefixOrUri, string childElementName)
    {
        return GetLocalNameXPathQuery(namespacePrefixOrUri, childElementName, childAttributeName: null);
    }

    /// <summary>
    /// Gets the <see cref="XNode" /> into a <c>local-name()</c>, XPath-predicate query.
    /// </summary>
    /// <param name="namespacePrefixOrUri">The namespace prefix or URI.</param>
    /// <param name="childElementName">Name of the child element.</param>
    /// <param name="childAttributeName">Name of the child attribute.</param>
    /// <returns></returns>
    /// <remarks>
    /// This routine is useful when namespace-resolving is not desirable or available.
    /// </remarks>
    public static string GetLocalNameXPathQuery(string namespacePrefixOrUri, string childElementName, string childAttributeName)
    {
        if (string.IsNullOrEmpty(childElementName)) return null;

        if (string.IsNullOrEmpty(childAttributeName))
        {
            return string.IsNullOrEmpty(namespacePrefixOrUri) ?
                string.Format("./*[local-name()='{0}']", childElementName)
                :
                string.Format("./*[namespace-uri()='{0}' and local-name()='{1}']", namespacePrefixOrUri, childElementName);
        }
        else
        {
            return string.IsNullOrEmpty(namespacePrefixOrUri) ?
                string.Format("./*[local-name()='{0}']/@{1}", childElementName, childAttributeName)
                :
                string.Format("./*[namespace-uri()='{0}' and local-name()='{1}']/@{2}", namespacePrefixOrUri, childElementName, childAttributeName);
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号