C# 获取子元素计数的XPath表达式

C# 获取子元素计数的XPath表达式,c#,xml,xpath,C#,Xml,Xpath,我有一个这样的XML <Root> <Mgr name="X"> <Emp name = "X1"/> <Emp name = "X2"/> <Emp name = "X3"/> </Mgr> <Mgr name="Y"> <Emp name = "Y1"/> <Emp name = "Y2"/>

我有一个这样的XML

<Root>
   <Mgr name="X">
       <Emp name = "X1"/>
       <Emp name = "X2"/>
       <Emp name = "X3"/>
   </Mgr>
   <Mgr name="Y">
       <Emp name = "Y1"/>
       <Emp name = "Y2"/>
   </Mgr>
</Root>

我可以使用此
计数(/Root/Mgr/Emp)
轻松获得员工总数,但我希望获得每个经理的计数。
类似这样的内容—“3,2”。

XPath 1.0 这个XPath 1.0表达式

concat(count(/Root/Mgr[1]/Emp), ',', count(/Root/Mgr[2]/Emp))
string-join(/root/Mgr/count(Emp), ',')
会回来的

3,2
3,2
根据要求,为您的XML

XPath2.0 这个XPath 2.0表达式

concat(count(/Root/Mgr[1]/Emp), ',', count(/Root/Mgr[2]/Emp))
string-join(/root/Mgr/count(Emp), ',')
会回来的

3,2
3,2

对于您的XML,按照要求(并且很好地概括了两个
Mgr
元素)。

它当然适用于您的XSLT-2.0解决方案,但是对于两个以上的
Mgr
节点,XPath-1.0解决方案会是什么样子呢?(我相信这会很感激)@zx485:要获得XPath 1.0中的XPath 2.0解决方案那样的通用性,需要调用XPath库的语言的帮助。我想是这样的。感谢您的肯定。@kjhughes-我得到错误:DOMXPath::evaluate():在联机xpath计算器中尝试字符串联接(/root/Mgr/count(Emp),“,”)时,表达式无效。@Fox:该错误意味着该xpath计算器不支持xpath 2.0。