XSLT 2.0
Using XSLT 2.0, the following solution works:
<xsl:variable name="map">
<entry key="key-1">value1</entry>
<entry key="key-2">value2</entry>
<entry key="key-3">value3</entry>
</xsl:variable>
<xsl:template match="/">
<output>
<xsl:value-of select="$map/entry[@key='key-1']"/>
</output>
</xsl:template>
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:.
<xsl:value-of select="document('')//xsl:variable[@name='map']/map/entry[@key='key-1']"/>
As described in the XSLT 1.0 specification:
document('')
refers to the root node of
the stylesheet; the tree
representation of the stylesheet is
exactly the same as if the XML
document containing the stylesheet was
the initial source document.
However, you don't need to use xsl:variable
for this. You could specify your map node directly under xsl:stylesheet
, but you must remember that a top level elements must have a non null namespace URI:
<xsl:stylesheet
version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="some.uri" exclude-result-prefixes="my">
<my:map>
<entry key="key-1">value1</entry>
<entry key="key-2">value2</entry>
<entry key="key-3">value3</entry>
</my:map>
<xsl:template match="/">
<output>
<xsl:value-of select="document('')/*/my:map/entry[@key='key-1']"/>
</output>
</xsl:template>
</xsl:stylesheet>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…