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 need an xpath expression that will get me <under>.

This has a namesURUR uri4 space.

Right now my expression looks like this:

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

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

I get the error "Expression must be evaluated using node-set."

I know that node exists. I know that everything pre4:underwill work fine in my xpath since I use it in the rest of the document without any problems. This is an additional pre4:underone that is causing the error, and I'm not sure why.

Any ideas?

Thank.

:

, . ... "pre4" "64" (), . "d" + myintvariable.

+3
2

, , XML. SketchPath, XPath node:

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

XPath? , , node, //under

+3

, .

#:

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
                              );
        }
    }
}

, , :

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

Source: https://habr.com/ru/post/1794831/


All Articles