XML xpath, get the parent element till a specific element

Given the XML

<?xml version="1.0"?>
<root>
    <foo id="id1">
        <foo id="i2">
            <baz/>
        </foo>
    </foo>
</root>

You can find the nearest ancestor foo element from baz using the XPath expression:

//baz/ancestor::foo[1]

Which will select the foo element node of id “i2”.

So in your example (if I understand right) once you have got the “a” element you want, you can get “back up” the tree to the nearest ancestor div by appending “/ancestor::div[1]” to your expression.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)