1

I have following xml and would like to fetch the "cc" value of "type" ID where a "type" of student exist. I want XPATH to get the value 345.

I have tried this but not getting any value.//a/cc[type="bond"]/following-sibling::cc[type="id"]/value/text()

<?xml version="1.0" encoding="UTF-8"?>

<root> 
  <a> 
    <bb>abc</bb>  
    <cc> 
      <type>id</type>  
      <value>345</value> 
    </cc>  
    <cc> 
      <type>student</type>  
      <value>567</value> 
    </cc>  
    <dd>ddd</dd> 
  </a>  
  <a> 
    <bb>zz</bb>  
    <cc> 
      <type>id</type>  
      <value>999</value> 
    </cc>  
    <cc> 
      <type>employee</type>  
      <value>890</value> 
    </cc>  
    <dd>2</dd> 
  </a> 
</root>
1
  • Why type="bond"? Commented Jan 5, 2018 at 11:45

2 Answers 2

1

Replace following-sibling by preceding-sibling (and bond by student).

//a/cc[type="student"]/preceding-sibling::cc[type="id"]/value/text()

(Tested in xsh).

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

//a[cc[type="student"]]/cc[type="id"]/value/text()

(Tested in https://codebeautify.org/Xpath-Tester)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.