0
votes

Does anybody know how to automagically get xpath with keys when using dom4j elements?

Let me explain:

Given the following xml I have created a dom4j document.

<response>
  <installation>
    <id>0001</cid>
    <code>0000</code>
  </installation>
  <installation>
    <id>0002</cid>
    <code>0078</code>
  </installation>
</response>

Looping through the document I can create an xpath for each element using element.getUniquePath() or element.getPath()

/response/installation[1]/code
or
/response/installation/code

So far so good, but not quite good enough. Since the order of the objects can never be know in advance I would like to use key in my xpath.

 /response/installation[id=0001]/code

Now my question is if anybody out there knows how to do this.

Thanks in advance.

1

1 Answers

1
votes

I don't know any way to change the strategy behind getUniquePath and getPath.

If you iterate over elements which are codes you can retrieve the preceding sibling and generate an XPath with that, by retrieving the text content of the id tag before the code one.

Otherwise this XPath would work: /response/installation/code[preceding-sibling::id[text()=0002]]