【问题标题】:Use of count in Xpath expressions在 Xpath 表达式中使用 count
【发布时间】:2012-12-10 10:39:22
【问题描述】:

如果我有这样的 XML:(只是做一个例子)

<restaurant>
    <tables>
            <table id=1 />
            <table id=2 />
            <table id=3 />
    </tables>

而且我还要数表的数量,正确的Xpath表达式是:

restaurant/tables/count(table)

count(restaurant/tables/table)

?

谢谢

【问题讨论】:

  • 两个都试试。看看返回了什么。看看你是否得到任何错误。自己测试既能更快又能教你一些东西。

标签: xpath


【解决方案1】:

查看this Microsoft XPath count() tutorial

count(/restaurant/tables/table)

将统计上述路径中的表格元素。

【讨论】:

    【解决方案2】:
    restaurant/tables/count(table)
    

    这个表达式只在 XPath 2.0 中是合法的。它生成一个数字序列,如果 XML 文档有多个 restorantrestorant/tables 元素,则该序列的长度可能大于 1。

    count(restaurant/tables/table)
    

    此表达式在 XPath 1.0 和 XPath 2.0 中都有效。它产生一个数字。

    因此,一般来说,这两个表达式是不同的,只有第二个在 XPath 1.0 中是合法的,而在 XPath 2.0 中,这些表达式可能会产生不同的结果,具体取决于评估它们的 XML 文档。

    但是在指定的 XML 文档上,这两个表达式(在 XPath 2.0 中计算时)产生相同的结果:

    3
    

    【讨论】:

      猜你喜欢
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多