1
votes

For the following xml:

<books>   
    <book>
       <author>Peter</author>
       <title>Tales from Somewhere</title>
       <data>
          <version>1</version>
       </data>
    </book>
    <book>
       <author>Paul</author>
       <title>Tales from Nowhere</title>
       <data>
          <version>2</version>
       </data>
    </book>
 </books>

How can I get the <version> value of the book author 'Paul' above, using this type of notation for building a Java XPathExpression:

//*[local-name()='books']/*

?

I used the following question as a reference: Get first child node in XSLT using local-name()

Thanks!

1
You submitted it twice it seems.user764357
Thanks. Duplicate deleted.Fuzzy Analysis

1 Answers

2
votes

This XPath will get the version of a book where there is at an author element with the value "Paul":

//book[author="Paul"]/data/version

When run against this XML:

<books> 
  <book> 
    <author>Peter</author>  
    <title>Tales from Somewhere</title>  
    <data> 
      <version>1</version> 
    </data> 
  </book>  
  <book> 
    <author>Paul</author>  
    <title>Tales from Nowhere</title>  
    <data> 
      <version>2</version> 
    </data> 
  </book> 
  <book> 
    <author>Peter</author>  
    <author>Paul</author>  
    <title>How to write a book with a friend</title>  
    <data> 
      <version>7</version> 
    </data> 
  </book> 
</books>

You get this result:

<version>1</version>
<version>7</version>