将基于同级值的节点与XPath匹配


89

具有这样的XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<records type="array">
  <record>
    <name>svn</name>
    <record-type>A</record-type>
    <ttl type="integer">86400</ttl>
    <zone-id type="integer">69075</zone-id>
    <aux type="integer">0</aux>
    <id type="integer">xxx</id>
    <active>Y</active>
    <data>xxx.xxx.xxx.xxx</data>
  </record>
  <record>
    <name>domain.tld.</name>
    <record-type>NS</record-type>
    <ttl type="integer">86400</ttl>
    <zone-id type="integer">xxx</zone-id>
    <aux type="integer">0</aux>
    <id type="integer">xxx</id>
    <active>Y</active>
    <data>domain.tld.</data>
  </record>
  <record>
    <name>blog</name>
    <record-type>A</record-type>
    <ttl type="integer">86400</ttl>
    <zone-id type="integer">xxx</zone-id>
    <aux type="integer">0</aux>
    <id type="integer">xxx</id>
    <active>Y</active>
    <data>xxx.xxx.xxx.xxx</data>
  </record>
</records>

如何将所有/records/record/name同胞/records/record/record-type与值匹配A

Answers:



58

令人惊讶的是,迄今为止,这个老问题的答案都没有提供最简单的XPath解决方案。

这个简单的XPath

/records/record[record-type = "A"]/name

选择

<name>svn</name>
<name>blog</name>

按照要求。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.