我想知道如何使用XSL在XML中显示以下内容?
displaying this formula:
(value1 * 5 + value2 * 4 + value3 * 3 + value4 * 2 + value5 * 1 / value1 + value2 + value3 + value4 + value5)
这里是从中获取值的地方:
<ratings>
<book>
<title>Doctor Sleep</title>
<author>Stephen King</author>
<userRatings outOf="5">
<numUsers vote="5">13411</numUsers> <!-- The number of users who voted 5 out of 5 -->
<numUsers vote="4">14794</numUsers> <!-- The number of users who voted 4 out of 5 -->
<numUsers vote="3">5359</numUsers> <!-- etc. -->
<numUsers vote="2">974</numUsers>
<numUsers vote="1">361</numUsers>
<!-- total = 34899 -->
</userRatings>
</book>
<book>
<title>Gone Girl</title>
<author>Gillian Flynn</author>
<userRatings outOf="5">
<numUsers vote="5">95595</numUsers>
<numUsers vote="4">140217</numUsers>
<numUsers vote="3">61642</numUsers>
<numUsers vote="2">16923</numUsers>
<numUsers vote="1">6225</numUsers>
<!-- total = 320602 -->
</userRatings>
</book>
</ratings>
这是我到目前为止所做的:
<average>
<xsl:for-each select="document('ratings.xml')/ratings/book[title = $compareTitles]">
<xsl:value-of select="sum(userRatings/numUsers * userRatings/numUsers/@vote) div sum(userRatings/numUsers)"/>
</xsl:for-each>
</average>
我尝试使用变量,但发现你不能操纵一个已定义的变量,并且在声明时它是固定的。
修改
<average>
<xsl:for-each select="document('ratings.xml')/ratings/book[title = $compareTitles]">
<xsl:value-of select="format-number(((userRatings/numUsers[@vote = '1'] * 1) +
(userRatings/numUsers[@vote = '2'] * 2) +
(userRatings/numUsers[@vote = '3'] * 3) +
(userRatings/numUsers[@vote = '4'] * 4) +
(userRatings/numUsers[@vote = '5'] * 5))
div (userRatings/numUsers[@vote = '5'] +
userRatings/numUsers[@vote = '4'] +
userRatings/numUsers[@vote = '3'] +
userRatings/numUsers[@vote = '2'] +
userRatings/numUsers[@vote = '1']),'0.##')"/>
</xsl:for-each>
</average>