Search code examples
saxonxslt-3.0

XSLT 3 - How to use namespace-context in xsl:evaluate


Using Saxon EE 9.9, I'm trying to use xsl:evaluate with the namespace-context, see my basic try below where I've assigned the dummy element that reports the namespace prefix "my" to the namespace which doesn't work in the variable $namespace-context. I can't find any examples of how to set this namespace-context argument on the web, this was the best that ChatGPT could provide:

<xsl:variable name="xmlData">
    <root xmlns="http://example.com/my">
        <element>Hello, World!</element>
    </root>
</xsl:variable>

<xsl:template match="/">
    <xsl:variable name="namespace-context"><my:dummy xmlns:my="http://example.com/my"/></xsl:variable>
    
    <xsl:variable name="expr" select="'/my:root/my:element'"/>
    <xsl:variable name="result" as="node()*">
        <xsl:evaluate xpath="$expr" context-item="$xmlData" namespace-context = "$namespace-context"/>
    </xsl:variable>
    
    <output>
        <xsl:copy-of select="$result"/>
    </output>
</xsl:template>

Solution

  • You need to select an element node, not a document node, so doing

        <xsl:evaluate xpath="$expr" context-item="$xmlData" namespace-context = "$namespace-context/*"/>
    

    should work: example fiddle.

    Or make sure you set up the variable as e.g. <xsl:variable name="namespace-context" as="element()"><my:dummy xmlns:my="http://example.com/my"/></xsl:variable>