Ruan Mendes
Ruan Mendes

Reputation: 92274

XPath to find element with a HTML line break

I need an xpath that will find some text containing HTML line breaks <br/>. For example:

<ul>
   <li>ABC<br/>DEF</li>
   <li>XYZ<br/>NOP</li>
</ul>

Let's say I'm trying to find the li that contains ABC<br/><DEF>. I've tried the following:

$x("//li[normalize-space(.)='ABC DEF']")
$x("//li[text() ='ABC<br/>DEF']")
$x("//li[contains(., 'ABC DEF']")

But they return nothing. I saw this answer XPath contains(text(),'some string') doesn't work when used with node with more than one Text subnode but I couldn't figure out how to use it in my case.

Upvotes: 2

Views: 2247

Answers (2)

harpo
harpo

Reputation: 43158

The following expression will get you close:

li[br[preceding-sibling::node()[1] = 'ABC']
     [starts-with(following-sibling::node()[1], 'DEF')]]

If you need to match only items where the text ends with ABC, it will be a little longer.

The following transform will select the first matching li:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" />

    <xsl:template match="/">
        <matches>
            <xsl:copy-of select="(//li[br[preceding-sibling::node()[1] = 'ABC']
                                                             [starts-with(following-sibling::node()[1], 'DEF')]
                                                         ])
                                                           [1]" />
        </matches>
    </xsl:template>

</xsl:stylesheet>

Input:

<ul>
   <li>ABC<br/>DEF</li>
   <li>XYZ<br/>NOP</li>
   <li><p>XYZ<br/>NOP</p></li>
   <li>ABC<br/>DEF</li>
   <li>DEF GHI</li>
   <li>ABC<![CDATA[<br/>]]>DEF</li>
</ul>

Output:

<?xml version="1.0" encoding="utf-8"?>
<matches>
    <li>ABC<br />DEF</li>
</matches>

Upvotes: 2

Michał Tabor
Michał Tabor

Reputation: 2467

//li[br]

This should work. It means: select all li elements having br child

Upvotes: -2

Related Questions