XPath 和 XML:多个命名空间 [英] XPath and XML: Multiple namespaces

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

问题描述

所以我有一个看起来像这样的文档

So I have a document that looks like

<a xmlns="uri1" xmlns:pre2="uri2">
 <b xmlns:pre3="uri3">
   <pre3:c>
     <stuff></stuff>
     <goes></goes>
     <here></here>
   </pre3:c>
   <pre3:d xmlns="uri4">
     <under></under>
     <the></the>
     <tree></tree>
   </pre3:d>
  </b>
</a>

我想要一个可以让我的 xpath 表达式.

I want an xpath expression that will get me <under>.

它的 namespaceURI 为 uri4.

This has a namespaceURI of uri4.

现在我的表情是这样的:

Right now my expression looks like:

//ns:a/ns:b/pre3:d/pre4:under

我让命名空间管理器为默认命名空间(在本例中为 uri1)添加了ns",并分别为 uri2、uri3 和 uri4 定义了 pre2、pre3 和 pre4.

I have the namespace manager add 'ns' for the default namespace (uri1 in this case) and I have it defined with pre2, pre3, and pre4 for uri2, uri3, and uri4 respectively.

我收到错误表达式必须评估为节点集."

我知道该节点存在.我知道直到我的 xpath 中的 pre4:under 之前的所有内容都可以正常工作,因为我在文档的其余部分使用它没有任何问题.导致错误的是额外的 pre4:under,我不知道为什么.

I know that the node exists. I know that everything up until the pre4:under in my xpath works fine as I use it in the rest of the document with no issues. It's the additional pre4:under that causes the error, and I'm not sure why.

有什么想法吗?

谢谢.

分辨率:

感谢大家坚持认为它是正确的 - 确实如此.但是......在我的代码中,我将pre4"作为64"(一个变量),它不喜欢一个整数作为前缀.将其更改为 "d" + myintvariable 有效.

Thank you all for your insistence that it's correct--it was. But... in my code I had "pre4" as "64" (a variable) and it didn't like an integer for a prefix. Changing it to "d" + myintvariable worked.

推荐答案

我的猜测是,您用来导航 XML 的实现可能存在错误.使用SketchPath,以下XPath成功导航到节点:

My guess is that there may be a bug with the implementation that you are using to navigate the XML. Using SketchPath, the following XPath navigated to the node successfully:

/def:a/def:b/pre3:d/def2:under

您能否尝试为 XPath 中的命名空间指定不同的前缀?否则,如果性能不是真正的问题,并且它是一个独特的节点,您可以尝试 //under

Could you try specifying different prefixes for the namespaces in the XPath? Otherwise, if performance isn't really an issue, and it's a unique node, you could just try //under

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

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