Viciouss
Viciouss

Reputation: 283

Select immeditely following XML elements of the same element with XPath

given the following XML snippet:

<?xml version="1.0" encoding="UTF-8"?>
<RootNode>
  <PR1>PR1</PR1>
  <ROL>one</ROL>*
  <ROL>two</ROL>*
  <DG1>DG1</DG1>
  <ROL>three</ROL>
  <ZBK>ZBK</ZBK>
  <ROL>four</ROL>
</RootNode>

Is it possible to select the two ROL elements that are right behind the PR1 element (the ones marked with an asterisk) with an XPath expression, but not the other ROL elements? I tried

//PR1/following-sibling::ROL

but that would also get the other ROL segments. Is there a way to stop XPath looking for elements after the first non matching hit? Or maybe there is another approach without following-sibling?

Upvotes: 1

Views: 137

Answers (1)

hr_117
hr_117

Reputation: 9627

I think the answer would be more like this :

<xsl:value-of select="//PR1[1]/following-sibling::ROL[
    generate-id( preceding-sibling::*[name() != 'ROL'][1])= generate-id(//PR1[1])
    ]" />

Find the following-sibling ROL of first PR1 where
the preceding-sibling (which is not an ROL) is the same as the first PR1.

Upvotes: 2

Related Questions