ddavison
ddavison

Reputation: 29032

xpath match without "complicated expressions"

Assuming the following XML

<project>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
    </dependency>
    <dependency>
      <groupId>javax.mail</groupId>
      <artifactId>mail</artifactId>
      <version>1.4.5</version>
    </dependency>
  </dependencies>
</project>

I am attempting to select the node <version>4.10</version> that belongs to junit.

I have succeeded in selecting the element using an xpath tester, using the expression:

/project/dependencies/dependency/artifactId[text()='junit']/../version

however, it appears the library I am using does not recognize the .. parent selector.

I've also tried

/project/dependencies/dependency/artifactId[text()='junit']/following-sibling::version

which worked for my tester as well, but again, this is a complicated expression (hence the following-sibling::).

Is there a way I can accomplish selecting this element using simpler expressions?

Upvotes: 1

Views: 42

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167471

Use the predicate a step earlier: /project/dependencies/dependency[artifactId = 'junit']/version.

Upvotes: 2

Related Questions