I want to have a key value map in xsl and so defined a variable which has an xml fragment, but later when I try to access the xml nodes in the variable I get an error that type of the xpath xpression cannot be resolved.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="map">
<map>
<entry key="key-1">value1</entry>
<entry key="key-2">value2</entry>
<entry key="key-3">value3</entry>
</map>
</xsl:variable>
<output>
<xsl:value-of select="$map/entry[@key='key-1']"/>
</output>
</xsl:template>
</xsl:stylesheet>
XSLT 2.0
Using XSLT 2.0, the following solution works:
XSLT 1.0
You cannot use a result tree fragment in a XPath expression in XSLT 1.0, but
fn:document()can retrieve map values. An answer to a similar question will work here:.As described in the XSLT 1.0 specification:
However, you don’t need to use
xsl:variablefor this. You could specify your map node directly underxsl:stylesheet, but you must remember that a top level elements must have a non null namespace URI: