【发布时间】:2016-05-07 09:46:55
【问题描述】:
不确定如何执行此操作,因为“sum()”函数正在添加 XML 页面中的所有值,而不是“if”语句选择的值。
这是 XML:
<book>
<title type="non-fiction">Harry Potter and the Philosophers Stone</title>
<author>J.K Rowling</author>
<publisher>Bloomsbury</publisher>
<year>1997</year>
<price>12.99</price>
</book>
<book>
<title type="fiction">The Lord of the Rings</title>
<author>J. R. R. Tolkien</author>
<publisher>George Allen and Unwin</publisher>
<year>1954</year>
<price>39.99</price>
</book>
<book>
<title type="non-fiction">The Right Stuff</title>
<author>Tom Wolfe</author>
<publisher>Farra, Staus and Giroux</publisher>
<year>1979</year>
<price>29.99</price>
</book>
这是 XSLT:
<xsl:output
method="html"
indent="yes"
version="4.0"
doctype-public="-//W3C//DTD HTML 4.01//EN"
doctype-system="http://www.w3.org/TR/html4/strict.dtd"/>
<xsl:template match="/library">
<html>
<body>
<xsl:for-each select="book">
<xsl:if test="title[@type='non-fiction']">
<xsl:if test="price<30" >
<p class="title"> <xsl:value-of select="title"/></p>
<p class="author"> <xsl:value-of select="author"/> </p>
<p class="price"> <xsl:value-of select="price"/> </p>
</xsl:if>
</xsl:if>
</xsl:for-each>
</body>
我想将符合所有条件的书籍总数相加。我假设 sum 函数会这样做,但它会添加所有书籍,无论它是否通过了 'if' 语句。
【问题讨论】:
-
不能用count()函数代替sum()函数吗?
-
我认为该计数是将列表中的项目数量相加。我会做一些研究。
标签: javascript html xml xslt foreach