Я снова и снова теряюсь из-за ваших именованных шаблонов. Но так как это ваш стиль, я внёс коррективы в вашу таблицу стилей, как вы уже догадались, правильно добавив именованные шаблоны.
Кажется, что суть вашей проблемы заключается в том, что xRate
в использовать. Пожалуйста, следуйте комментариям для объяснения.
<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:strip-space elements="*"/>
<xsl:key name="amt-by-rate" match="Amounts" use="VATRate/*" />
<xsl:variable name="Curr" select="//Header/Currency" />
<xsl:decimal-format name="decFormat" decimal-separator="." grouping-separator="," NaN="0.00"/>
<xsl:decimal-format name="XRFormat" decimal-separator="." grouping-separator="," NaN="1.00"/>
<xsl:template match="Document">
<Invoice>
<xsl:apply-templates select ="Header" />
<xsl:apply-templates select ="InvoiceLines" />
<xsl:call-template name="Summary" />
</Invoice>
</xsl:template>
<xsl:template match="InvoiceLines">
<InvoiceDetails>
<xsl:apply-templates select ="Lines" />
</InvoiceDetails>
</xsl:template>
<xsl:template match ="Lines">
<!-- added an xRate variable here -->
<xsl:variable name="xRate">
<xsl:choose>
<xsl:when test="Extra/ExchangeRate!=''">
<xsl:value-of select ="format-number(Extra/ExchangeRate,'#0.000','XRFormat')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number(//Header/ExchangeRate,'#0.000','XRFormat')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<InvLine>
<Number>
<xsl:value-of select="LineNumber"/>
</Number>
<UnitPrice>
<xsl:value-of select="Amount"/>
</UnitPrice>
<Quantity>
<xsl:value-of select="Quantity"/>
</Quantity>
<Amounts>
<xsl:copy-of select="Amounts/VATRate"/>
<xsl:call-template name="Amounts">
<xsl:with-param name="SectionName" select="'LineAmountData'" />
<xsl:with-param name="FieldName" select="'LineAmount'" />
<xsl:with-param name="Data" select="Amounts/Net" />
<xsl:with-param name="xRate" select="$xRate" />
</xsl:call-template>
<xsl:call-template name="Amounts">
<xsl:with-param name="SectionName" select="'LineVATData'" />
<xsl:with-param name="FieldName" select="'VATAmount'" />
<xsl:with-param name="Data" select="Amounts/VATAmount" />
<xsl:with-param name="xRate" select="$xRate" />
</xsl:call-template>
<xsl:call-template name="Amounts">
<xsl:with-param name="SectionName" select="'LineGrossData'" />
<xsl:with-param name="FieldName" select="'GrossAmount'" />
<xsl:with-param name="Data" select="Amounts/Gross" />
<xsl:with-param name="xRate" select="$xRate" />
</xsl:call-template>
</Amounts>
</InvLine>
</xsl:template>
<xsl:template name="Summary">
<InvoiceSummary>
<xsl:variable name="amounts" select="InvoiceLines/Lines/Amounts" />
<Summary>
<!-- group for each unique VATRate percentage -->
<xsl:for-each select="$amounts[generate-id(.)=generate-id(key('amt-by-rate', VATRate/*)[1])]">
<!-- store each value of Net according to xRate -->
<xsl:variable name="each_Net">
<xsl:call-template name="xRate_times_net">
<xsl:with-param name="curr_VATRate" select="VATRate/*"/>
</xsl:call-template>
</xsl:variable>
<!-- convert the string to get a total value -->
<xsl:variable name="Net">
<xsl:call-template name="getCount">
<xsl:with-param name="str" select="$each_Net"/>
<xsl:with-param name="delimiter" select="','"/>
</xsl:call-template>
</xsl:variable>
<!-- same approach as above -->
<xsl:variable name="each_VAT">
<xsl:call-template name="xRate_times_VAT">
<xsl:with-param name="curr_VATRate" select="VATRate/*"/>
</xsl:call-template>
</xsl:variable>
<!-- same approach as above -->
<xsl:variable name="VAT">
<xsl:call-template name="getCount">
<xsl:with-param name="str" select="$each_VAT"/>
<xsl:with-param name="delimiter" select="','"/>
</xsl:call-template>
</xsl:variable>
<!-- same approach as above -->
<xsl:variable name="each_Gross">
<xsl:call-template name="xRate_times_Gross">
<xsl:with-param name="curr_VATRate" select="VATRate/*"/>
</xsl:call-template>
</xsl:variable>
<!-- same approach as above -->
<xsl:variable name="Gross">
<xsl:call-template name="getCount">
<xsl:with-param name="str" select="$each_Gross"/>
<xsl:with-param name="delimiter" select="','"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="current-group" select="key('amt-by-rate', VATRate/*)" />
<Rate>
<xsl:copy-of select="VATRate"/>
<!-- this is a called template for grouped Summary -->
<xsl:call-template name="Amounts_Summary">
<xsl:with-param name="SectionName" select="'NetAmountData'" />
<xsl:with-param name="FieldName" select="'NetAmount'" />
<xsl:with-param name="Data" select="sum($current-group/Net)" />
<xsl:with-param name="Data_n_xRate" select="$Net" />
</xsl:call-template>
<xsl:call-template name="Amounts_Summary">
<xsl:with-param name="SectionName" select="'VATAmountData'" />
<xsl:with-param name="FieldName" select="'VATAmount'" />
<xsl:with-param name="Data" select="sum($current-group/VATAmount)" />
<xsl:with-param name="Data_n_xRate" select="$VAT" />
</xsl:call-template>
<xsl:call-template name="Amounts_Summary">
<xsl:with-param name="SectionName" select="'GrossAmountData'" />
<xsl:with-param name="FieldName" select="'GrossAmount'" />
<xsl:with-param name="Data" select="sum($current-group/Gross)" />
<xsl:with-param name="Data_n_xRate" select="$Gross" />
</xsl:call-template>
</Rate>
</xsl:for-each>
</Summary>
<!-- the following shows the templates for the overall summary -->
<xsl:variable name="all_Net">
<xsl:for-each select="//Amounts/Net">
<xsl:variable name="xRate">
<xsl:choose>
<xsl:when test="../following-sibling::Extra/ExchangeRate!=''">
<xsl:value-of select ="format-number(../following-sibling::Extra/ExchangeRate,'#0.000','XRFormat')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number(//Header/ExchangeRate,'#0.000','XRFormat')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="position()>1">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:value-of select=".*$xRate"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="sum_Net">
<xsl:call-template name="getCount">
<xsl:with-param name="str" select="$all_Net"/>
<xsl:with-param name="delimiter" select="','"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="all_VAT">
<xsl:for-each select="//Amounts/VATAmount">
<xsl:variable name="xRate">
<xsl:choose>
<xsl:when test="../following-sibling::Extra/ExchangeRate!=''">
<xsl:value-of select ="format-number(../following-sibling::Extra/ExchangeRate,'#0.000','XRFormat')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number(//Header/ExchangeRate,'#0.000','XRFormat')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="position()>1">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:value-of select=".*$xRate"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="sum_VAT">
<xsl:call-template name="getCount">
<xsl:with-param name="str" select="$all_VAT"/>
<xsl:with-param name="delimiter" select="','"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="all_Gross">
<xsl:for-each select="//Amounts/Gross">
<xsl:variable name="xRate">
<xsl:choose>
<xsl:when test="../following-sibling::Extra/ExchangeRate!=''">
<xsl:value-of select ="format-number(../following-sibling::Extra/ExchangeRate,'#0.000','XRFormat')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number(//Header/ExchangeRate,'#0.000','XRFormat')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="position()>1">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:value-of select=".*$xRate"/>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="sum_Gross">
<xsl:call-template name="getCount">
<xsl:with-param name="str" select="$all_Gross"/>
<xsl:with-param name="delimiter" select="','"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="Amounts_Summary">
<xsl:with-param name="SectionName" select="'InvoiceNetAmountData'" />
<xsl:with-param name="FieldName" select="'InvoiceNetAmount'" />
<xsl:with-param name="Data" select="sum($amounts/Net)" />
<xsl:with-param name="Data_n_xRate" select="$sum_Net" />
</xsl:call-template>
<xsl:call-template name="Amounts_Summary">
<xsl:with-param name="SectionName" select="'InvoiceVATAmountData'" />
<xsl:with-param name="FieldName" select="'InvoiceVATAmount'" />
<xsl:with-param name="Data" select="sum($amounts/VATAmount)" />
<xsl:with-param name="Data_n_xRate" select="$sum_VAT" />
</xsl:call-template>
<xsl:call-template name="Amounts_Summary">
<xsl:with-param name="SectionName" select="'InvoiceGrossAmountData'" />
<xsl:with-param name="FieldName" select="'InvoiceGrossAmount'" />
<xsl:with-param name="Data" select="sum($amounts/Gross)" />
<xsl:with-param name="Data_n_xRate" select="$sum_Gross" />
</xsl:call-template>
</InvoiceSummary>
</xsl:template>
<xsl:template match="Header">
<InvoiceHeader>
<Name>
<xsl:value-of select="concat(FirstName,' ',LastName)" />
</Name>
<InvoiceDate>
<xsl:value-of select="InvoiceDate" />
</InvoiceDate>
<InvoiceNumber>
<xsl:value-of select="InvoiceNumber" />
</InvoiceNumber>
</InvoiceHeader>
</xsl:template>
<xsl:template name ="Amounts">
<xsl:param name="SectionName" />
<xsl:param name="FieldName" />
<xsl:param name="DecimalPrecision" />
<xsl:param name="Data" />
<xsl:param name="xRate" />
<xsl:choose>
<xsl:when test="$SectionName!=''">
<xsl:element name="{$SectionName}">
<xsl:element name="{$FieldName}">
<xsl:value-of select="format-number($Data, '#0.00' ,'decFormat')" />
</xsl:element>
<xsl:element name="{$FieldName}PHP">
<xsl:value-of select="format-number($Data * $xRate, '#0.00','decFormat')" />
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{$FieldName}">
<xsl:value-of select="format-number($Data, '#0.00','decFormat')" />
</xsl:element>
<xsl:element name="{$FieldName}PHP">
<xsl:value-of select="format-number($Data * $xRate, '#0.00','decFormat')" />
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name ="Amounts_Summary">
<xsl:param name="SectionName" />
<xsl:param name="FieldName" />
<xsl:param name="DecimalPrecision" />
<xsl:param name="Data" />
<xsl:param name="Data_n_xRate" />
<xsl:choose>
<xsl:when test="$SectionName!=''">
<xsl:element name="{$SectionName}">
<xsl:element name="{$FieldName}">
<xsl:value-of select="format-number($Data, '#0.00' ,'decFormat')" />
</xsl:element>
<xsl:element name="{$FieldName}PHP">
<xsl:value-of select="format-number($Data_n_xRate, '#0.00','decFormat')" />
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{$FieldName}">
<xsl:value-of select="format-number($Data, '#0.00','decFormat')" />
</xsl:element>
<xsl:element name="{$FieldName}PHP">
<xsl:value-of select="format-number($Data_n_xRate, '#0.00','decFormat')" />
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="xRate_times_net">
<xsl:param name="curr_VATRate"/>
<xsl:for-each select="key('amt-by-rate', $curr_VATRate)">
<xsl:variable name="xRate">
<xsl:choose>
<xsl:when test="following-sibling::Extra/ExchangeRate!=''">
<xsl:value-of select ="format-number(following-sibling::Extra/ExchangeRate,'#0.000','XRFormat')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number(//Header/ExchangeRate,'#0.000','XRFormat')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="position()>1">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:value-of select="Net*$xRate"/>
</xsl:for-each>
</xsl:template>
<xsl:template name="xRate_times_VAT">
<xsl:param name="curr_VATRate"/>
<xsl:for-each select="key('amt-by-rate', VATRate/*)">
<xsl:variable name="xRate">
<xsl:choose>
<xsl:when test="following-sibling::Extra/ExchangeRate!=''">
<xsl:value-of select ="format-number(following-sibling::Extra/ExchangeRate,'#0.000','XRFormat')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number(//Header/ExchangeRate,'#0.000','XRFormat')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="position()>1">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:value-of select="VATAmount*$xRate"/>
</xsl:for-each>
</xsl:template>
<xsl:template name="xRate_times_Gross">
<xsl:param name="curr_VATRate"/>
<xsl:for-each select="key('amt-by-rate', VATRate/*)">
<xsl:variable name="xRate">
<xsl:choose>
<xsl:when test="following-sibling::Extra/ExchangeRate!=''">
<xsl:value-of select ="format-number(following-sibling::Extra/ExchangeRate,'#0.000','XRFormat')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number(//Header/ExchangeRate,'#0.000','XRFormat')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:if test="position()>1">
<xsl:text>,</xsl:text>
</xsl:if>
<xsl:value-of select="Gross*$xRate"/>
</xsl:for-each>
</xsl:template>
<xsl:template name="getCount" >
<xsl:param name="str" />
<xsl:param name="delimiter" />
<xsl:param name="summation" select="0" />
<xsl:choose>
<xsl:when test="contains($str,$delimiter)">
<xsl:variable name="beforecomma" select="substring-before($str,$delimiter)" />
<xsl:variable name="aftercomma" select="substring-after($str,$delimiter)" />
<xsl:call-template name="getCount">
<xsl:with-param name="str" select="$aftercomma" />
<xsl:with-param name="delimiter" select="$delimiter" />
<xsl:with-param name="summation" select="$summation + $beforecomma" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$summation + $str" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Смотрите это работает здесь: http://xsltransform.net/93FbieK