来自带有命名空间前缀的 xml 的 xpath 表达式 [英] xpath expression from xml with namespace prefix

查看:20
本文介绍了来自带有命名空间前缀的 xml 的 xpath 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当设置 xml 路径命名空间前缀时,我无法使以下 xpath 表达式工作.

I could not get the following xpath expression to work when the xml path namespace prefix set.

/bk:BookStore/bk:Books/bk:Book[text()='Time Machine']

XML 是:

<BookStore xmlns:bk="http://www.bookstore.com/book#">
  <bk:Books>
    <bk:Book id="1">Time Machine></bk:Book>
  </bk:Books>
</bk:BookStore>

推荐答案

如果没有关于宿主语言的更多信息(您尝试在其中评估 XPath 表达式)无法提供一个有用的建议.

Without more information about the host language (in which you attempt to evaluate XPath expressions) it is not possible to provide an useful recommendation.

通常,需要向命名空间管理器注册"一个命名空间,这也将一个前缀关联到已注册的命名空间.然后,使用这个 NamespaceManager 对象作为 XPath 评估方法的参数,可以指定一个 XPath 表达式作为该方法的参数,其中包含以该特定前缀为前缀的名称.

Generally, one needs to "register" a namespace with a namespace manager and this also associates a prefix to the registered namespace. Then, using this NamespaceManager object as an argument to the XPath-evaluation method, one can specify as argument to this method an XPath expression that contains names prefixed by that particular prefix.

解决方法:

/*/*[name()='bk:Books']/*[name()='bk:Book' and text()='Time Machine']

这篇关于来自带有命名空间前缀的 xml 的 xpath 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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