XSLT在IF语句后添加图书价格
XSLT Adding the price of books after an IF Statement
不太确定如何做到这一点,因为"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"语句。
一种可能性是定义
- 一个节点集变量,用于保存要打印的所有书籍
- 然后在设置为打印书籍的节点上循环
- 并在最后使用sum函数来计算总价
例如
<body>
<xsl:variable name="books" select="book[title[@type='non-fiction']][price<30]" />
<xsl:for-each select="$books">
<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:for-each>
<p class="total-price"> <xsl:value-of select="sum($books/price)"/> </p>
</body>
你可以试试这个:
<xsl:template match="/library" >
<html>
<body>
<xsl:variable name="fiction_lt30"
select="book[title[@type='non-fiction'] and price < 30] " />
<xsl:for-each select="$fiction_lt30">
<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:for-each>
<p>total: <xsl:value-of select="sum($fiction_lt30/price)" /></p>
</body>
</html>
</xsl:template>
这里有一个完整而简短的解决方案:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:variable name="vBooks"
select="book[title[@type='non-fiction'] and not(price >= 30)]"/>
<html>
<body>
<xsl:apply-templates select="$vBooks"/>
<p><strong>Total Cost:</strong><span><xsl:value-of
select="sum($vBooks/price)"/></span></p>
</body>
</html>
</xsl:template>
<xsl:template match="book/*[self::title or self::author or self::price]">
<p class="{name()}"> <xsl:value-of select="."/></p>
</xsl:template>
</xsl:stylesheet>
相关文章:
- 正在尝试使用if和else添加类,但无法正常工作
- 在Javascript中为if/else语句添加OR
- 将 HTML 添加到 Javascript IF 函数
- XSLT在IF语句后添加图书价格
- 如何创建自定义属性以添加if.bind
- 当if条件被添加到循环中时,Javascript函数将停止工作
- 将默认选项添加到ng-if
- 从指令中向元素添加ng-if属性
- JQuery 悬停代码:添加 If 语句会破坏它
- 如何在 if else 语句期间添加暂停/延迟
- 如何添加 if 语句以循环图像
- 结构化功能.可以't在现有代码中添加if语句
- 添加 if 条件后,切换按钮停止工作
- 在if语句中添加if语句
- 如何在jQuery中添加if语句来检查JSON文件中是否存在属性
- 如何在javascript(编辑)中添加if else条件到数组中
- 添加if-else语句会导致JavaScript停止工作
- 在Javascript HTML中添加If/Else If Statment
- 向javascript添加if语句
- 如何在我的d3js示例中添加if条件