[英]Xpath count() within a relative path
I am testing some xpath expressions using http://www.freeformatter.com/xpath-tester.html ( xpath 1.0 ) 我正在使用http://www.freeformatter.com/xpath-tester.html(xpath 1.0 )测试一些xpath表达式
I have this xml code 我有这个xml代码
<urls>
<url uri="http://example.com/pingtest.html">
<elements>
<element title="header" xpath="My header"></element>
</elements>
</url>
<url uri="http://example.com/othertest.html">
<elements>
<element title="header3" xpath="Third header"/>
<element title="header2" xpath="Second header"/>
</elements>
</url>
</urls>
Usually when I query xpath with //element/@title I actually get 通常当我用// element / @ title查询xpath时,我实际上得到了
Attribute='title="header"' Attribute='title="header3"' Attribute='title="header2"' Attribute ='title =“ header”'Attribute ='title =“ header3”'Attribute ='title =“ header2”'
What I'm looking for is a way to get a count of elements by url. 我正在寻找的是一种通过url获取元素计数的方法。 It could be something like this. 可能是这样的。 When I query: 当我查询时:
//urls/url/elements/count(element)
I should get (EDIT: as @Ian Roberts said, this query is valid in xpath 2.0): 我应该得到(编辑:正如@Ian Roberts所说,此查询在xpath 2.0中有效):
Double='1.0' Double='2.0' Double ='1.0'Double ='2.0'
Is there a way to do this in a single xpath expression? 有没有办法在单个xpath表达式中执行此操作?
Some failing examples : 一些失败的例子 :
This returns the count of all element 3 (is not what I'm looking for) 这将返回所有元素3的计数(不是我想要的)
count(//urls/url/elements/element)
This returns the count of all elements list 2 (neither what I'm looking for) 这将返回所有元素列表2的计数(都不是我要找的)
count(//urls/url/elements)
I need this because I need to know how many elements does the xml have for each url, so I can use this count afterwards 我需要这个,因为我需要知道xml每个网址有多少个元素,因此以后可以使用此计数
You can use //urls/url/elements/count(element)
with any XPath 2.0 or later or XQuery 1.0 or later implementation to get a sequence of integer values with the counts. 您可以将//urls/url/elements/count(element)
与任何XPath 2.0或更高版本或XQuery 1.0或更高版本的实现一起使用,以获取带有计数的整数值序列。 If you only have XPath 1.0 available then you need to select //urls/url
first in your host language and then you can for each returned item select count(elements/element)
. 如果只有XPath 1.0可用,则需要先使用宿主语言选择//urls/url
,然后可以为每个返回的项目选择count(elements/element)
。
I believe that what you're looking for can't be done... 我相信您要找的东西无法完成...
//element/@title
searches the XML for paths containing an element with a title attribute; //element/@title
在XML中搜索包含带有title属性的元素的路径; then returns an array of those attributes with their values. 然后返回这些属性及其值的数组。
//urls/url
returns an array of xml elements for paths with element urls containing an element url. //urls/url
为包含元素url的元素url返回路径的xml元素数组。 count(/elements/element)
then performs the count function on the number of element
elements under the elements
element within each of the above paths. count(/elements/element)
对上述每个路径中的elements
元素下的element
elements数量执行count函数。
Trying to put these into a single xpath would give us //urls/url/count(/elements/element)
. 尝试将它们放入单个xpath中将给我们//urls/url/count(/elements/element)
。 The issue here is the xpath is being used for two purposes: 这里的问题是xpath被用于两个目的:
However an xpath can only be used for one function or the other (eg depending on whether it were called from a match
or a select
statement. 但是,xpath只能用于一个函数或另一个函数(例如,取决于是从match
还是select
语句调用它)。
As such (per @MartinHonnen's answer), you'd need to do a match first, then iterat through the results performing the select. 这样(根据@MartinHonnen的回答),您需要先进行匹配,然后遍历结果以执行选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.