【发布时间】:2014-07-23 17:00:45
【问题描述】:
想要在 xsl 中添加每个节点的价格值。但这些值也可以有 -9756vc(eg) 类型的值,不需要包含在总和中,然后它给出 NaN 作为我不想要的结果。我无法弄清楚我们如何做到这一点。 以下是示例 XML:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>-9756vc</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
</catalog>
我正在尝试的代码。
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>`enter code here`
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<xsl:variable name="color" select="format-number(price,'0.##')" />
<xsl:choose>
<xsl:when test="$color = 'NaN' ">
<td> <xsl:value-of select="'0'"/> </td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="$color"/></td>
</xsl:otherwise>
</xsl:choose>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
<xsl:for-each select="catalog/cd">
<xsl:variable name="color" select="format-number(price,'0.##')" />
<xsl:choose>
<xsl:when test="$color != 'NaN' ">
<xsl:variable name="Sum" select ="$color + 1"/>
<xsl:value-of select="$Sum"/>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
【问题讨论】:
-
在这种情况下,您是否可以显示您实际期望的输出?谢谢!