请检查Xpath校正

时间:2013-02-06 14:43:10

标签: xml xslt xpath

鉴于XML:

  <?xml version="1.0" encoding="UTF-8"?> 
     <sensor-system>
        <velocity>120.00</velocity> <!-- km/h --> 
        <temperature location="inside">24.6</temperature> 
         <temperature location="outside">-12.5</temperature>

     <seats>

     <seat location="front">
          <id>1</id> 
          <temperature>32.5</temperature> 
          <heating-is-on/>

      </seat> 
    <seat location="back">

      <id>2</id>
      <temperature>23.5</temperature>
   </seat>

</seats>
  </sensor-system>

必需的XML路径:

给出绝对XPATH,识别温度大于或等于20.0且小于或等于30.0摄氏度的所有前排座椅。

我的解决方案请检查:

这是否正确:

/sensor-system/seats[temperature>=20 | temperature <=30]/seat[@location="front"]

谢谢。

2 个答案:

答案 0 :(得分:3)

你的xpath不太正确,因为当你应该检查座位<时,期望温度元素成为席位元素的子元素/ strong>元素。

编辑:另外要指出的是,在原始xpath中,您正在使用&#39; |&#39; operator,它是union运算符,它将两个节点集作为参数。所以它不会在你的例子中起作用,因为你的论据实际上是条件&#39;。

试试这个

/sensor-system/seats/seat[temperature >= 20][temperature <= 30][@location = "front"]

注意,如果您在XSLT中使用它,则可能必须转义&lt;这里

/sensor-system/seats/seat[temperature >= 20][temperature &lt;= 30][@location = "front"]

虽然您可能更喜欢这样做

/sensor-system/seats/seat[temperature >= 20][not(temperature > 30)][@location = "front"]

答案 1 :(得分:-1)

尝试这样做:

/sensor-system/seats/seat[@location="front"]/temperature >=20 and /sensor-system/seats/seat[@location="front"]/temperature <= 30

true[@location="back"]false[@location="front"]