开发者

XPath and XML: Multiple namespaces

So I have a document that looks like

<a xmlns="uri1" xmlns:pre2="uri2">
 <b xmlns:pre3="uri3">
   <pre3:c>
     <stuff></stuff>
     <goes></goes>
     <here></here>
   </pre3:c>
   <pre3:d xmlns="uri4">
     <under></under>
     <the></the>
     <tree></tree>
   </pre3:d>
  </b>
</a>

I want an xpath expression that will get me <under>.

This has a namespaceURI of uri4.

Right now my expression looks like:

//ns:a/ns:b/pre3:d/pre4:under

I have the namespace manager add 'ns' for the default namespace (uri1 in this case) and I have it defined with pre2, pre3, and pre4 for uri2, uri3, and uri4 respectively.

I get the error "Expression must evaluate to a node-set."

I know that the node exists. I know that everything up until the pre4:under in my xpath works fine as I use it in the rest of the document with no issues. It's the additional pre4:under that causes the error, and I'm not sure why.

Any ideas?

Thanks.

Resolution:

Thank you all for your insistence that it's correct--it was. But... in my code I had "pre4" as "64" (a variab开发者_运维问答le) and it didn't like an integer for a prefix. Changing it to "d" + myintvariable worked.


My guess is that there may be a bug with the implementation that you are using to navigate the XML. Using SketchPath, the following XPath navigated to the node successfully:

/def:a/def:b/pre3:d/def2:under

Could you try specifying different prefixes for the namespaces in the XPath? Otherwise, if performance isn't really an issue, and it's a unique node, you could just try //under


There is an error in your code, which you have not shown to us.

This C# console application:

using System;
using System.Xml;


namespace Namespaces
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(
                @"<a xmlns='uri1' xmlns:pre2='uri2'>
                    <b xmlns:pre3='uri3'>
                        <pre3:c>
                            <stuff></stuff>
                            <goes></goes>
                            <here></here>
                        </pre3:c>
                        <pre3:d xmlns='uri4'>
                            <under></under>
                            <the></the>
                            <tree></tree>
                        </pre3:d>
                    </b>
                </a>"                
                       );
            XmlNamespaceManager nsman = 
                new XmlNamespaceManager(new NameTable());
            nsman.AddNamespace("ns", "uri1");
            nsman.AddNamespace("pre2", "uri2");
            nsman.AddNamespace("pre3", "uri3");
            nsman.AddNamespace("pre4", "uri4");

            Console.WriteLine(
                doc.SelectSingleNode("/")
                  .SelectNodes("//ns:a/ns:b/pre3:d/pre4:under",

                                nsman)[0].OuterXml
                              );
        }
    }
}

when executed, produces the wanted, correct result:

<under xmlns="uri4"></under>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜