xml XPath基于复杂过滤器计算子节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6270859/
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
XPath to count the child nodes based on complex filter
提问by Vini
I have an XML in the following format:
我有以下格式的 XML:
<ComRequest>
<root lineId="1" creator="jumnix">
<component lineId="101">
<compLine lineId="1001">1</compLine>
<compLine lineId="1002">2</compLine>
<compLine lineId="1003">3</compLine>
<compLine lineId="1004">4</compLine>
<compLine lineId="1005">5</compLine>
<compLine lineId="1006">6</compLine>
<compLine lineId="1007">7</compLine>
<compLine lineId="1008">8</compLine>
<compLine lineId="1009">9</compLine>
<compLine lineId="1010">10</compLine>
<compLine lineId="1011">11</compLine>
</component>
<component lineId="102">
<compLine lineId="1012">12</compLine>
<compLine lineId="1013">13</compLine>
<compLine lineId="1014">14</compLine>
<compLine lineId="1015">15</compLine>
<compLine lineId="1016">16</compLine>
<compLine lineId="1017">17</compLine>
<compLine lineId="1018">18</compLine>
<compLine lineId="1019">19</compLine>
<compLine lineId="1020">20</compLine>
<compLine lineId="1021">21</compLine>
<compLine lineId="1022">22</compLine>
</component>
</root>
</ComRequest>
I have a requirement to get the count of the 'component' nodes that have more than 10 'compLine' elements. Till now I have the following XPath query -
我需要获取具有 10 个以上“compLine”元素的“组件”节点的计数。直到现在我有以下 XPath 查询 -
count(//*[local-name()='ComRequest']/*[local-name()='root']/*[local-name()='component']/*[local-name()='compLine' and count(self) gt 10])
But this does not work (gives a '0' result). Any help in getting this resolved is appreciated.
但这不起作用(给出“0”结果)。对解决此问题的任何帮助表示赞赏。
回答by Bala R
How about count(//ComRequest/root/component[count(compLine)>10])?
怎么样count(//ComRequest/root/component[count(compLine)>10])?
回答by Emiliano Poggi
@Bala-R (+1) is correctly evaluated using a compliant XSLT 1.0 processor (Saxon):
@Bala-R (+1) 使用兼容的 XSLT 1.0 处理器(Saxon)正确评估:
count(//ComRequest/root/component[count(compLine)>10])
or, either
或者,要么
count(/*/*/*[count(compLine)>10])
Otherwise something is going bad in your tests, your context (different from the one provided in the question) or your xpath evaluator.
否则,您的测试、您的上下文(与问题中提供的上下文不同)或您的 xpath 评估器会出现问题。

