[英]How to transform xsl that XML document has namespace
<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>
请帮助我修复此错误,非常感谢
因为您以//开头,所以它将搜索整个文档,因此每次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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.