简体   繁体   中英

Issue while accessing Hashmap into XSL

Assuming, I have a snippet of code as:

 Map mappingId = new HashMap();
 mappingId.put("1", "1000");
 transformer.setParameter("mappingId", mappingId);

 transformer.transform(...);

and I have a simple XSLT that attempts to get the key from this

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:map="xalan://java.util.Map"
extension-element-prefixes="map">

<xsl:param name="mappingId" />

<xsl:template match="/">
<xsl:variable name="id" select="map:get($mappingId, '1')" />
<MappedId><xsl:value-of select="id"/></MappedId>

</xsl:template>
</xsl:stylesheet>

I obtain the following error:

ERROR: 'could not find method java.util.Map.get([ExpressionContext,] #STRING, #STRING) FATAL ERROR: 'Could not compile stylesheet'.

Can someone please help me out with how to access the java map into XSL?

The "id" parameter must be accessed by appending $. The following XSL seems to give expected output for me (java 1.6).

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:map="xalan://java.util.Map" extension-element-prefixes="map">

    <xsl:param name="mappingId" />

    <xsl:template match="/">
        <xsl:variable name="id" select="map:get($mappingId, '1')" />
        <MappedId>
            <xsl:value-of select="$id" />
        </MappedId>

    </xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<MappedId>1000</MappedId>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM