1

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!

Community
  • 1
  • 1
Fuzzy Analysis
  • 3,168
  • 2
  • 42
  • 66

1 Answers1

2

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>
  • Thanks. That is the XPath, which is great. Now I am looking for the expression used to build a Java XPathExpression with, based on this XPath (apologies for not specifying that earlier). – Fuzzy Analysis Jan 10 '14 at 02:16
  • I don't do the java. It might be best asking a fresh question for that. –  Jan 10 '14 at 02:20
  • 1
    Was not expecting your answer to be able to compile into an XPathExpression but it did. Cheers – Fuzzy Analysis Jan 10 '14 at 02:36