XML: A namespace prefix is ​​allegedly not declared when it is a fact

We have a web service that returns very simple XML.

<?xml version="1.0"?>
<t:RequestResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://our.website.com/ns/" xmlns:t="http://our.website.com/ns/">
  <t:Result>No candy for you today.</t:Result>
  <t:Success>false</t:Success>
</t:RequestResult>

The caller receives this XML seamlessly using XMLHTTP. But XPath queries do not work again with this XML due to "Link to undeclared namespace prefix: 't'"

Why is that? I would say that the prefix 't' is somewhat declared. Is this document invalid?

If you are wondering why we had to use XmlNamespaceDeclarations to add namespace prefixes , this is because otherwise the resulting cannod document would be requested, because it has a target namespace, but it does not have a prefix, so XPath ignores the node names. because they do not belong to the requested (empty) namespace, and we do not want to use constructs for example "//*[namespace-uri()='http://our.website.com/ns' and local-name()='RequestResult']".

+3
source share
2 answers

You have already answered the question, but it is worth understanding why this is so.

, , . , t:foo, ancestor-or-self, node, t:. :

<t:one xmlns:t="ns-one">
   <t:one>
      <t:two xmlns:t="ns-two">
         <t:two/>
      </t:two>
   </t:one>
</t:one>

one ns-one, two ns-two. , ns-two , t: ns-two, , , , xmlns:t - - .

, XPath //t:*? , t: .

, , . , one ns-one, , t: x:, xmlns.

XML- XPath, , . SelectionNamespaces DOMDocument # - : , XPath. , a: ns-one, XPath //a:one one ns-one, , , , .

, , , .

+7

( ), XPath. XPath.

, SelectionNamespaces DOMObject.

objXML.setProperty("SelectionNamespaces", "xmlns:t='http://our.website.com/ns/'")

XPath , t:. , XmlNamespaceDeclarations.

+3

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


All Articles