我一直在尝试将变换中的参数传递给xsl文件,但是当我尝试使用它的值时,它似乎总是空白。
我的java代码:
myTransformer.setParameter("show", "all");
我在xsl文件中的参数的全局声明:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:param name="show"/>
我试图像这样测试模板中的值:
<xsl:value-of select="$show"/>
<xsl:value-of select="concat($show, ' =? ','all')"/>
但&#34;显示&#34;总是空白的。我已经尝试在java代码中添加双引号和单引号以获取值&#34;&#39; all&#39;&#34;为了避免被解释为xpath,还尝试在模板中使用xsl:with-param,但似乎没有任何工作......
修改 这是我的xsl文件(carpresentor.xsl)的一个更扩展的部分:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:param name="show" />
<xsl:template match="/">
<html><body>
<xsl:apply-templates>
</xsl:apply-templates>
</body></html>
</xsl:template>
<xsl:template match="/*">
<h1 style="color: #FFFFFF; text-align: center;"></h1>
<xsl:value-of select="$show"/>
<xsl:value-of select="concat($show, ' =? ','all')"/>
<table align="center" border="2" style="background-color: #ffffff;">
<tr>
<xsl:for-each select="*[position() = 1]/@*">
<td><xsl:value-of select="concat(local-name(), ' ')"/></td>
</xsl:for-each>
<xsl:for-each select="*[position() = 1]/*">
<td><xsl:value-of select="concat(local-name(), ' ')"/></td>
</xsl:for-each>
</tr>
<xsl:apply-templates/>
</table>
</xsl:template>
</xsl:stylesheet>
我的xmltransformer.java(void init和doPost)的一部分:
public void init(ServletConfig config) throws UnavailableException {
System.out.println("Init start");
try {
ctx = config.getServletContext(); // we will use the 'contex' below
absPath = ctx.getRealPath("/WEB-INF/carPresentor.xsl");
xsltDoc = new SAXSource(new InputSource(absPath));
tF = TransformerFactory.newInstance();
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
fact.setNamespaceAware(true);
DocumentBuilder builder = fact.newDocumentBuilder();
doc = builder.parse(absPath);
System.out.println("Name of document element is " + doc.getDocumentElement().getNodeName());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("dopost start");
System.out.println("Name of document element (at the post) is " + doc.getDocumentElement().getNodeName());
String color = request.getParameter("color");
String tablecolor = request.getParameter("table-color"); // edw to request
String xml = request.getParameter("show");
PrintWriter pwr = response.getWriter();
try {
DOMSource ds = new DOMSource(doc);
System.out.println(((Document) ds.getNode()).getDocumentElement().getNodeName() + " " + ((Document) ds.getNode()).getDocumentElement().getNodeValue());
myTransformer = tF.newTransformer(new DOMSource(doc));
myTransformer = tF.newTransformer(xsltDoc);
myTransformer = tF.newTransformer(ds);
StreamSource xmlSource;
xmlSource = new StreamSource(ctx.getResourceAsStream("/WEB-INF/cars.xml"));
System.out.println("Sending back the xml transformed into html");
response.setContentType("text/html"); //in order to put in http body
myTransformer.transform(xmlSource, new StreamResult(pwr));
myTransformer.setParameter("show", "'xml'");
pwr.close();
}
我使用DOM&amp;用于转换的SAX解析器。我的目标是使用额外的参数&#34; show&#34;在xsl文件中根据用户输入(java文件从其请求其参数的html格式)显示不同的一些xml属性。