使用 xslt-3 向元素添加数值
add a numeric value to an element using xslt-3
如果我有如下 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<CATALOG>
<CD ID="1">
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD ID="2">
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>31</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG>
我如何使用 xslt-3 将 1000 的值添加到每个价格元素,从而得到类似于当前值 + 1000 的总和?
输出:
<?xml version="1.0" encoding="UTF-8"?>
<CATALOG>
<CD ID="1001">
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>1010</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD ID="1002">
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>1031</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG>
如果您为 PRICE
元素编写模板并通过 xsl:mode on-no-match="shallow-copy"
(https://www.w3.org/TR/xslt-30/#built-in-templates-shallow-copy) 处理其余部分,您将得到
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="3.0">
<xsl:param name="to-be-added" as="xs:decimal" select="1000"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="PRICE">
<xsl:copy>
<xsl:value-of select=". + $to-be-added"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
如果我有如下 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<CATALOG>
<CD ID="1">
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD ID="2">
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>31</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG>
我如何使用 xslt-3 将 1000 的值添加到每个价格元素,从而得到类似于当前值 + 1000 的总和? 输出:
<?xml version="1.0" encoding="UTF-8"?>
<CATALOG>
<CD ID="1001">
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>1010</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD ID="1002">
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>1031</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG>
如果您为 PRICE
元素编写模板并通过 xsl:mode on-no-match="shallow-copy"
(https://www.w3.org/TR/xslt-30/#built-in-templates-shallow-copy) 处理其余部分,您将得到
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="3.0">
<xsl:param name="to-be-added" as="xs:decimal" select="1000"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="PRICE">
<xsl:copy>
<xsl:value-of select=". + $to-be-added"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>