...">

Matching node based on brother value with XPath

Having an XML document as follows:

<?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> 

How to match all the names / records / record / having a type with a value of "A" as a kinship / record / record / record?

+68
xml xpath
May 26 '09 at 19:10
source share
4 answers

Found:

 /records/record/name[../record-type/text() = "A"] 
+94
May 26 '09 at 19:29
source share

You can also filter the parent element with your child elements:

/records/record[record-type[text()='A']]/name

+39
Jul 19 '12 at 12:16
source share

Surprisingly, none of the answers to this old question provide the simplest XPath solution.

This simple XPath

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

The choice

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

upon request.

+29
Jun 29 '15 at 14:50
source share
 /records/record[record-type='A'] 
-3
May 26 '09 at 19:28
source share



All Articles