xml 在 Xpath 中不同?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2812122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
distinct in Xpath?
提问by Antoine
I have this XML file, from which I'd like to count the number of users referenced in it. But they can appear in more than one category, and I'd like these duplicates not to be taken into account.
In the example below, the query should return 3 and not 4. Is there a way in XPath to do so? Users are not sorted at all.
我有这个 XML 文件,我想从中计算出其中引用的用户数量。但是它们可以出现在多个类别中,我希望不要考虑这些重复项。
在下面的示例中,查询应该返回 3 而不是 4。在 XPath 中有没有办法这样做?用户根本没有排序。
<list>
<group name='QA'>
<user name='name1'>name1@email</user>
<user name='name2'>name2@email</user>
</group>
<group name='DEV'>
<user name='name3'>name3@email</user>
<user name='name2'>name2@email</user>
</group>
</list>
采纳答案by Dimitre Novatchev
A pure XPath 1.0 -- one-liner:
纯 XPath 1.0 —— 单行:
Use:
使用:
count(/*/group/user[not(. = ../following-sibling::group/user)])
count(/*/group/user[not(. = ../following-sibling::group/user)])
回答by gingerbreadboy
using the functions namespace http://www.w3.org/2005/xpath-functionsyou can use
使用函数命名空间http://www.w3.org/2005/xpath-functions你可以使用
distinct-values(//list/group/user)
UPDATE:
更新:
At the top of your xsl/xslt file you should have a stylesheet element, map the url above to the prefix fnas below...
在您的 xsl/xslt 文件的顶部,您应该有一个样式表元素,将上面的 url 映射到如下前缀fn...
<xsl:stylesheet version="1.0"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
>
then you can use
然后你可以使用
select="fn:distinct-values(//list/group/user)"
this would assume you are doing this in templates and not in some xpathdocument object inwhich case you need to use a namespacemanager class.
这将假设您是在模板中执行此操作,而不是在某些 xpathdocument 对象中执行此操作,在这种情况下,您需要使用 namespacemanager 类。
links...
链接...
XSLT: Add namespace to root element
http://www.xqueryfunctions.com/xq/fn_distinct-values.html
http://www.xqueryfunctions.com/xq/fn_distinct-values.html
http://msdn.microsoft.com/en-us/library/d6730bwt(VS.80).aspx
http://msdn.microsoft.com/en-us/library/d6730bwt(VS.80).aspx
Otherwisetry Dimitre Novatchev's answer.
否则请尝试 Dimitre Novatchev 的回答。
回答by Raghavendra
I have a better answer
我有更好的答案
count(//user[not(. = following::user/.)])
回答by JSprang
Not sure if you could do it in XPath, but it could be done easily using System.Linq:
不确定您是否可以在 XPath 中完成,但可以使用 System.Linq 轻松完成:
string xml = "<list><group name='QA'><user name='name1'>name1@email</user><user name='name2'>name2@email</user></group><group name='DEV'><user name='name3'>name3@email</user><user name='name2'>name2@email</user></group></list>";
XElement xe = XElement.Parse(xml);
int distinctCount = xe.Elements().Elements().Select(n => n.Value).Distinct().Count();
In this example, distinctCount will equal 3.
在此示例中,distinctCount 将等于 3。
回答by Mitchel Sellers
You will need to use two functions like this.
您将需要使用两个这样的函数。
count(distinct-values(//list/group/user))
First get the distinctvalues, then countthem
首先获取distinct值,然后获取count它们
回答by zak zak
count(//user[not(./@name = preceding::user/@name)])
I think the best way is to try to draw your xml data on paper to see how you can solve it easily
我认为最好的方法是尝试在纸上绘制您的xml数据,看看如何轻松解决它

