c# - 用于获取子元素计数的 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"/>
   </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/

相关文章:

c# - 参数类型 'string' 无法分配给参数类型 'System.Collection.Generic.KeyValuePair<string,object>'

c# - 从特定文件夹执行命令行

Java - Maven JAXB-2插件具有不同配置的多个方案不会生成类

c# - 缺少 XmlSchemaSet UWP

python - 如何用Python进行日期推导?

xpath - 我可以使用 xpath 获得数字的符号吗

RS Selenium : replacing xpath with variable in R

java - 如何通过 XPaths 将 XML 映射到 Java 对象?

c# - Servicestack 对象参数未通过 Swagger-UI 传递给服务

c# - 传递给函数的参数数量?