下面,我需要返回一个带有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>
使用classifications/classification[@name="Category"]
我得到:
Category One
Category Two
Category One
Category Three
Category One
Category Two
我需要的是:
Category One
Category Two
Category Three
答案 0 :(得分:1)
此XPath将选择不同的元素:
//classification[@name = "Category" and not(preceding::classification = .)]
或更具体:
//classification[@name = "Category"
and not(preceding::classification[@name = "Category"] = .)
]
答案 1 :(得分:0)
对于那些能够使用XPath 2.0及更高版本的人:
另一种可能更灵活,效率更高的替代方法(至少在Saxon 9.8.0中)是:
distinct-values(//classification[@name = 'Category'])