数据为例
<Value>YQ: LH YQ surcharge: 10.00 EUR; GB: United Kingdom Air Passenger Duty APD: 15.46 EUR; RA: Germany Passenger Service Charge International Departure: 26.59 EUR; DE: Germany Airport Security Charge: 9.50 EUR; OY: Germany Air Transport Tax: 7.38 EUR; UB: United Kingdom Passenger Service Charge Departures: 20.70 EUR</Value>
预期结果 89.63
您需要将输入的内容标记为单个费用,从每个标记中提取数字金额,并将它们相加。在XSLT 1.0中,这需要一个递归模板。
XSLT 1. 0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="Value">
<total>
<xsl:call-template name="total">
<xsl:with-param name="string" select="concat(., '; ')"/>
</xsl:call-template>
</total>
</xsl:template>
<xsl:template name="total">
<xsl:param name="string"/>
<xsl:param name="sum" select="0"/>
<xsl:choose>
<xsl:when test="contains($string, ';')">
<xsl:variable name="token" select="substring-before($string, '; ')" />
<xsl:variable name="amount" select="substring-before(substring-after(substring-after($token, ': '), ': '), ' EUR')" />
<xsl:call-template name="total">
<xsl:with-param name="string" select="substring-after($string, '; ')" />
<xsl:with-param name="sum" select="$sum + $amount" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$sum"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>