XML命名空间和XPath [英] XML namespaces and XPath

查看:149
本文介绍了XML命名空间和XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有加载XML文档,并根据XPath的输出节点的应用程序

I have an application that has to load XML document and output nodes depending on XPath.

假设我开始与像这样的文件:

Suppose I start with a document like this:

<aaa>
  ...[many nodes here]...
  <bbb>text</bbb>
  ...[many nodes here]...
  <bbb>text</bbb>
  ...[many nodes here]...
</aaa>

使用XPath的 // BBB

到目前为止,一切是很好的。

So far everything is nice.

和选择 doc.SelectNodes(// BBB ); 返回所需的节点列表

And selection doc.SelectNodes("//bbb"); returns the list of required nodes.

然后有人上传一个节点的文件如< myfancynamespace:富/方式> ,并在根标签额外的空间,一切都休息

Then someone uploads a document with one node like <myfancynamespace:foo/> and extra namespace in the root tag, and everything breaks.

为什么呢? // BBB 没有给出一个关于 myfancynamespace 该死的,理论上它甚至应该是不错的与 // myfancynamespace:富,因为没有歧义,但表达式返回0的结果,这就是它

Why? //bbb does not give a damn about myfancynamespace, theoretically it should even be good with //myfancynamespace:foo, as there is no ambiguity, but the expression returns 0 results and that's it.

对此有一个解决方法。行为?

Is there a workaround for this behavior?

我有对文档命名空间经理,我把它传递给XPath查询。但是,命名空间和前缀是未知的给我,所以查询之前,我不能添加。

I do have a namespace manager for the document, and I am passing it to the Xpath query. But the namespaces and the prefixes are unknown to me, so I can't add them before the query.

我必须预先解析文档,以填补空间经理之前,我做任何选择?为什么地球上的这种行为,它只是没有任何意义

Do I have to pre-parse the document to fill the namespace manager before I do any selections? Why on earth such behavior, it just doesn't make sense.

编辑:

我使用:
的XmlDocument 的XmlNamespaceManager

EDIT2:

XmlDocument doc = new XmlDocument();
doc.XmlResolver = null;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
//I wish I could:
//nsmgr.AddNamespace("magic", "http://magicnamespaceuri/
//...
doc.LoadXML(usersuppliedxml);
XmlNodeList nodes = doc.SelectNodes(usersuppliedxpath, nsmgr);//usersuppliedxpath -> "//bbb"

//nodes.Count should be > 0, but with namespaced document they are 0

EDIT3:
发现的一篇文章它描述了问题的实际情况,一个解决办法,但不是很漂亮的解决方法:的 http://codeclimber.net.nz/archive/2008/01/09/How-to-query-a-XPath-doc-that-has-a- Default.aspx的

几乎似乎剥离的xmlns是去...

Almost seems that stripping the xmlns is the way to go...

推荐答案

您错过XML命名空间的整点。

You're missing the whole point of XML namespaces.

但如果你真的需要在文档上执行的XPath将使用一个未知的命名空间,你真的不关心它,你将需要剥离出来,并重新加载文档。的XPath不会在命名空间中无关的工作方式,除非你想使用本地名称()功能在你选择的每一个点。

But if you really need to perform XPath on documents that will use an unknown namespace, and you really don't care about it, you will need to strip it out and reload the document. XPath will not work in a namespace-agnostic way, unless you want to use the local-name() function at every point in your selectors.

private XmlDocument StripNamespace(XmlDocument doc)
{
    if (doc.DocumentElement.NamespaceURI.Length > 0)
    {
        doc.DocumentElement.SetAttribute("xmlns", "");
        // must serialize and reload for this to take effect
        XmlDocument newDoc = new XmlDocument();
        newDoc.LoadXml(doc.OuterXml);
        return newDoc;
    }
    else
    {
        return doc;
    }
}

这篇关于XML命名空间和XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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