我有这样一个 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>
我可以使用此count(/Root/Mgr/Emp)
轻松获得员工总数,但我希望获得每个经理的数量。
像这样 - '3,2'。
最佳答案
XPath 1.0
这个 XPath 1.0 表达式,
concat(count(/Root/Mgr[1]/Emp), ',', count(/Root/Mgr[2]/Emp))
会回来
3,2
根据要求为您的 XML。
XPath 2.0
这个 XPath 2.0 表达式,
string-join(/root/Mgr/count(Emp), ',')
会回来
3,2
根据要求为您的 XML(并且很好地概括为超出两个 Mgr
元素)。
关于c# - 用于获取子元素计数的 XPath 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40941906/