我必须根据我在使用表达式语言制作 JSP 的 XSL 文件中的请求属性来使 xPath 动态化。所以现在我有这个东西:
<xsl:variable name="xPath">@name='${categoryName}'</xsl:variable>
<xsl:for-each select="tar:products/category[$xPath]/subcategory">
但这不起作用。所以问题是如何解决这个问题并使 xPath 工作?
输入 XML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tar:products SYSTEM "products.dtd" >
<?xml-stylesheet type='text/xsl' href='products2HTML.xsl'?>
<tar:products xmlns:tar="http://www.epam.com/products"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.epam.com/products products.xsd ">
<category name="Electronics">
<subcategory name="mp3">
<product name="iPod 4g">
<provider>Apple</provider>
<model>Qd246</model>
<dateOfIssue>11-11-2012</dateOfIssue>
<color>white</color>
<notInStock/>
</product>
<product name="Cowon">
<provider>Cowon</provider>
<model>sF223</model>
<dateOfIssue>13-12-2012</dateOfIssue>
<color>black</color>
<price>13456</price>
</product>
<product name="IRiver">
<provider>IRiver e30</provider>
<model>Ir234</model>
<dateOfIssue>13-12-2012</dateOfIssue>
<color>black</color>
<price>13456</price>
</product>
</subcategory>
<subcategory name="Computers">
<product name="iMac">
<provider>Apple</provider>
<model>Mm232</model>
<dateOfIssue>20-02-2012</dateOfIssue>
<color>silver</color>
<notInStock/>
</product>
<product name="PC">
<provider>Acer</provider>
<model>Ae135</model>
<dateOfIssue>22-11-2012</dateOfIssue>
<color>black</color>
<price>13456</price>
</product>
</subcategory>
</category>
<category name="Toys">
<subcategory name="Electronical toys">
<product name="Cat">
<provider>UrkToy</provider>
<model>cC246</model>
<dateOfIssue>11-11-2012</dateOfIssue>
<color>grey</color>
<price>1100</price>
</product>
<product name="Dog">
<provider>LatToy</provider>
<model>Ld223</model>
<dateOfIssue>03-12-2012</dateOfIssue>
<color>white</color>
<price>13456</price>
</product>
</subcategory>
<subcategory name="Fabric toys">
<product name="Bear">
<provider>RusToy</provider>
<model>BB122</model>
<dateOfIssue>20-02-2012</dateOfIssue>
<color>brown</color>
<notInStock/>
</product>
<product name="Bird">
<provider>BelToy</provider>
<model>Bb110</model>
<dateOfIssue>05-11-2012</dateOfIssue>
<color>red</color>
<price>500</price>
</product>
</subcategory>
</category>
</tar:products>
我在上一页的请求中放入了参数categoryName
,它已成功显示在我的页面上,所以问题不在其中。xsl
以下是有关文件的更多信息:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tar="http://www.epam.com/products"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.epam.com/products products.xsd ">
<xsl:output method="html" />
<xsl:param name="categoryName" select="'${categoryName}'"></xsl:param>
<xsl:template match="/">
<table border="1">
<tr bgcolor="#CCCCCC">
<td align="center">
<strong>
Subcategories
</strong>
</td>
</tr>
<xsl:value-of select="$categoryName"/><!-- this string works -->
<xsl:for-each select="tar:products/category[@name=$categoryName]/subcategory">
<xsl:sort order="descending" select="count(product)"
data-type="number" />
<a href="controller?command=products&name={@name}">
<tr bgcolor="#F5F5F5">
<td>
<xsl:value-of select="@name" />
(
<xsl:value-of select="count(product)" />
)
</td>
</tr>
</a>
</xsl:for-each>
</table>
<form method="post" action="/controller?command=back">
<input type="submit" value="Back"/>
</form>
</xsl:template>
</xsl:stylesheet>
预期输出,当请求参数categoryName
等于'Toys'
:
实际输出:
已解决
在我的 java 类中,我在转换之前将参数放入请求中,但只需将其放入转换器参数中:
transformer.setParameter("categoryName", name);