<ns2:Products xmlns="https://www.schema.product.com" xmlns:ns2="https://www.schema.products.com">
<Product ProductId="1">
<ProductName> Hộp Hoa Hồng Trắng</ProductName>
<ProductPrice>550000</ProductPrice>
<ProductImage>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</ProductImage>
</Product>
<Product ProductId="2">
<ProductName>An Lành</ProductName>
<ProductPrice>780000</ProductPrice>
<ProductImage>https://dienhoa24gio.net//assets/upload/product/17-10-2015/an-lanh-1445039808/274_default.jpg</ProductImage>
</Product>
</ns2:Products>
用xsl文件显示表格,使用xml文件的数据
<xsl:template match="//*[local-name()='Products']">
<table border="1">
<tr>
<th>name</th>
<th>price</th>
<th>image</th>
</tr>
<xsl:for-each select="//*[local-name()='Product']">
<tr>
<td><xsl:value-of select="//*[local-name()='ProductName']"/></td>
<td><xsl:value-of select="//*[local-name()='ProductPrice']"/></td>
<td><xsl:value-of select="//*[local-name()='ProductImage']"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<c:import url="test.xml" var="xmlDoc" charEncoding="UTF-8"/>
<c:import url="test.xsl" var="xslDoc" charEncoding="UTF-8"/>
<x:transform xml="${xmlDoc}" xslt="${xslDoc}"/>
但是当运行jsp页面时,行表的数据是相同的,我不知道
<table border="1">
<tr>
<th>name</th><th>price</th><th>image</th>
</tr>
<tr>
<td> Hộp Hoa Hồng Trắng</td><td>550000</td><td>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</td>
</tr>
<tr>
<td> Hộp Hoa Hồng Trắng</td><td>550000</td><td>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</td>
</tr>
<tr>
<td> Hộp Hoa Hồng Trắng</td><td>550000</td><td>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</td>
</tr>
<tr>
<td> Hộp Hoa Hồng Trắng</td><td>550000</td><td>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</td>
</tr>
<tr>
<td> Hộp Hoa Hồng Trắng</td><td>550000</td><td>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</td>
</tr>
</table>
请帮助我修复此错误, 非常感谢
答案 0 :(得分:0)
因为您从//开始所有内容,所以它将搜索整个文档,因此每次for-each查找产品时,它都会查找与第一个与ProductName匹配的元素,而不是当前Product元素中的ProductName。
请尝试以下xsl:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"><xsl:template match="//*[local-name()='Products']">
<table border="1">
<tr>
<th>name</th>
<th>price</th>
<th>image</th>
</tr>
<xsl:for-each select="//*[local-name()='Product']">
<tr>
<td><xsl:value-of select="*[local-name()='ProductName']"/></td>
<td><xsl:value-of select="*[local-name()='ProductPrice']"/></td>
<td><xsl:element name="img">
<xsl:attribute name="src"><xsl:value-of select="*[local-name()='ProductImage']"/></xsl:attribute></xsl:element></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
输入
<ns2:Products xmlns="https://www.schema.product.com" xmlns:ns2="https://www.schema.products.com">
<Product ProductId="1">
<ProductName> Hộp Hoa Hồng Trắng</ProductName>
<ProductPrice>550000</ProductPrice>
<ProductImage>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</ProductImage>
</Product>
<Product ProductId="2">
<ProductName>An Lành</ProductName>
<ProductPrice>780000</ProductPrice>
<ProductImage>https://dienhoa24gio.net//assets/upload/product/17-10-2015/an-lanh-1445039808/274_default.jpg</ProductImage>
</Product>
</ns2:Products>
它给了我输出
<table border="1">
<tr>
<th>name</th>
<th>price</th>
<th>image</th>
</tr>
<tr>
<td> Hộp Hoa Hồng Trắng</td>
<td>550000</td>
<td>https://dienhoa24gio.net//assets/upload/product/20-09-2015/hop-hoa-hong-trang-1442711392/274_default.jpg</td>
</tr>
<tr>
<td>An Lành</td>
<td>780000</td>
<td>https://dienhoa24gio.net//assets/upload/product/17-10-2015/an-lanh-1445039808/274_default.jpg</td>
</tr>
</table>