检索XSLT中的哈希图值 [英] Retrieving hashmap values in XSLT
问题描述
我正在使用Java程序执行XSLT转换.下面给出的是用于转换的代码块. 在这里,我正在创建一个哈希映射并设置一个需要在XSLT中访问的值.
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);
执行转换的xslt代码(inputXSL)如下所示.
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>
在上面的xslt程序中,如果我传递诸如'101'的硬编码键(请参阅students.xml),则可以获取变量addressData的值.但是,如果我遍历数组,然后将rollnumber作为键传递,则addressData变量中的值为空.
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.
我不确定map.:get方法为什么会接受硬编码值而不接受参数.可能是我遗漏了一些东西.
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.
下面给出的是传递到xslt的数据xml.
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>
任何人都可以解释为什么map:get方法将接受硬编码的输入但不接受变量吗?
Can anybody explain why would the map:get method would take hard coded input but not a variable ?
推荐答案
如您所定义-HashMap<String,String>()
其键和值必须为String
.在检索数据时,您将$rollNumber
作为variable
类型传递
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)"/>
您必须先将其从variable
投射到String
中.
you have to cast it into String
from variable
before passing.
<xsl:variable name="addressData" select="map:get($mapData,(String)$rollNumber)"/>
这篇关于检索XSLT中的哈希图值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!