为什么XmlNamespaceManager的必要吗? [英] Why is XmlNamespaceManager necessary?

查看:274
本文介绍了为什么XmlNamespaceManager的必要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经出来有点干干为为什么 - 至少在.Net框架 - 这是需要使用的XmlNamespaceManager 为了处理命名空间(或相当笨重和详细 [本地名称()= ... 的XPath predicate /功能/等等)时执行XPath查询。 我做明白为什么命名空间是必要的或至少是有益的,但​​为什么是如此复杂?

I've come up kinda dry as to why -- at least in the .Net Framework -- it is necessary to use an XmlNamespaceManager in order to handle namespaces (or the rather clunky and verbose [local-name()=... XPath predicate/function/whatever) when performing XPath queries. I do understand why namespaces are necessary or at least beneficial, but why is it so complex?

为了查询一个简单的XML文档(没有命名空间)...

In order to query a simple XML Document (no namespaces)...

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootNode>
   <nodeName>Some Text Here</nodeName>
</rootNode>

...可以使用类似 doc.SelectSingleNode(//节点名称)(这将匹配&LT;节点名称&GT;有的文字此处&lt; /节点名称&GT;

奥秘#1 我的第一个烦恼 - 如果我理解正确 - 是仅仅增加一个命名空间的引用是否用作部分父/根标签(子节点标签或没有)像这样的:

Mystery #1: My first annoyance -- If I understand correctly -- is that merely adding a namespace reference to the parent/root tag (whether used as part of a child node tag or not) like so:

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootNode xmlns="http://someplace.org">
   <nodeName>Some Text Here</nodeName>
</rootNode>

......需要code一些额外的线路,以获得相同的结果:

...requires several extra lines of code to get the same result:

Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("ab", "http://s+omeplace.org")
Dim desiredNode As XmlNode = doc.SelectSingleNode("//ab:nodeName", nsmgr)

...基本上是做梦了一个不存在的preFIX( AB )地发现,甚至不使用preFIX节点。 如何与这是否有道理?的什么是错的(概念)doc.SelectSingleNode(//节点名称)

...essentially dreaming up a non-existent prefix ("ab") to find a node that doesn't even use a prefix. How does this make sense? What is wrong (conceptually) with doc.SelectSingleNode("//nodeName")?

奥秘#2 的:所以,说你有一个使用prefixes的XML文档:

Mystery #2: So, say you've got an XML document that uses prefixes:

<?xml version="1.0" encoding="ISO-8859-1"?>
<rootNode xmlns:cde="http://someplace.org" xmlns:feg="http://otherplace.net">
   <cde:nodeName>Some Text Here</cde:nodeName>
   <feg:nodeName>Some Other Value</feg:nodeName>
   <feg:otherName>Yet Another Value</feg:otherName>
</rootNode>

...如果我理解正确的话,你就必须两个命名空间添加到的XmlNamespaceManager ,以便进行查询单个节点...

... If I understand correctly, you would have to add both namespaces to the XmlNamespaceManager, in order to make a query for a single node...

Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
nsmgr.AddNamespace("cde", "http://someplace.org")
nsmgr.AddNamespace("feg", "http://otherplace.net")
Dim desiredNode As XmlNode = doc.SelectSingleNode("//feg:nodeName", nsmgr)

...为什么,在这种情况下,我需要(概念)命名空间管理器?

... Why, in this case, do I need (conceptually) a namespace manager?

的**被编辑成下面的注释**

**REDACTED into comments below**

修改补充: 我的修订和完善的问题是基于XmlNamespaceManager的的东西我认为是大多数情况下和使用命名空间管理器来指定URI的preFIX映射明显的冗余:

Edit Added: My revised and refined question is based upon the apparent redundancy of the XmlNamespaceManager in what I believe to be the majority of cases and the use of the namespace manager to specify a mapping of prefix to URI:

在命名空间preFIX(CDE)的命名空间URI(http://someplace.org)的直接映射显式地源文件中所述:

When the direct mapping of the namespace prefix ("cde") to the namespace URI ("http://someplace.org") is explicitly stated in the source document:

...<rootNode xmlns:cde="http://someplace.org"...

什么是概念上需要一个程序员做一个查询之前重新创建映射?

what is the conceptual need for a programmer to recreate that mapping before making a query?

推荐答案

其基本点(如指出<一href="http://stackoverflow.com/questions/7178111/why-is-xmlnamespacemanager-necessary/7178260#7178260">Kev,上述),是该命名空间URI是命名空间的重要组成部分,而不是命名空间preFIX,在preFIX是任意方便

The basic point (as pointed out by Kev, above), is that the namespace URI is the important part of the namespace, rather than the namespace prefix, the prefix is an "arbitrary convenience"

至于为什么你需要一个命名空间的管理,而不是那里是一些魔法的使用文档的作品出来,我能想到的两个原因。

As for why you need a namespace manager, rather than there being some magic that works it out using the document, I can think of two reasons.

如果它被允许只添加名称空间声明的documentElement,在您的例子,它确实是琐碎的selectSingleNode只使用任何定义。

If it were permitted to only add namespace declarations to the documentElement, as in your examples, it would indeed be trivial for selectSingleNode to just use whatever is defined.

不过,你可以在任何元素在文档中定义命名空间prefixes和命名空间prefixes不是唯一绑定到文档中的任何给定的命名空间。请看下面的例子

However, you can define namespace prefixes on any element in a document, and namespace prefixes are not uniquely bound to any given namespace in a document. Consider the following example

<w xmlns:a="mynamespace">
  <a:x>
    <y xmlns:a="myOthernamespace">
      <z xmlns="mynamespace">
      <b:z xmlns:b="mynamespace">
      <z xmlns="myOthernamespace">
      <b:z xmlns:b="myOthernamespace">
    </y>
  </a:x>
</w>

在这个例子中,你会想要 //ž //一:Z // B:Z 返回?怎么样,没有某种形式的外部命名空间管理器,你会EX preSS是什么?

In this example, what would you want //z, //a:z and //b:z to return? How, without some kind of external namespace manager, would you express that?

它可以让你重复使用相同的XPath EX pression任何类似文件,而无需了解命名空间prefixes使用的东西。

It allows you to reuse the same XPath expression for any equivalent document, without needing to know anything about the namespace prefixes in use.

myXPathExpression = "//z:y"
doc1.selectSingleNode(myXPathExpression);
doc2.selectSingleNode(myXPathExpression);

doc1的:

doc1:

<x>
  <z:y xmlns:z="mynamespace" />
</x>

DOC2:

doc2:

<x xmlns"mynamespace">
  <y>
</x>

为了实现没有命名空间管理后者的目标,你就必须检查每个文档,建立一个自定义的XPath EX pression为每一个。

In order to achieve this latter goal without a namespace manager, you would have to inspect each document, building a custom XPath expression for each one.

这篇关于为什么XmlNamespaceManager的必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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