[英]XPath filter by attribute and return distinct values
From the following I need to return a distinct list of values with the Category
attribute.从下面我需要返回具有
Category
属性的不同值列表。
<root>
<classifications>
<classification name="Category">Category One</classification>
<classification name="Sub-Category">Sub-Category One</classification>
</classifications>
<classifications>
<classification name="Category">Category Two</classification>
<classification name="Sub-Category">Sub-Category One</classification>
</classifications>
<classifications>
<classification name="Category">Category One</classification>
<classification name="Sub-Category">Sub-Category Two</classification>
</classifications>
<classifications>
<classification name="Category">Category Three</classification>
<classification name="Sub-Category">Sub-Category One</classification>
</classifications>
<classifications>
<classification name="Category">Category One</classification>
<classification name="Sub-Category">Sub-Category Two</classification>
</classifications>
<classifications>
<classification name="Category">Category Two</classification>
<classification name="Sub-Category">Sub-Category One</classification>
</classifications>
</root>
Using classifications/classification[@name="Category"]
I get:使用
classifications/classification[@name="Category"]
我得到:
Category One
Category Two
Category One
Category Three
Category One
Category Two
What I need is:我需要的是:
Category One
Category Two
Category Three
This XPath will select distinct elements:此 XPath 将选择不同的元素:
//classification[@name = "Category" and not(preceding::classification = .)]
or more specific:或更具体的:
//classification[@name = "Category"
and not(preceding::classification[@name = "Category"] = .)
]
For those able to use XPath 2.0 and above:对于那些能够使用 XPath 2.0 及更高版本的人:
An alternative approach that may be more flexible, and appears to be more efficient (at least in Saxon 9.8.0) is:另一种可能更灵活且似乎更有效(至少在 Saxon 9.8.0 中)的替代方法是:
distinct-values(//classification[@name = 'Category'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.