Search code examples
xmlxpathwhitespacetrim

XPath - trim and starts-with together (cut of whitespaces)


I have XML like this:

<div class="errorMessage">
    <ul>
        <li>
            <a href="...">
                ABC - efg
            </a>
            <a href="...">
                HIJ - klm
            </a>
        </li>
    </ul>
</div>

How do I select link's text which starts with "ABC"? I need to trim the "text()" of "a" and apply "starts-with()" on it. I have something like this, but it doesn't work:

//div[@class='errorMessage']/.//a[starts-with(normalize-space(./text())),'ABC')]/text()

Solution

  • This ought to work:

    //div[@class='errorMessage']//a[starts-with(normalize-space(.), 'ABC')]/text()