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

查看:96
本文介绍了带有名称空间前缀的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天全站免登陆