Skip to content Skip to sidebar Skip to footer

Xpath Subset Selection

I have the following XML (which is actually HTML):

something

a

b

otherthing

Solution 1:

My take

//p[preceding-sibling::h4[1] and not(preceding-sibling::h4[position() > 1])]

finds all p elements which are siblings of the first h4 but not siblings of any other h4 on the same axis

Alternative

//h4[1]/following-sibling::p[count(preceding-sibling::h4) = 1]

finds all following p element of the first h4 element that do have exactly one preceding h4 element

Solution 2:

Use:

/*/h4[1]/following-sibling::p
            [not(count(preceding-sibling::* | /*/h4[2])
                =
                 count(preceding-sibling::*)
                 )
             ]

More generally, the intersection of two nodesets $ns1 and $ns2 is selected by:

$ns1[count(.|$ns2) = count($ns2)]

The fact that a node $n is not in a node-set $ns1 is expressed by:

not(count($n | $ns1) = count($ns1))

This is fundamental set theory and usage of the standard XPath | (union operator and not() function.

Solution 3:

Assuming no <p> preceding the first <h4>,

//h4[2]/preceding-sibling::p

Post a Comment for "Xpath Subset Selection"