Xml XSL如何根据条件求和节点值
XML文件Xml XSL如何根据条件求和节点值,xml,xslt,Xml,Xslt,XML文件 <?xml version="1.0"?> <books> <book> <title fiction="true">Happy Potter</title> <author>J. K. Rowling et al</author> <price>29.99</price> </book> <book> <tit
<?xml version="1.0"?>
<books>
<book>
<title fiction="true">Happy Potter</title>
<author>J. K. Rowling et al</author>
<price>29.99</price>
</book>
<book>
<title fiction="true">The Hobbit</title>
<author>J.R.R. Tolkien</author>
<price>25.99</price>
</book>
<book>
<title fiction="true">Greater than 30</title>
<author>J.R.R. Tolkien</author>
<price>31.99</price>
</book>
<book>
<title fiction="false">Non-Fiction</title>
<author>J.R.R. Tolkien</author>
<price>19.99</price>
</book>
<xsl:for-each select="books/book">
<xsl:if test="(title[@fiction='false']) and (price < 30)">
<span name="title"><xsl:value-of select="title"/></span><br/>
<span name="author"><xsl:value-of select="author"/></span><br/>
<span name="price"><xsl:value-of select="price"/></span><br/><br/>
</xsl:if>
</xsl:for-each>
Total: <span name="total"><xsl:value-of select="sum(books/book/price)"/></span>
快乐波特
J.K.罗琳等人
29.99
霍比特人
托尔金
25.99
大于30
托尔金
31.99
非小说类
托尔金
19.99
XSL文件
<?xml version="1.0"?>
<books>
<book>
<title fiction="true">Happy Potter</title>
<author>J. K. Rowling et al</author>
<price>29.99</price>
</book>
<book>
<title fiction="true">The Hobbit</title>
<author>J.R.R. Tolkien</author>
<price>25.99</price>
</book>
<book>
<title fiction="true">Greater than 30</title>
<author>J.R.R. Tolkien</author>
<price>31.99</price>
</book>
<book>
<title fiction="false">Non-Fiction</title>
<author>J.R.R. Tolkien</author>
<price>19.99</price>
</book>
<xsl:for-each select="books/book">
<xsl:if test="(title[@fiction='false']) and (price < 30)">
<span name="title"><xsl:value-of select="title"/></span><br/>
<span name="author"><xsl:value-of select="author"/></span><br/>
<span name="price"><xsl:value-of select="price"/></span><br/><br/>
</xsl:if>
</xsl:for-each>
Total: <span name="total"><xsl:value-of select="sum(books/book/price)"/></span>
总数:
因此,我的XSL将根据给定的条件从XML中提取数据。
当虚构=true
且价格<30
时。但我不知道如何计算总价值。任何建议都将不胜感激 使用谓词求和(books/book[title[@fibrity='false']和price 30]/price)
你也可以缩短
<xsl:for-each select="books/book">
<xsl:if test="(title[@fiction='false']) and (price < 30)">
到
更好的是,定义一个变量
,并将其用于
和
。我甚至不能缩短它和总和,它会变成语法错误谢谢Michael.hor257k。你的想法是在我身上工作。谢谢你提供的优势信息。@JackWong,我不确定你是从哪里得到语法错误的,我提出的建议似乎很管用。您的输入片段不完整,我必须为根元素添加一个结束标记。