Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

XPath expression for finding subsequent elements that have the same tag name

Here is a sample XML:

<Body>
<Subsection lims:inforce-start-date="2018-12-15" lims:fid="170842" lims:id="170842">
   <MarginalNote lims:inforce-start-date="2018-12-15" lims:fid="170843" lims:id="170843">Determination of rate — 2nd case</MarginalNote>
   <Label>(11.08)</Label>
   <Text>If A is greater than 4.95%, D is less than or equal to 4.95% and the percentage determined by the formula</Text>
   <Text>1/2(A - D) [...] is less than or equal to 0.1%, then the contribution rate for employees and employers for each year after the October 1 date referred to in subsection (11.05) is the rate determined by the formula</Text>
   <Text>4.95% + 1/2(A - 4.95%) + C </Text>
</Subsection>
</Body>

I’d like to write an XPath query that returns Text elements that have siblings that are also Text elements.

I have tried many variations of the following expression, but I can’t quite get it right:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$xPathFR->query("Body//Text[.= following-sibling]");

Any help or pointers would be appreciated!

>Solution :

You could use both the following-sibling:: and preceding-sibling:: axis:

/Body//Text[following-sibling::Text or preceding-sibling::Text]

But probably easier to just test if the parent of the Text element has more than one Text, and then select those Text siblings:

/Body/*[Text[2]]/Text
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading