使用xslt-3为元素添加数值

时间:2018-03-05 14:35:41

标签: xslt-3.0

如果我有一个像下面这样的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>

我如何为每个价格元素添加1000的值,所以有一个像当前值+ 1000的总和,使用xslt-3? 输出:

<?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>

1 个答案:

答案 0 :(得分:1)

如果您为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>

https://xsltfiddle.liberty-development.net/6qM2e2j