XPath expression to select self, preceding and following nodes

xpath

Solution

Use a single XPath like:

"//*[
     (self::a and following-sibling::*[1][self::div and span/a='TRAGET']) or
     (self::div and span/a='TARGET') or
     (self::br and preceding-sibling::*[1][self::div and span/a='TARGET']) or
     (self::br and preceding-sibling::*[2][self::div and span/a='TARGET'])
    ]"

Do note that your document is not well formed due to unclosed br tags. Moreover, I didn't include any namespace, which you can add if necessary.

Problem

I'd like to select the following HTML in a document, based on the content of TARGET. I.e. if TARGET matches, select everything. However, I'm not sure where to go after: `id('page')/x:div/span/a='TARGET'` – How to use parent, child, and sibling expressions to get the containing `div`, the `a` preceding that `div`, and the two `br` tags following the `div` ``` <a></a> <div> <br /> <span> <a>TARGET</a> <a></a> <span> <span> <a></a> </span> <a></a> <span></span> </span> <span> <a></a> </span> </span> </div> <br /> <br /> ```

Original source