Matching a node based on a sibling’s value with XPath

Matching a node based on a sibling’s value with XPath

Asked on January 8, 2019 in XML.
Add Comment


  • 6 Answer(s)

         Matching a node which is based on a sibling’s value with XPath, found the below code,

    /records/record/name[../record-type/text() = "A"]
    
    Answered on January 8, 2019.
    Add Comment

         Alternatively can filter a parent element by its children like the given code

    /records/record[record-type[text()='A']]/name
    
    Answered on January 8, 2019.
    Add Comment

         To match a node this simple XPath is used

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

         it will selects

    <name>svn</name>
    <name>blog</name>
    
    Answered on January 8, 2019.
    Add Comment
    <?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>
    Answered on February 28, 2019.
    Add Comment
    <a>
      <bb>abc</bb>
      <cc>ccc</cc>
      <dd>ddd</dd>
    </a>
    <a>
      <bb>zz</bb>
      <cc>1</cc>
      <dd>2</dd>
    </a>
    Answered on February 28, 2019.
    Add Comment
    TEXT VALUES OF ALL <CC> ELEMENTS
    //cc                                    /text()
        THAT HAVE A PRECEDING SIBLING <BB>
        [preceding-sibling::bb             ]
                              THAT HAS TEXT VALUE EQUAL TO "zz"
                              [text()="zz"]
    Answered on February 28, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.