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

Advertisements

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:

$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

Leave a ReplyCancel reply