Search code examples
xmlxpathxmllint

xmllint selects a node even though contains() should not match


I've got this CML input:

<prova>
    <parent>
        <groupId>error</groupId>
    </parent>
    <groupId>ok</groupId>
</prova>

and this XPath:

xmllint --xpath '/prova/parent/groupId/text()[contains(.,'ok')]'

that should not match anything but it matches error. Why?


Solution

  • Don't use nested single quotes:

    xmllint --xpath '/prova/parent/groupId/text()[contains(.,"ok")]' test.xml
    

    result

    XPath set is empty
    

    Explanation:

    The shell sees '/prova/parent/groupId/text()[contains(.,'ok')]' as

    /prova/parent/groupId/text()[contains(.,ok)]
    

    In this XPath, ok is interpreted as an element name. The element <ok> does not exist, so this selects an empty node-set.

    For the sake of contains(), the empty node-set is converted to the empty string. And any string contains the empty string, so the condition is always true.