XPath中的前pression声明命名空间 [英] Declare namespaces within XPath expression

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

问题描述

我的应用程序需要计算XPath EX $ P $对一些XML数据pssion。防爆pression由用户在运行时提供。所以,我不能创建的XmlNamespaceManager 传递给 XPathEvaluate ,因为我不知道在编译prefixes和命名空间时间。

有指定范围内的XPath EX pression命名空间的声明的可能性?


答案评论:

  1. 在XML数据有一个默认的命名空间,但也可以是嵌套的任何命名空间的元素。用户知道自己的作品与数据的命名空间。
  2. 用户提供的XPath EX pression是对许多XML文档进行评估,并且每个文件可以有自己的prefixes为同一命名空间。
解决方案

我不知道任何方式来定义一个命名空间preFIX在XPath EX pression。

但你的可以的写的XPath EX pression通过使用本地名称()和namespace-URI是不可知的namespace- prefixes()函数在适当的。

的,如果你知道XML名称空间提前,可以在XmlNamespaceManager的为他们注册一个任意preFIX并告诉你的用户使用了preFIX中的XPath前pression。如果XML文档本身注册了一个不同的preFIX或没有preFIX都不要紧。路径解析是基于单独的命名空间,而不是在preFIX。

另一种选择是扫描在运行时的文件(使用XmlReader的低资源开销,如果你还没有装好了的话),然后在XmlNamespaceManager的文件中添加使用的映射。我不知道,如果你可以从XmlDocument的获得命名空间和prefixes,但我看不出有什么直接的方法来做到这一点。这很容易用的XmlReader虽然,因为它暴露的namespaceURI和preFIX成员的每个节点。

My application needs to evaluate XPath expression against some XML data. Expression is provided by user at runtime. So, I cannot create XmlNamespaceManager to pass to XPathEvaluate because I don't know prefixes and namespaces at compile time.

Is there any possibility to specify namespaces declaration within xpath expression?


Answers to comments:

  1. XML data has one default namespace but there can be nested elements with any namespaces. User knows namespaces of the data he works with.
  2. User-provided xpath expression is to be evaluated against many XML documents, and every document can have its own prefixes for the same namespaces.

解决方案

I don't know any way to define a namespace prefix in an XPath expression.

But you can write the XPath expression to be agnostic of namespace-prefixes by using local-name() and namespace-uri() functions where appropriate.

Or if you know the XML-namespaces in advance, you can register an arbitrary prefix for them in the XmlNamespaceManager and tell your user to use that prefix in the XPath expression. It doesn't matter if the XML document itself registers a different prefix or no prefix at all. Path resolution is based on the namespace alone, not on the prefix.

Another option would be to scan the document at runtime (use XmlReader for low resource overhead if you haven't loaded it already) and then add the used mappings in the document in the XmlNamespaceManager. I'm not sure if you can get the namespaces and prefixes from XmlDocument, but I see no direct method to do it. It's easy with XmlReader though, since it exposes NamespaceURI and Prefix members for each node.

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

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