Search code examples
xpathdomxpath

XPath Match Nodes with Only <br> Tags


I'm trying to target all nodes which contain only line break tags tags. I'm using the below expression:

//*[br and not(*[not(self::br)])]

This almost works but it also captures nodes which contain free text:

<p>
    This node should be omitted from results
    <br />
    More sample text
</p>

How would I only capture nodes structured like below:

<p>
    <br />
</p>

Solution

  • You were very close. This should do it:

    //*[br and not(node()[not(self::br)])]
    

    Another alternative:

    //*[br and count(br) = count(node())]