根据相同级别的值选择XPath中的值

时间:2014-11-16 22:59:27

标签: xml xpath xpath-1.0

我有这个部分XML

 <Events>
   <Properties>
     <Property Descriptor="1">VALUE1</Property>
     <Property Descriptor="2">FOO</Property>
     <Property Descriptor="3">BAR</Property>
     </Properties>
   <Properties>
     <Property Descriptor="1">VALUE2</Property>
     <Property Descriptor="2">NO</Property>
     <Property Descriptor="3">NOTHINGHERE</Property>
   </Properties>
 </Events>

当属性描述符=“2”=“FOO”时,我可以查询第一次出现的属性描述符=“3”吗?此处的结果应为BAR

我尝试了//Properties/Property[@Descriptor="3"][..//Property[@Descriptor="2"] = "FOO"],但它现在正在运作。

1 个答案:

答案 0 :(得分:3)

  

当Property Descriptor =“2”=“FOO”时,我可以查询Property Descriptor =“3”的第一次出现吗?

/Events/Properties[Property[@Descriptor = '2'] = 'FOO']/Property[@Descriptor = '3'][1]

更详细地说:

/Events/Properties                    Look for a Properties element
[Property                             but only if it has a Property element
[@Descriptor = '2'] = 'FOO']          but only if its Descriptor attribute equals 2 and
                                      its textual content is "FOO"
/Property[@Descriptor = '3']          for that Properties element look for a child element
                                      called Property where the Descriptor attribute equals 3
[1]                                   thereof return the first occurrence