XPath - 属性通配符不返回具有名为value的属性的元素 [英] XPath - attribute wildcard not returning element with attribute named value

查看:148
本文介绍了XPath - 属性通配符不返回具有名为value的属性的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用XPath(Java)根据以特定值开头的未知属性获取所有未知节点。出于某种原因,它不返回包含名为 value 的属性的节点。我还在 www.freeformatter.com/xpath-tester.html 进行了测试并得到了同样的结果结果。以下是我的内容:

I am trying to use XPath (Java) to get all unknown nodes based on unknown attributes starting with a specific value. For some reason, it is not returning a node that contains an attribute named value. I also tested at www.freeformatter.com/xpath-tester.html and got the same result. Here is what I have:

XML:

<div>
    <object data="/v1/assets/mp4Video-1" type="video/mp4">
        <param name="webmSource" value="/v1/assets/webmVideo-1" type="REF"/>
    </object>
</div>

XPath表达式:

//*[starts-with(@*, '/v1/assets/')]

结果 - 返回< object> ,但不返回 < param>

现在,如果我将XPath表达式更改为 // * [starts-with( @ *,'/ v1 / assets /')或者开头(@value,'/ v1 / assets /')] ,它会按预期返回。

Now, if I change the XPath expression to //*[starts-with(@*, '/v1/assets/') or starts-with(@value, '/v1/assets/')], it returns both as expected.

我想我的问题是,什么是属性导致XPath无法正确识别它作为属性,或者不返回属性包含我要查询的值时的元素?

I guess my question is, what is it about the value attribute that causes XPath to not properly recognize it as an attribute, or to not return the element when the value attribute contains the value I am querying for?

推荐答案

原始路径表达式的原因:

The reason why your original path expression:

//*[starts-with(@*, '/v1/assets/')]

不起作用与XPath 1.0中的函数如何处理更多节点有关汉预计。 starts-with()函数需要将单个节点作为其第一个参数,并将字符串(或计算为字符串的节点)作为其第二个参数。

does not work has to do with how functions in XPath 1.0 cope with more nodes than expected. The starts-with() function expects a single node as its first argument, and a string (or a node that evaluates to a string) as its second argument.

但是在上面的表达式中, starts-with()被赋予 set 属性节点, @ * ,作为其第一个参数。在这种情况下,此函数仅使用这些属性节点的 first 。集合中的所有其他节点都将被忽略。由于属性的顺序未在XML中定义,因此XPath引擎可以自由选择要在函数中使用的任何属性节点。但是您的特定XPath引擎(以及许多其他引擎)似乎始终按照它们出现的顺序使用第一个属性节点。

But in the expression above, starts-with() is handed a set of attribute nodes, @*, as its first argument. In this case, only the first of those attribute nodes is used by this function. All other nodes in the set are ignored. Since the order of attributes is not defined in XML, the XPath engine is free to choose any attribute node to be used in the function. But your specific XPath engine (and many others) appear to consistently use the first attribute node, in the order of their appearance.

为了说明这(并证明),将输入文件更改为

To illustrate this (and to prove it), change your input document to

<div>
    <object data="other" type="/v1/assets/mp4Video-1">
        <param name="/v1/assets/webmVideo-1" value="other" type="REF"/>
    </object>
</div>

如您所见,我更改了属性的顺序,以及包含<$ c $的属性c> / v1 / assets / 现在是对象元素的第二个属性,反之亦然 param 元素。使用此输入文档,原始XPath表达式将仅返回 param 元素。

as you can see, I have changed the order of attributes, and the attribute containing /v1/assets/ is now the second attribute of the object element, and vice versa for the param element. Using this input document, your original XPath expression will only return the param element.

同样,此行为不是不同的XPath引擎之间必然一致!使用XPath的其他实现可能会产生不同的结果。

Again, this behaviour is not necessarily consistent between different XPath engines! Using other implementations of XPath might yield different results.

执行所需操作的XPath表达式

The XPath expression that does what you need is

//*[@*[starts-with(., '/v1/assets/')]]

用简单的英语,它说


select文档中任何位置的元素,但只有在元素的所有属性节点中,还有一个属性,其值以/ v1 / assets /\".

select elements anywhere in the document, but only if, among all attribute nodes of an element, there is an attribute whose value starts with "/v1/assets/".

这篇关于XPath - 属性通配符不返回具有名为value的属性的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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