3
votes

I am executing an XSLT transformation using java program. Given below is the block of code that is used for transformation. Here I am creating a hashmap and setting a value which needs to be accessed in the XSLT.

    TransformerFactory factory = TransformerFactory.newInstance();
    StreamSource xslStream = new StreamSource(inputXSL);
    Transformer transformer = factory.newTransformer(xslStream);
    Map<String,String> mapData = new HashMap<String,String>();
    mapData.put("103", "188 E 6th Street");
    transformer.setParameter("mapData", mapData);

The xslt code(inputXSL) that does the transformation is shown below.

<?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="mapData"/>

<xsl:template match="/class">

        <html>
            <body>
                <h3>Student Details</h3>
                <table border="1">
                    <tr bgcolor="#E6E6FA">
                        <th>Roll Number</th>
                        <th>First Name</th>
                        <th>Last Name</th>
                        <th>Nick Name</th>
                        <th>Marks</th>
                        <th>Address</th>
                    </tr>


                    <xsl:for-each select="student">
                        <tr>
                            <xsl:variable name="rollNumber" select="./@rollno"/>
                            <xsl:variable name="addressData" select="map:get($mapData,$rollNumber)"/>
                            <td>
                                <xsl:value-of select="./@rollno" />
                            </td>
                            <td>
                                <xsl:value-of select="firstname" />
                            </td>
                            <td>
                                <xsl:value-of select="lastname" />
                            </td>
                            <td>
                                <xsl:value-of select="nickname" />
                            </td>
                            <td>
                                <xsl:value-of select="marks" />
                            </td>
                            <td>
                                <xsl:value-of select="$addressData" />
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

In the above xslt program I am able get a value for variable addressData if I pass a hard coded key such as '101'(refer the students.xml). But if I iterate over the array and then pass the rollnumber as the key the value in addressData variable is empty.

I am not sure why would the map.:get method would accept a hard coded value but not a parameter. Probably I am missing something.

Given below is the data xml that is passed to the xslt.

    <?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="ParseResponse.xsl"?>
<class>
    <student rollno="101">
        <firstname>PPP</firstname>
        <lastname>LLL</lastname>
        <nickname>JJJ</nickname>
        <marks>85</marks>
    </student>
    <student rollno="102">
        <firstname>AAA</firstname>
        <lastname>CCC</lastname>
        <nickname>DDD</nickname>
        <marks>95</marks>
    </student>
    <student rollno="103">
        <firstname>KKK</firstname>
        <lastname>LLL</lastname>
        <nickname>WWW</nickname>
        <marks>90</marks>
    </student>
</class>

Can anybody explain why would the map:get method would take hard coded input but not a variable ?

1
Try map:get($mapData,string($rollNumber)) instead of map:get($mapData,$rollNumber). - Martin Honnen

1 Answers

3
votes

As you defined- HashMap<String,String>() its keys and values must be String.While You are passing $rollNumber as a variable type on retrieving data.

<xsl:variable name="addressData" select="map:get($mapData,$rollNumber)"/>

you have to cast it into String from variable before passing.

<xsl:variable name="addressData" select="map:get($mapData,(String)$rollNumber)"/>