XML:命名空间preFIX声称,当它实际上是将未声明 [英] XML: A namespace prefix is claimed to be not declared when it fact it is

查看:230
本文介绍了XML:命名空间preFIX声称,当它实际上是将未声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经有了,它返回一个非常简单的XML Web服务。

We've got a web service that returns a 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>

调用者得到这个XML没有问题,使用XMLHTTP。 但是XPath查询不工作agains此XML,因为引用了未声明的命名空间preFIX:T

The caller gets this XML no problem, using XMLHTTP. But XPath queries don't work agains this XML because of "Reference to undeclared namespace prefix: 't'"

为什么会这样?我想说的'T'preFIX有所宣布。 以任何方式这个文件无效?

Why so? I'd say the 't' prefix is somewhat declared. Is this document invalid in any way?

如果你想知道为什么我们不得不<一个href="http://social.msdn.microsoft.com/forums/en/asmxandxml/thread/77aa3fb0-6b8a-4dc4-9308-0dcd5184c287/"相对=nofollow>使用XmlNamespaceDeclarations添加命名空间prefixes 的摆在首位,这是因为,否则所产生的文件cannod在becase的它有一个目标命名空间进行查询,但没有得到一个preFIX吧,这样的XPath忽略节点名称,因为它们不属于要求(空)命名空间,我们不wan't使用像建筑// * [命名空间URI() =HTTP://our.website.com/ns和本地名称()='RequestResult']

In case you wonder why we had to use XmlNamespaceDeclarations to add namespace prefixes in the first place, that is because otherwise the resulting document cannod be queried upon becase it has a target namespace but hasn't got a prefix for it, so XPath ignores node names because they don't belong to requested (empty) namespace, and we don't wan't to use constructions like "//*[namespace-uri()='http://our.website.com/ns' and local-name()='RequestResult']".

推荐答案

您已经回答了这个问题,但它是值得理解这是为什么。

You've already answered the question, but it's worth understanding why this is.

这一个元件是在该命名空间中不能仅由命名空间preFIX确定。要查找命名空间的元素命名为 T:富是,你要搜索出祖先或自身轴,直到找到定义的命名空间最近的节点 T:。例如:

The namespace that an element is in cannot be determined solely by the namespace prefix. To find what namespace an element named t:foo is in, you have to search up the ancestor-or-self axis until you find the nearest node that defines the namespace for t:. For instance:

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

在该文件中,每一个元素,它的名字叫一个 NS-一个命名空间,每个元素他的名字是两个 NS-两个命名空间。你可以告诉大家,在该文件中最深的元素是在 NS-两个不是因为 T:本质手段 NS-两个,而是因为如果你搜索了祖先或自身轴,第一个元素,你击中了的xmlns:T已属性就可以了 - 其母公司 - 告诉你的名字空间

In that document, every element whose name is one is in the ns-one namespace, and every element whose name is two is in the ns-two namespace. You can tell that the deepest element in that document is in ns-two not because t: intrinsically means ns-two, but because if you search up the ancestor-or-self axis, the first element that you hit with an xmlns:t attribute on it - its parent - tells you the namespace.

鉴于此,该节点应该在XPath的前pression // T:* 比赛?这很难说,因为什么命名空间 T:映射到整个文件的修改

Given that, which nodes should the XPath expression //t:* match? It's impossible to say, because what namespace t: is mapped to changes throughout the document.

此外,命名空间prefixes是暂时的,但命名空间是永久性的。如果你知道一个 NS-一个,你真的,真的不在乎是否其preFIX是 T: X:或者如果它没有preFIX可言,只是一个的xmlns 属性。

Also, namespace prefixes are temporary, but namespaces are permanent. If you know that one is in ns-one, you really, truly don't care whether its prefix is t: or x: or if it has no prefix at all and just an xmlns attribute.

当你使用XPath查询XML文档,你需要指定的命名空间,一个给定的元素是的一种方式。这就是 SelectionNamespaces 在DOM文档,或在C#中的命名空间管理器,或任何对:他们告诉你的命名空间prefixes在你的XPath查询重新present。所以,如果我已经设置了preFIX A: NS-一个,中的XPath //一:一个会找到我所有命名的元素一个 NS-一个命名空间。

When you're querying an XML document with XPath, you need a way of specifying what namespace a given element is in. And that's what SelectionNamespaces in a DOMDocument, or a namespace manager in C#, or whatever are for: they tell you what namespaces the prefixes in your XPath queries represent. So if I've set the prefix a: to ns-one, the XPath //a:one will find me all of the elements named one in the ns-one namespace, irrespective of what the actual prefix they're using in the document I'm searching is.

这是一个有点违反直觉,当你第一次学习它,但实际上,它使任何意义可言的唯一途径。

This is a little counterintuitive when you're first learning it, but really, it's the only way that makes any sense at all.

这篇关于XML:命名空间preFIX声称,当它实际上是将未声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆