xml - xpath 查询在使用大于和小于时返回错误值?

标签 xml xpath

此查询返回的值小于 1000。它应该只返回 1000 到 1100 之间的值。这是为什么?

//results/Building[ 1 = 1 and (( Vacancy/sqft > 1000 ) and ( Vacancy/sqft < 1100 ) ) ]

查询将返回以下建筑物,其空置面积小于 1000 平方英尺且大于 1100 平方英尺:

<Building>
  <Vacancy><sqft>900</sqft></Vacancy>
  <Vacancy><sqft>1000</sqft></Vacancy>
  <Vacancy><sqft>2000</sqft></Vacancy>
  <Vacancy><sqft>500</sqft></Vacancy>
</Building>

为什么包含在结果中?

示例数据:

<results>
  <Building><!--Shouldn't be selected.--></Building>

  <Building><!--Should be selected-->
    <Vacancy><sqft>1050</sqft></Vacancy>
  </Building>

  <Building><!--Should be selected-->
    <Vacancy><sqft>1025</sqft></Vacancy>
    <Vacancy><sqft>1075</sqft></Vacancy>
  </Building>

  <Building><!--Shouldn't be selected-->
    <Vacancy><sqft>10</sqft></Vacancy>
    <Vacancy><sqft>50</sqft></Vacancy>
  </Building>

  <Building><!--Should be selected.-->
    <Vacancy><sqft>1050</sqft></Vacancy>
    <Vacancy><sqft>2000</sqft></Vacancy>
  </Building>

  <Building><!--Should be selected.-->
    <Vacancy><sqft>900</sqft></Vacancy>
    <Vacancy><sqft>1040</sqft></Vacancy>
  </Building>

  <Building><!--Shouldn't be selected-->
    <Vacancy><sqft>10500</sqft></Vacancy>
  </Building>

  <Building><!--Shouldn't be selected-->
    <Vacancy><sqft>900</sqft></Vacancy>
    <Vacancy><sqft>1000</sqft></Vacancy>
    <Vacancy><sqft>2000</sqft></Vacancy>
    <Vacancy><sqft>500</sqft></Vacancy>
  </Building>

</results>

谢谢。

最佳答案

示例建筑有一个 2000 平方英尺的空置子建筑,所以 Vacancy/sqft > 1000成功。它有一个 child ,面积为 1000 平方英尺(以及 900 和 500),所以 Vacancy/sqft < 1100成功。因此 xpath 选择建筑物。

比较表达式(例如 Vacancy/sqft <= 1000 )隐式限定为“there exists ”——如“存在一个 Vacancy 子代,其 sqft 子代的值 > 1000”——因为 Vacancy/sqftset of nodes ,而不是单个节点。此外,每个比较都有自己的条件,因此 Vacancy/sqft > 1000 中的 sqft不需要与 Vacancy/sqft < 1100 中的相同平方英尺.注意 //results/Buildings是一个节点集;谓词 [...]分别应用于集合中的每个项目,这就是为什么限定符没有问题的原因。将您的原始 xpath 翻译成英文,我们得到:

Select the buildings (in the results) such that 1=1 and there exists a vacancy square footage > 1000 and there exists a vacancy square footage < 1100.

让我们采用所需查询的英文语句,使其更接近于逻辑语句,得出以下之一:

Select the buildings (in the results) such that there exists a vacancy with square footage such that it's > 1000 and it's < 1100

Select the buildings (in the results) such that there exists a vacancy such that the square footage > 1000 and the square footage < 1100

前者导致jasso的解决方案,后者导致:

//results/Building[ Vacancy[1000 < sqft and sqft < 1100] ]

原解

(注意:这回答了原始问题,当时不清楚 OP 想要什么。该技术可能对其他有类似问题但要求不同的人有用,所以我将其保留。)

尝试条件的逻辑双重否定:

//results/Building[ Vacancy and not (Vacancy/sqft <= 1000 or Vacancy/sqft >= 1100) ]

此谓词包括对 Vacancy 子项的测试,以过滤掉其他情况下基本成立的情况,即没有空缺的建筑物。此解决方案的英文等效内容是:

Select buildings (in the results) such that the building has a vacancy and it's not the case that there exists a vacancy square footage <= 1000 or there exists a vacancy square footage >= 1100

简而言之:

Select all buildings with vacancies where no vacancy has <= 1000 square feet or >= 1100 square feet.

简而言之:

Select all buildings with vacancies where all vacancies are between 1000 and 1100 square feet.

关于xml - xpath 查询在使用大于和小于时返回错误值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4028633/

相关文章:

java - 为背景创建状态列表可绘制 XML - Android

java - 将子元素添加到现有元素并将父节点的内容放入新的子节点中

selenium - xpath - 选择包含 CLASS 名称中的文本的元素

java - 通过元素xpath获取值

selenium - 如果两个链接中包含相同的文本,如何选择绝对链接?

java - 使用 Jackson 将 POJO 序列化为 xml 时删除 xml 包装器

XML 解析 + Windows Phone 7

java - JAXB 将 boolean 值编码为复杂类型

vb.net - xquery 上限函数的奇怪结果

python - 如何使用 scrapy 抓取应用程序的 google play 评论?