[英]XPath results to empty string
Following is my XML file 以下是我的XML文件
<xyzevent xmlns="http://www.xyz.com/common/xyzevent/v1" xmlns:xsi="http://www.w3.org2001XMLSchema-instance">
<header>
----
</header>
<subscription xmlns="http://www.xyz.com/common/xyzevent/source/v1">
<sender></sender>
<receiver>
<clientsubscription>
<servicemap>nanna</servicemap>
</clientsubscription>
</receiver>
</subscription>
</xyzevent>
When I budila org.w3c.dom.Document from this XML and applying XPathExperssion with expression 当我从这个XML发布org.w3c.dom.Document并使用表达式应用XPathExperssion时
/xyzevent/subscription/receiver/clientsubscription/servicemap/text()
results empty string. 结果空字符串。 What can be the issue with the expression?
表达式的问题可能是什么?
Thank you 谢谢
That's because your XML document uses a namespace. 那是因为您的XML文档使用了命名空间。 XPath is really annoying with namespaces.
XPath真的很烦人的命名空间。 To confirm this, strip the two
xmlns=http://.../v1
from the document and run your XPath expression agains the unnamespaced, unverifiable XML file. 要确认这一点,
xmlns=http://.../v1
从文档中删除两个xmlns=http://.../v1
,然后再使用未命名的无法验证的XML文件运行XPath表达式。 It'll match. 它会匹配。
What's happening is that your XPath expression tries to select /xyzevent
, when your document contains {http://.../v1}:xyzevent
, which is not the same thing. 发生的事情是当你的文档包含
{http://.../v1}:xyzevent
时,你的XPath表达式会尝试选择/xyzevent
{http://.../v1}:xyzevent
,这不是一回事。
There are various ways around this problem. 有很多方法可以解决这个问题。 The proper way is to set up a
NamespaceContext
so you can use the prefix:localName
notation in your XPath expression and have the prefixes be resolved to the correct URI. 正确的方法是设置
NamespaceContext
以便在XPath表达式中使用prefix:localName
表示法,并将前缀解析为正确的URI。 There's a short blurb about this in the xerces docs and some more elsewhere on StackOverflow . 在xerces文档中有一个简短的模糊 , 在StackOverflow的其他地方还有一些。 There's an extensive description at ibm.com .
ibm.com上有广泛的描述。
Your NamespaceContext
will contain two (or more) mappings: 您的
NamespaceContext
将包含两个(或更多)映射:
{
event => http://www.xyz.com/common/xyzevent/v1
source => http://www.xyz.com/common/xyzevent/source/v1
}
Your XPath expression can then become /event:xyzevent/source:subscription/source:receiver/.../text()
. 然后,您的XPath表达式可以成为
/event:xyzevent/source:subscription/source:receiver/.../text()
。
As a nasty workaround, you can rewrite your xpath expression to select using the local-name()
function: 作为一种讨厌的解决方法,您可以使用
local-name()
函数重写xpath表达式以进行选择:
/*[local-name()='xyzevent']/*[local-name()='subscription'/ ...
In this case, the expression matches any element whose local name is xyzevent
, regardless of namespace URI. 在这种情况下,表达式匹配任何本地名称为
xyzevent
元素,而不管名称空间URI如何。
Your XML has default namespace: xmlns="http://www.xyz.com/common/xyzevent/v1"
, therefore you need to define it in your XML/XPath engine. 您的XML具有默认命名空间:
xmlns="http://www.xyz.com/common/xyzevent/v1"
,因此您需要在XML / XPath引擎中定义它。
Or use this XPath: 或者使用这个XPath:
/*[local-name() = 'xyzevent']
/*[local-name() = 'subscription']
/*[local-name() = 'receiver']
/*[local-name() = 'clientsubscription']
/*[local-name() = 'servicemap']
/text()
xyzevent
是你的根元素,所以你只需要使用"/subscription/receiver/clientsubscription/servicemap/text()"
。
I evaluated your expression in the following link: 我在以下链接中评估了您的表达式:
http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm
And it seems, it selects "nanna". 看起来,它选择了“nanna”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.