如何使用xslt:evaluate()来计算表达式

时间:2017-11-06 12:34:31

标签: xml xslt

我有一个示例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>10.90</price>
        <additionalprice>12.90</additionalprice>
        <currencyvalue>60</currencyvalue>
        <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>
        <additionalprice>10.90</additionalprice>
        <currencyvalue>68</currencyvalue>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <additionalprice>30.90</additionalprice>
        <currencyvalue>80</currencyvalue>
        <year>1982</year>
    </cd>
    <cd>
        <title>Still got the blues</title>
        <artist>Gary Moore</artist>
        <country>UK</country>
        <company>Virgin records</company>
        <price>10.20</price>
        <additionalprice>15.90</additionalprice>
        <currencyvalue>90</currencyvalue>
        <year>1990</year>
    </cd>
    <cd>
        <title>Eros</title>
        <artist>Eros Ramazzotti</artist>
        <country>EU</country>
        <company>BMG</company>
        <price>9.90</price>
        <additionalprice>11.90</additionalprice>
        <currencyvalue>20</currencyvalue>
        <year>1997</year>
    </cd>
</catalog>

我的XSLT如下所示:

我想介绍另一个名为&#34; Total Price&#34;等于 价格+(additionalPrice * currencyvalue)。这只是我的随机表达。我想为此使用xslt:evaluate()。虽然可能有更好的方法,但我想练习评估以理解相同的方法。谁能告诉我怎么能这样做......

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.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>
                    </tr>
                    <xsl:for-each select="catalog/cd">
                        <tr>
                            <td>
                                <xsl:value-of select="title"/>
                            </td>
                            <td>
                                <xsl:value-of select="artist"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

0 个答案:

没有答案