Search code examples
xpathxmlspy

XPath with namespace in query tool tab


I am trying to use the XPath query tool (bottom tab in XMLSpy) to test XPaths in an XML that uses namespaces.

I don't know how to declare the namespace.

Example XML

<?xml version="1.0" encoding="UTF-8"?>
<Level1>
    <Level2>
        <Data xmlns="http://www.myns.com">
            <Text>test</Text>
        </Data>
    </Level2>
</Level1>

If I select XQuery 3.1 as my query language, I can test my XPath like this:

declare namespace myns="http://www.myns.com";
//Level1/Level2/myns:Data

But if I select XPath 3.1 as my query language, how do I define the namespace? (Doing simply //Level1/Level2/Data does not work.)

Thanks.


Solution

  • XPath 1.0 (and higher)

    //Level1/Level2/*[local-name()='Data']
    

    or

    //Level1/Level2/*[namespace-uri()='http://www.myns.com' and local-name()='Data']
    

    XPath 2.0 (and higher)

    //Level1/Level2/*:Data
    

    XPath 3.0 or 3.1

    //Level1/Level2/Q{http://www.myns.com}Data
    

    See also this excellent answer