问:
Problem with computation based on Grouping in XSLT
I have a situation here where I need to get a value from a node is that exists and use that for the computation of the values.

Here is my XML:

```<code><?xml version="1.0" encoding="utf-8"?>
<Document>
  <Header>
    <FirstName>Joel</FirstName>
    <LastName>Cabarles</LastName>
    <InvoiceDate>01-JAN-2020</InvoiceDate>
    <InvoiceNumber>123</InvoiceNumber>
    <Currency>PHP</Currency>
    <ExchangeRate>1.5</ExchangeRate>
  </Header>
  <InvoiceLines>
    <Lines>
      <LineNumber>1</LineNumber>
      <Description>Test1</Description>
      <Quantity>200</Quantity>
      <UnitPrice>500</UnitPrice>
      <Amounts>
        <Net>100000</Net>
        <VATRate>
          <Percent>2</Percent>
        </VATRate>
        <VATAmount>2000</VATAmount>
        <Gross>102000</Gross>
      </Amounts>
      <Extra>
        <ExchangeRate>1.5</ExchangeRate>
      </Extra>
    </Lines>
    <Lines>
      <LineNumber>2</LineNumber>
      <Description>Test2</Description>
      <Quantity>300</Quantity>
      <UnitPrice>1000</UnitPrice>
      <Amounts>
        <Net>300000</Net>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <VATAmount>9000</VATAmount>
        <Gross>309000</Gross>
      </Amounts>
      <Extra>
        <ExchangeRate>2</ExchangeRate>
      </Extra>
    </Lines>
    <Lines>
      <LineNumber>3</LineNumber>
      <Description>Test3</Description>
      <Quantity>100</Quantity>
      <UnitPrice>500</UnitPrice>
      <Amounts>
        <Net>50000</Net>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <VATAmount>1500</VATAmount>
        <Gross>51500</Gross>
      </Amounts>
      <Extra>
        <ExchangeRate>3</ExchangeRate>
      </Extra>
    </Lines>
    <Lines>
      <LineNumber>4</LineNumber>
      <Description>Test4</Description>
      <Quantity>100</Quantity>
      <UnitPrice>500</UnitPrice>
      <Amounts>
        <Net>50000</Net>
        <VATRate>
          <Excempt>0</Excempt>
        </VATRate>
        <VATAmount>0</VATAmount>
        <Gross>50000</Gross>
      </Amounts>
      <Extra>
        <ExchangeRate>1.2</ExchangeRate>
      </Extra>
    </Lines>
  </InvoiceLines>
</Document>
</code>```

And as you can see there is an ExhangeRate tag in the section Document/Header/ExchangeRate and also inside the section Document/InvoiceLines/Lines/Extra/ExchangeRate

if the section Document/InvoiceLines/Lines/Extra/ExchangeRate is not existing it should always take the value from Document/Header/ExchangeRate

But with my XSLT1.0 below, it always takes the first instance  of the tag Document/InvoiceLines/Lines/Extra/ExchangeRate:

```<code><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">
    <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: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: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:call-template>
      </Amounts>
    </InvLine>
  </xsl:template>

  <xsl:template name="Summary">
    <InvoiceSummary>
      <xsl:variable name="amounts" select="InvoiceLines/Lines/Amounts" />
      <Summary>
        <xsl:for-each select="$amounts[count(. | key('amt-by-rate', VATRate/*)[1]) = 1]">
          <xsl:variable name="current-group" select="key('amt-by-rate', VATRate/*)" />
          <Rate>
            <xsl:copy-of select="VATRate"/>
            <xsl:call-template name="Amounts">
              <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:call-template>
            <xsl:call-template name="Amounts">
              <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:call-template>
            <xsl:call-template name="Amounts">
              <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:call-template>
          </Rate>
        </xsl:for-each>
      </Summary>
      <xsl:call-template name="Amounts">
        <xsl:with-param name="SectionName" select="'InvoiceNetAmountData'" />
        <xsl:with-param name="FieldName" select="'InvoiceNetAmount'" />
        <xsl:with-param name="Data" select="sum($amounts/Net)" />
      </xsl:call-template>
      <xsl:call-template name="Amounts">
        <xsl:with-param name="SectionName" select="'InvoiceVATAmountData'" />
        <xsl:with-param name="FieldName" select="'InvoiceVATAmount'" />
        <xsl:with-param name="Data" select="sum($amounts/VATAmount)" />
      </xsl:call-template>
      <xsl:call-template name="Amounts">
        <xsl:with-param name="SectionName" select="'InvoiceGrossAmountData'" />
        <xsl:with-param name="FieldName" select="'InvoiceGrossAmount'" />
        <xsl:with-param name="Data" select="sum($amounts/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:variable name="xRate">
      <xsl:choose>
        <xsl:when test="//Extra/ExchangeRate[position()]!=''">
          <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>

    <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:stylesheet>
</code>```

Here is the supposed to be result and the current wrong result of the lines and summary section:

Can anyone please help me to identify what is wrong with this.

Thanks in advance, and by the way I need to use XSLT 1.0.

Here is the current XML result:

```<code><?xml version="1.0" encoding="utf-8"?>
<Invoice>
  <InvoiceHeader>
    <Name>Joel Cabarles</Name>
    <InvoiceDate>01-JAN-2020</InvoiceDate>
    <InvoiceNumber>123</InvoiceNumber>
  </InvoiceHeader>
  <InvoiceDetails>
    <InvLine>
      <Number>1</Number>
      <UnitPrice></UnitPrice>
      <Quantity>200</Quantity>
      <Amounts>
        <VATRate>
          <Percent>2</Percent>
        </VATRate>
        <LineAmountData>
          <LineAmount>100000.00</LineAmount>
          <LineAmountPHP>150000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>2000.00</VATAmount>
          <VATAmountPHP>3000.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>102000.00</GrossAmount>
          <GrossAmountPHP>153000.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
    <InvLine>
      <Number>2</Number>
      <UnitPrice></UnitPrice>
      <Quantity>300</Quantity>
      <Amounts>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <LineAmountData>
          <LineAmount>300000.00</LineAmount>
          <LineAmountPHP>450000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>9000.00</VATAmount>
          <VATAmountPHP>13500.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>309000.00</GrossAmount>
          <GrossAmountPHP>463500.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
    <InvLine>
      <Number>3</Number>
      <UnitPrice></UnitPrice>
      <Quantity>100</Quantity>
      <Amounts>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <LineAmountData>
          <LineAmount>50000.00</LineAmount>
          <LineAmountPHP>75000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>1500.00</VATAmount>
          <VATAmountPHP>2250.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>51500.00</GrossAmount>
          <GrossAmountPHP>77250.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
    <InvLine>
      <Number>4</Number>
      <UnitPrice></UnitPrice>
      <Quantity>100</Quantity>
      <Amounts>
        <VATRate>
          <Excempt>0</Excempt>
        </VATRate>
        <LineAmountData>
          <LineAmount>50000.00</LineAmount>
          <LineAmountPHP>75000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>0.00</VATAmount>
          <VATAmountPHP>0.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>50000.00</GrossAmount>
          <GrossAmountPHP>75000.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
  </InvoiceDetails>
  <InvoiceSummary>
    <Summary>
      <Rate>
        <VATRate>
          <Percent>2</Percent>
        </VATRate>
        <NetAmountData>
          <NetAmount>100000.00</NetAmount>
          <NetAmountPHP>150000.00</NetAmountPHP>
        </NetAmountData>
        <VATAmountData>
          <VATAmount>2000.00</VATAmount>
          <VATAmountPHP>3000.00</VATAmountPHP>
        </VATAmountData>
        <GrossAmountData>
          <GrossAmount>102000.00</GrossAmount>
          <GrossAmountPHP>153000.00</GrossAmountPHP>
        </GrossAmountData>
      </Rate>
      <Rate>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <NetAmountData>
          <NetAmount>350000.00</NetAmount>
          <NetAmountPHP>525000.00</NetAmountPHP>
        </NetAmountData>
        <VATAmountData>
          <VATAmount>10500.00</VATAmount>
          <VATAmountPHP>15750.00</VATAmountPHP>
        </VATAmountData>
        <GrossAmountData>
          <GrossAmount>360500.00</GrossAmount>
          <GrossAmountPHP>540750.00</GrossAmountPHP>
        </GrossAmountData>
      </Rate>
      <Rate>
        <VATRate>
          <Excempt>0</Excempt>
        </VATRate>
        <NetAmountData>
          <NetAmount>50000.00</NetAmount>
          <NetAmountPHP>75000.00</NetAmountPHP>
        </NetAmountData>
        <VATAmountData>
          <VATAmount>0.00</VATAmount>
          <VATAmountPHP>0.00</VATAmountPHP>
        </VATAmountData>
        <GrossAmountData>
          <GrossAmount>50000.00</GrossAmount>
          <GrossAmountPHP>75000.00</GrossAmountPHP>
        </GrossAmountData>
      </Rate>
    </Summary>
    <InvoiceNetAmountData>
      <InvoiceNetAmount>500000.00</InvoiceNetAmount>
      <InvoiceNetAmountPHP>750000.00</InvoiceNetAmountPHP>
    </InvoiceNetAmountData>
    <InvoiceVATAmountData>
      <InvoiceVATAmount>12500.00</InvoiceVATAmount>
      <InvoiceVATAmountPHP>18750.00</InvoiceVATAmountPHP>
    </InvoiceVATAmountData>
    <InvoiceGrossAmountData>
      <InvoiceGrossAmount>512500.00</InvoiceGrossAmount>
      <InvoiceGrossAmountPHP>768750.00</InvoiceGrossAmountPHP>
    </InvoiceGrossAmountData>
  </InvoiceSummary>
</Invoice>
</code>```

And here is the expected output:

```<code><?xml version="1.0" encoding="utf-8"?>
<Invoice>
  <InvoiceHeader>
    <Name>Joel Cabarles</Name>
    <InvoiceDate>01-JAN-2020</InvoiceDate>
    <InvoiceNumber>123</InvoiceNumber>
  </InvoiceHeader>
  <InvoiceDetails>
    <InvLine>
      <Number>1</Number>
      <UnitPrice></UnitPrice>
      <Quantity>200</Quantity>
      <Amounts>
        <VATRate>
          <Percent>2</Percent>
        </VATRate>
        <LineAmountData>
          <LineAmount>100000.00</LineAmount>
          <LineAmountPHP>150000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>2000.00</VATAmount>
          <VATAmountPHP>3000.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>102000.00</GrossAmount>
          <GrossAmountPHP>153000.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
    <InvLine>
      <Number>2</Number>
      <UnitPrice></UnitPrice>
      <Quantity>300</Quantity>
      <Amounts>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <LineAmountData>
          <LineAmount>300000.00</LineAmount>
          <LineAmountPHP>600000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>9000.00</VATAmount>
          <VATAmountPHP>18000.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>309000.00</GrossAmount>
          <GrossAmountPHP>618000.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
    <InvLine>
      <Number>3</Number>
      <UnitPrice></UnitPrice>
      <Quantity>100</Quantity>
      <Amounts>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <LineAmountData>
          <LineAmount>50000.00</LineAmount>
          <LineAmountPHP>150000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>1500.00</VATAmount>
          <VATAmountPHP>4500.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>51500.00</GrossAmount>
          <GrossAmountPHP>154500.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
    <InvLine>
      <Number>4</Number>
      <UnitPrice></UnitPrice>
      <Quantity>100</Quantity>
      <Amounts>
        <VATRate>
          <Excempt>0</Excempt>
        </VATRate>
        <LineAmountData>
          <LineAmount>50000.00</LineAmount>
          <LineAmountPHP>60000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>0.00</VATAmount>
          <VATAmountPHP>0.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>50000.00</GrossAmount>
          <GrossAmountPHP>60000.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
  </InvoiceDetails>
  <InvoiceSummary>
    <Summary>
      <Rate>
        <VATRate>
          <Percent>2</Percent>
        </VATRate>
        <NetAmountData>
          <NetAmount>100000.00</NetAmount>
          <NetAmountPHP>150000.00</NetAmountPHP>
        </NetAmountData>
        <VATAmountData>
          <VATAmount>2000.00</VATAmount>
          <VATAmountPHP>3000.00</VATAmountPHP>
        </VATAmountData>
        <GrossAmountData>
          <GrossAmount>102000.00</GrossAmount>
          <GrossAmountPHP>153000.00</GrossAmountPHP>
        </GrossAmountData>
      </Rate>
      <Rate>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <NetAmountData>
          <NetAmount>350000.00</NetAmount>
          <NetAmountPHP>750000.00</NetAmountPHP>
        </NetAmountData>
        <VATAmountData>
          <VATAmount>10500.00</VATAmount>
          <VATAmountPHP>22500.00</VATAmountPHP>
        </VATAmountData>
        <GrossAmountData>
          <GrossAmount>360500.00</GrossAmount>
          <GrossAmountPHP>772500.00</GrossAmountPHP>
        </GrossAmountData>
      </Rate>
      <Rate>
        <VATRate>
          <Excempt>0</Excempt>
        </VATRate>
        <NetAmountData>
          <NetAmount>50000.00</NetAmount>
          <NetAmountPHP>60000.00</NetAmountPHP>
        </NetAmountData>
        <VATAmountData>
          <VATAmount>0.00</VATAmount>
          <VATAmountPHP>0.00</VATAmountPHP>
        </VATAmountData>
        <GrossAmountData>
          <GrossAmount>50000.00</GrossAmount>
          <GrossAmountPHP>60000.00</GrossAmountPHP>
        </GrossAmountData>
      </Rate>
    </Summary>
    <InvoiceNetAmountData>
      <InvoiceNetAmount>500000.00</InvoiceNetAmount>
      <InvoiceNetAmountPHP>960000.00</InvoiceNetAmountPHP>
    </InvoiceNetAmountData>
    <InvoiceVATAmountData>
      <InvoiceVATAmount>12500.00</InvoiceVATAmount>
      <InvoiceVATAmountPHP>25500.00</InvoiceVATAmountPHP>
    </InvoiceVATAmountData>
    <InvoiceGrossAmountData>
      <InvoiceGrossAmount>512500.00</InvoiceGrossAmount>
      <InvoiceGrossAmountPHP>985500.00</InvoiceGrossAmountPHP>
    </InvoiceGrossAmountData>
  </InvoiceSummary>
</Invoice>
</code>```

答:
I was lost time and again following your named templates. But because it is your style, I have made adjustments to your stylesheet by, you've guessed it right, adding named templates yet again.

It seems that the core of your problem lies on which xRate to use. Please follow the comments for explanations.

```<code><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>
</code>```

See it working here: http://xsltransform.net/93FbieK

Body:
I have a situation here where I need to get a value from a node is that exists and use that for the computation of the values.

Here is my XML:

<?xml version="1.0" encoding="utf-8"?>
<Document>

    <FirstName>Joel</FirstName>
    <LastName>Cabarles</LastName>
    <InvoiceDate>01-JAN-2020</InvoiceDate>
    <InvoiceNumber>123</InvoiceNumber>
    <Currency>PHP</Currency>
    <ExchangeRate>1.5</ExchangeRate>

  <InvoiceLines>
    <Lines>
      <LineNumber>1</LineNumber>
      <Description>Test1</Description>
      <Quantity>200</Quantity>
      <UnitPrice>500</UnitPrice>
      <Amounts>
        <Net>100000</Net>
        <VATRate>
          <Percent>2</Percent>
        </VATRate>
        <VATAmount>2000</VATAmount>
        <Gross>102000</Gross>
      </Amounts>
      <Extra>
        <ExchangeRate>1.5</ExchangeRate>
      </Extra>
    </Lines>
    <Lines>
      <LineNumber>2</LineNumber>
      <Description>Test2</Description>
      <Quantity>300</Quantity>
      <UnitPrice>1000</UnitPrice>
      <Amounts>
        <Net>300000</Net>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <VATAmount>9000</VATAmount>
        <Gross>309000</Gross>
      </Amounts>
      <Extra>
        <ExchangeRate>2</ExchangeRate>
      </Extra>
    </Lines>
    <Lines>
      <LineNumber>3</LineNumber>
      <Description>Test3</Description>
      <Quantity>100</Quantity>
      <UnitPrice>500</UnitPrice>
      <Amounts>
        <Net>50000</Net>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <VATAmount>1500</VATAmount>
        <Gross>51500</Gross>
      </Amounts>
      <Extra>
        <ExchangeRate>3</ExchangeRate>
      </Extra>
    </Lines>
    <Lines>
      <LineNumber>4</LineNumber>
      <Description>Test4</Description>
      <Quantity>100</Quantity>
      <UnitPrice>500</UnitPrice>
      <Amounts>
        <Net>50000</Net>
        <VATRate>
          <Excempt>0</Excempt>
        </VATRate>
        <VATAmount>0</VATAmount>
        <Gross>50000</Gross>
      </Amounts>
      <Extra>
        <ExchangeRate>1.2</ExchangeRate>
      </Extra>
    </Lines>
  </InvoiceLines>
</Document>

And as you can see there is an ExhangeRate tag in the section Document/Header/ExchangeRate and also inside the section Document/InvoiceLines/Lines/Extra/ExchangeRate

if the section Document/InvoiceLines/Lines/Extra/ExchangeRate is not existing it should always take the value from Document/Header/ExchangeRate

But with my XSLT1.0 below, it always takes the first instance  of the tag Document/InvoiceLines/Lines/Extra/ExchangeRate:

<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">
    <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: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: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:call-template>
      </Amounts>
    </InvLine>
  </xsl:template>

  <xsl:template name="Summary">
    <InvoiceSummary>
      <xsl:variable name="amounts" select="InvoiceLines/Lines/Amounts" />

        <xsl:for-each select="$amounts[count(. | key('amt-by-rate', VATRate/*)[1]) = 1]">
          <xsl:variable name="current-group" select="key('amt-by-rate', VATRate/*)" />
          <Rate>
            <xsl:copy-of select="VATRate"/>
            <xsl:call-template name="Amounts">
              <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:call-template>
            <xsl:call-template name="Amounts">
              <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:call-template>
            <xsl:call-template name="Amounts">
              <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:call-template>
          </Rate>
        </xsl:for-each>

      <xsl:call-template name="Amounts">
        <xsl:with-param name="SectionName" select="'InvoiceNetAmountData'" />
        <xsl:with-param name="FieldName" select="'InvoiceNetAmount'" />
        <xsl:with-param name="Data" select="sum($amounts/Net)" />
      </xsl:call-template>
      <xsl:call-template name="Amounts">
        <xsl:with-param name="SectionName" select="'InvoiceVATAmountData'" />
        <xsl:with-param name="FieldName" select="'InvoiceVATAmount'" />
        <xsl:with-param name="Data" select="sum($amounts/VATAmount)" />
      </xsl:call-template>
      <xsl:call-template name="Amounts">
        <xsl:with-param name="SectionName" select="'InvoiceGrossAmountData'" />
        <xsl:with-param name="FieldName" select="'InvoiceGrossAmount'" />
        <xsl:with-param name="Data" select="sum($amounts/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:variable name="xRate">
      <xsl:choose>
        <xsl:when test="//Extra/ExchangeRate[position()]!=''">
          <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>

    <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:stylesheet>

Here is the supposed to be result and the current wrong result of the lines and summary section:

Can anyone please help me to identify what is wrong with this.

Thanks in advance, and by the way I need to use XSLT 1.0.

Here is the current XML result:

<?xml version="1.0" encoding="utf-8"?>
<Invoice>
  <InvoiceHeader>
    <Name>Joel Cabarles</Name>
    <InvoiceDate>01-JAN-2020</InvoiceDate>
    <InvoiceNumber>123</InvoiceNumber>
  </InvoiceHeader>
  <InvoiceDetails>
    <InvLine>
      <Number>1</Number>
      <UnitPrice></UnitPrice>
      <Quantity>200</Quantity>
      <Amounts>
        <VATRate>
          <Percent>2</Percent>
        </VATRate>
        <LineAmountData>
          <LineAmount>100000.00</LineAmount>
          <LineAmountPHP>150000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>2000.00</VATAmount>
          <VATAmountPHP>3000.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>102000.00</GrossAmount>
          <GrossAmountPHP>153000.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
    <InvLine>
      <Number>2</Number>
      <UnitPrice></UnitPrice>
      <Quantity>300</Quantity>
      <Amounts>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <LineAmountData>
          <LineAmount>300000.00</LineAmount>
          <LineAmountPHP>450000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>9000.00</VATAmount>
          <VATAmountPHP>13500.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>309000.00</GrossAmount>
          <GrossAmountPHP>463500.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
    <InvLine>
      <Number>3</Number>
      <UnitPrice></UnitPrice>
      <Quantity>100</Quantity>
      <Amounts>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <LineAmountData>
          <LineAmount>50000.00</LineAmount>
          <LineAmountPHP>75000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>1500.00</VATAmount>
          <VATAmountPHP>2250.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>51500.00</GrossAmount>
          <GrossAmountPHP>77250.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
    <InvLine>
      <Number>4</Number>
      <UnitPrice></UnitPrice>
      <Quantity>100</Quantity>
      <Amounts>
        <VATRate>
          <Excempt>0</Excempt>
        </VATRate>
        <LineAmountData>
          <LineAmount>50000.00</LineAmount>
          <LineAmountPHP>75000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>0.00</VATAmount>
          <VATAmountPHP>0.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>50000.00</GrossAmount>
          <GrossAmountPHP>75000.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
  </InvoiceDetails>
  <InvoiceSummary>

      <Rate>
        <VATRate>
          <Percent>2</Percent>
        </VATRate>
        <NetAmountData>
          <NetAmount>100000.00</NetAmount>
          <NetAmountPHP>150000.00</NetAmountPHP>
        </NetAmountData>
        <VATAmountData>
          <VATAmount>2000.00</VATAmount>
          <VATAmountPHP>3000.00</VATAmountPHP>
        </VATAmountData>
        <GrossAmountData>
          <GrossAmount>102000.00</GrossAmount>
          <GrossAmountPHP>153000.00</GrossAmountPHP>
        </GrossAmountData>
      </Rate>
      <Rate>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <NetAmountData>
          <NetAmount>350000.00</NetAmount>
          <NetAmountPHP>525000.00</NetAmountPHP>
        </NetAmountData>
        <VATAmountData>
          <VATAmount>10500.00</VATAmount>
          <VATAmountPHP>15750.00</VATAmountPHP>
        </VATAmountData>
        <GrossAmountData>
          <GrossAmount>360500.00</GrossAmount>
          <GrossAmountPHP>540750.00</GrossAmountPHP>
        </GrossAmountData>
      </Rate>
      <Rate>
        <VATRate>
          <Excempt>0</Excempt>
        </VATRate>
        <NetAmountData>
          <NetAmount>50000.00</NetAmount>
          <NetAmountPHP>75000.00</NetAmountPHP>
        </NetAmountData>
        <VATAmountData>
          <VATAmount>0.00</VATAmount>
          <VATAmountPHP>0.00</VATAmountPHP>
        </VATAmountData>
        <GrossAmountData>
          <GrossAmount>50000.00</GrossAmount>
          <GrossAmountPHP>75000.00</GrossAmountPHP>
        </GrossAmountData>
      </Rate>

    <InvoiceNetAmountData>
      <InvoiceNetAmount>500000.00</InvoiceNetAmount>
      <InvoiceNetAmountPHP>750000.00</InvoiceNetAmountPHP>
    </InvoiceNetAmountData>
    <InvoiceVATAmountData>
      <InvoiceVATAmount>12500.00</InvoiceVATAmount>
      <InvoiceVATAmountPHP>18750.00</InvoiceVATAmountPHP>
    </InvoiceVATAmountData>
    <InvoiceGrossAmountData>
      <InvoiceGrossAmount>512500.00</InvoiceGrossAmount>
      <InvoiceGrossAmountPHP>768750.00</InvoiceGrossAmountPHP>
    </InvoiceGrossAmountData>
  </InvoiceSummary>
</Invoice>

And here is the expected output:

<?xml version="1.0" encoding="utf-8"?>
<Invoice>
  <InvoiceHeader>
    <Name>Joel Cabarles</Name>
    <InvoiceDate>01-JAN-2020</InvoiceDate>
    <InvoiceNumber>123</InvoiceNumber>
  </InvoiceHeader>
  <InvoiceDetails>
    <InvLine>
      <Number>1</Number>
      <UnitPrice></UnitPrice>
      <Quantity>200</Quantity>
      <Amounts>
        <VATRate>
          <Percent>2</Percent>
        </VATRate>
        <LineAmountData>
          <LineAmount>100000.00</LineAmount>
          <LineAmountPHP>150000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>2000.00</VATAmount>
          <VATAmountPHP>3000.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>102000.00</GrossAmount>
          <GrossAmountPHP>153000.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
    <InvLine>
      <Number>2</Number>
      <UnitPrice></UnitPrice>
      <Quantity>300</Quantity>
      <Amounts>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <LineAmountData>
          <LineAmount>300000.00</LineAmount>
          <LineAmountPHP>600000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>9000.00</VATAmount>
          <VATAmountPHP>18000.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>309000.00</GrossAmount>
          <GrossAmountPHP>618000.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
    <InvLine>
      <Number>3</Number>
      <UnitPrice></UnitPrice>
      <Quantity>100</Quantity>
      <Amounts>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <LineAmountData>
          <LineAmount>50000.00</LineAmount>
          <LineAmountPHP>150000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>1500.00</VATAmount>
          <VATAmountPHP>4500.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>51500.00</GrossAmount>
          <GrossAmountPHP>154500.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
    <InvLine>
      <Number>4</Number>
      <UnitPrice></UnitPrice>
      <Quantity>100</Quantity>
      <Amounts>
        <VATRate>
          <Excempt>0</Excempt>
        </VATRate>
        <LineAmountData>
          <LineAmount>50000.00</LineAmount>
          <LineAmountPHP>60000.00</LineAmountPHP>
        </LineAmountData>
        <LineVATData>
          <VATAmount>0.00</VATAmount>
          <VATAmountPHP>0.00</VATAmountPHP>
        </LineVATData>
        <LineGrossData>
          <GrossAmount>50000.00</GrossAmount>
          <GrossAmountPHP>60000.00</GrossAmountPHP>
        </LineGrossData>
      </Amounts>
    </InvLine>
  </InvoiceDetails>
  <InvoiceSummary>

      <Rate>
        <VATRate>
          <Percent>2</Percent>
        </VATRate>
        <NetAmountData>
          <NetAmount>100000.00</NetAmount>
          <NetAmountPHP>150000.00</NetAmountPHP>
        </NetAmountData>
        <VATAmountData>
          <VATAmount>2000.00</VATAmount>
          <VATAmountPHP>3000.00</VATAmountPHP>
        </VATAmountData>
        <GrossAmountData>
          <GrossAmount>102000.00</GrossAmount>
          <GrossAmountPHP>153000.00</GrossAmountPHP>
        </GrossAmountData>
      </Rate>
      <Rate>
        <VATRate>
          <Percent>3</Percent>
        </VATRate>
        <NetAmountData>
          <NetAmount>350000.00</NetAmount>
          <NetAmountPHP>750000.00</NetAmountPHP>
        </NetAmountData>
        <VATAmountData>
          <VATAmount>10500.00</VATAmount>
          <VATAmountPHP>22500.00</VATAmountPHP>
        </VATAmountData>
        <GrossAmountData>
          <GrossAmount>360500.00</GrossAmount>
          <GrossAmountPHP>772500.00</GrossAmountPHP>
        </GrossAmountData>
      </Rate>
      <Rate>
        <VATRate>
          <Excempt>0</Excempt>
        </VATRate>
        <NetAmountData>
          <NetAmount>50000.00</NetAmount>
          <NetAmountPHP>60000.00</NetAmountPHP>
        </NetAmountData>
        <VATAmountData>
          <VATAmount>0.00</VATAmount>
          <VATAmountPHP>0.00</VATAmountPHP>
        </VATAmountData>
        <GrossAmountData>
          <GrossAmount>50000.00</GrossAmount>
          <GrossAmountPHP>60000.00</GrossAmountPHP>
        </GrossAmountData>
      </Rate>

    <InvoiceNetAmountData>
      <InvoiceNetAmount>500000.00</InvoiceNetAmount>
      <InvoiceNetAmountPHP>960000.00</InvoiceNetAmountPHP>
    </InvoiceNetAmountData>
    <InvoiceVATAmountData>
      <InvoiceVATAmount>12500.00</InvoiceVATAmount>
      <InvoiceVATAmountPHP>25500.00</InvoiceVATAmountPHP>
    </InvoiceVATAmountData>
    <InvoiceGrossAmountData>
      <InvoiceGrossAmount>512500.00</InvoiceGrossAmount>
      <InvoiceGrossAmountPHP>985500.00</InvoiceGrossAmountPHP>
    </InvoiceGrossAmountData>
  </InvoiceSummary>
</Invoice>

Body:
I was lost time and again following your named templates. But because it is your style, I have made adjustments to your stylesheet by, you've guessed it right, adding named templates yet again.

It seems that the core of your problem lies on which xRate to use. Please follow the comments for explanations.

<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()&gt;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()&gt;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()&gt;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()&gt;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()&gt;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()&gt;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>

See it working here: http://xsltransform.net/93FbieK


来源: stackexchange

问:
Route does not detect the ajax url
I am doing the download and connecting ajax with the controller using controller. However, I am not sure why when I click the button, it only detect controller and ignore ajax url.

Route:

```<code>Route::get('filter-export', 'FiltersController@excel')->name('download');
</code>```

Ajax connected by url 'filter-export':

```<code>$('#download').click(function(){
      var age = $('#age').val();

      ...

    });
</code>```

答:
You are targeting $('#download') for the click event however your  does not have an id so you should add id="download" to the

Also, If you intend the "filter-export" URL to only be used in an ajax then you do not need to add it as the href of .

ContentLicense: CC BY-SA 4.0

Title: Route does not detect the ajax url

Body:
I am doing the download and connecting ajax with the controller using controller. However, I am not sure why when I click the button, it only detect controller and ignore ajax url.

Route:

Route::get('filter-export', 'FiltersController@excel')->name('download');

Ajax connected by url 'filter-export':

$('#download').click(function(){
      var age = $('#age').val();

      ...

    });

Body:
You are targeting $('#download') for the click event however your <a> does not have an id so you should add id="download" to the <a>

Also, If you intend the "filter-export" URL to only be used in an ajax then you do not need to add it as the href of <a>.


问:
How to get input from textarea in html to .ts in Angular
I am a newbie in Angular and now I want to get user input from a textarea but I fail to do so.

Updated: ng-model="num1" > [(ngModel)]="num1"

HTML:

<input [(ngModel)]="num1" type="text" placeholder="Enter value">
     // user enter one number

<input type="text" readonly value={{answer}}>

<button class="result" (click)=onSelectEqual()>=</button>
     // click this button to get the value that user entered
```

<p>.ts:</p>

```num1 : number;
answer: number;

onSelectEqual(){
  this.answer = this.num1 + 1 ;
}
```

答:
In angular, for two-way binding you have to enclose ngModel in [()]. See - https://angular.io/api/forms/NgModel.

<input [(ngModel)]="num1" type="text" placeholder="Enter value">

Body:
I am a newbie in Angular and now I want to get user input from a textarea but I fail to do so.

Updated: ng-model="num1" > [(ngModel)]="num1"

HTML:

<input [(ngModel)]="num1" type="text" placeholder="Enter value">
     // user enter one number

<input type="text" readonly value={{answer}}>

<button class="result" (click)=onSelectEqual()>=</button>
     // click this button to get the value that user entered

.ts:

num1 : number;
answer: number;

onSelectEqual(){
  this.answer = this.num1 + 1 ;
}

Body:
In angular, for two-way binding you have to enclose ngModel in [()]. See - https://angular.io/api/forms/NgModel.

<span><input [(ngModel)]="num1" type="text" placeholder="Enter value"></span>


问:
How to encrypt and decrypt a number using same key?
I need a way to encrypt a number (say 3423234234) using a secret key, which can also be decrypted using that same key.

```<code>const encrypted = encrypt(number, key)
const decrypted = decrypt(encrypted, key)
</code>```

I tried CryptoJS using AES, DES, Rabbit and RC4 algorithms, but it gives me a long encrypted value with special characters.

I want some encrypted value like MongoDB's ObjectId (which contains only alphanumeric characters like 1575866cab3f22f0c8510451f293f405, and should not exceed 20 or 40 characters).

答:
I figured out a way.

```<code>const  decrypt = function (num) {
    const base = "tGVwFAPiJqh3maNHdZfby9eIQXcr2zvgD57YMn8KWCxkSUujpB10sL6oRTl4OE"
    num = num.toString()
    let lastChar, baseConverted = 0, index=0;
    while(num !== ''){
        lastChar = num.slice(-1);
        baseConverted += base.indexOf(lastChar) * Math.pow(62,index);
        index++;
        num = num.slice(0, num.length-1);
    }
    return baseConverted;
};
const  encrypt = function (num) {
    const base = "tGVwFAPiJqh3maNHdZfby9eIQXcr2zvgD57YMn8KWCxkSUujpB10sL6oRTl4OE"
    // console.log(base.length)
    let mod, baseConverted = '';
    while(num){
        mod = num%62;
        baseConverted = base.slice(mod, mod+1)+baseConverted;
        num = Math.floor(num/62);
    }
    return baseConverted;
};

module.exports = {
    encrypt,
    decrypt
}
</code>```

The reason I wanted it because I want to encrypt all the id's of a resource before sharing it with client.

It will decrypt the keys when request is received.

Body:
I need a way to encrypt a number (say 3423234234) using a secret key, which can also be decrypted using that same key.

const encrypted = encrypt(number, key)
const decrypted = decrypt(encrypted, key)

I tried CryptoJS using AES, DES, Rabbit and RC4 algorithms, but it gives me a long encrypted value with special characters.

I want some encrypted value like MongoDB's ObjectId (which contains only alphanumeric characters like 1575866cab3f22f0c8510451f293f405, and should not exceed 20 or 40 characters).

Body:
I figured out a way.

const  decrypt = function (num) {
    const base = "tGVwFAPiJqh3maNHdZfby9eIQXcr2zvgD57YMn8KWCxkSUujpB10sL6oRTl4OE"
    num = num.toString()
    let lastChar, baseConverted = 0, index=0;
    while(num !== ''){
        lastChar = num.slice(-1);
        baseConverted += base.indexOf(lastChar) * Math.pow(62,index);
        index++;
        num = num.slice(0, num.length-1);
    }
    return baseConverted;
};
const  encrypt = function (num) {
    const base = "tGVwFAPiJqh3maNHdZfby9eIQXcr2zvgD57YMn8KWCxkSUujpB10sL6oRTl4OE"
    // console.log(base.length)
    let mod, baseConverted = '';
    while(num){
        mod = num%62;
        baseConverted = base.slice(mod, mod+1)+baseConverted;
        num = Math.floor(num/62);
    }
    return baseConverted;
};

module.exports = {
    encrypt,
    decrypt
}

The reason I wanted it because I want to encrypt all the id's of a resource before sharing it with client.

It will decrypt the keys when request is received.


问:
Capacitor background service, Unable to invoke method
i'm trying to make an android service via capacitor custom plugin.
but i got error.

this is custom capacitor plugin inside android/src/main/java :

it could be called from ionic app.i also set @PluginMethod return type to none.
    package io.ionic.starter;

```<code>import com.getcapacitor.JSObject;
import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;

@NativePlugin()
public class CustomNativePlugin extends Plugin {

    @PluginMethod(returnType=PluginMethod.RETURN_NONE)
    public void customService() {
        MainActivity mainActivity = new MainActivity();
        mainActivity.startService();
        call.resolve();
    }
}
</code>```

android service :
this is MyService

```<code>package io.ionic.starter;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

public class MyService extends Service {
    public MyService() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Let it continue running until it is stopped.
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }
}
</code>```

and from MainActivity i called MyService, startService will be triger but after that i got error.
maybe because of call back function. i dont know.

```<code>  public void startService() {
     startService(new Intent(getBaseContext(), MyService.class));
  }

  // Method to stop the service
  public void stopService(View view) {
    stopService(new Intent(getBaseContext(), MyService.class));
  }
</code>```

call method from ionic app like this:

```<code>import { Plugins } from "@capacitor/core";
const { CustomNativePlugin } = Plugins;
runService(){
    CustomNativePlugin.customService();
}
</code>```

and the error is :

```<code>2020-02-19 16:25:34.988 28206-28338/io.ionic.starter E/Capacitor: Unable to execute plugin method
com.getcapacitor.PluginInvocationException: Unable to invoke method customService on plugin io.ionic.starter.CustomNativePlugin
    at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:101)
    at com.getcapacitor.Bridge$2.run(Bridge.java:537)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.os.HandlerThread.run(HandlerThread.java:65)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:99)
    at com.getcapacitor.Bridge$2.run(Bridge.java:537)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.os.HandlerThread.run(HandlerThread.java:65)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
    at android.content.ComponentName.<init>(ComponentName.java:130)
    at android.content.Intent.<init>(Intent.java:5780)
    at io.ionic.starter.MainActivity.startService(MainActivity.java:26)
    at io.ionic.starter.CustomNativePlugin.customService(CustomNativePlugin.java:37)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:99)
    at com.getcapacitor.Bridge$2.run(Bridge.java:537)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.os.HandlerThread.run(HandlerThread.java:65)
</code>```

答:
because of An Android component (service, receiver, activity) can trigger the execution of a service via the startService(intent) method. i have to call it from context;   </p>

```<code>@PluginMethod(returnType = PluginMethod.RETURN_NONE)
public void customService(PluginCall call) {
    call.resolve();
    Intent service = new Intent(getContext(), MyIntentService.class);
    getContext().startService(service);

}
</code>```

Body:
i'm trying to make an android service via capacitor custom plugin.
but i got error.

this is custom capacitor plugin inside android/src/main/java :

it could be called from ionic app.i also set @PluginMethod return type to none.
    package io.ionic.starter;

import com.getcapacitor.JSObject;
import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;

@NativePlugin()
public class CustomNativePlugin extends Plugin {

    @PluginMethod(returnType=PluginMethod.RETURN_NONE)
    public void customService() {
        MainActivity mainActivity = new MainActivity();
        mainActivity.startService();
        call.resolve();
    }
}

android service :
this is MyService

package io.ionic.starter;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

public class MyService extends Service {
    public MyService() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // Let it continue running until it is stopped.
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }
}

and from MainActivity i called MyService, startService will be triger but after that i got error.
maybe because of call back function. i dont know.

  public void startService() {
     startService(new Intent(getBaseContext(), MyService.class));
  }

  // Method to stop the service
  public void stopService(View view) {
    stopService(new Intent(getBaseContext(), MyService.class));
  }

call method from ionic app like this:

import { Plugins } from "@capacitor/core";
const { CustomNativePlugin } = Plugins;
runService(){
    CustomNativePlugin.customService();
}

and the error is :

2020-02-19 16:25:34.988 28206-28338/io.ionic.starter E/Capacitor: Unable to execute plugin method
com.getcapacitor.PluginInvocationException: Unable to invoke method customService on plugin io.ionic.starter.CustomNativePlugin
    at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:101)
    at com.getcapacitor.Bridge$2.run(Bridge.java:537)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.os.HandlerThread.run(HandlerThread.java:65)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:99)
    at com.getcapacitor.Bridge$2.run(Bridge.java:537)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.os.HandlerThread.run(HandlerThread.java:65)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
    at android.content.ComponentName.<init>(ComponentName.java:130)
    at android.content.Intent.<init>(Intent.java:5780)
    at io.ionic.starter.MainActivity.startService(MainActivity.java:26)
    at io.ionic.starter.CustomNativePlugin.customService(CustomNativePlugin.java:37)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:99)
    at com.getcapacitor.Bridge$2.run(Bridge.java:537)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.os.HandlerThread.run(HandlerThread.java:65)

Body:
because of An Android component (service, receiver, activity) can trigger the execution of a service via the startService(intent) method. i have to call it from context;

@PluginMethod(returnType = PluginMethod.RETURN_NONE)
public void customService(PluginCall call) {
    call.resolve();
    Intent service = new Intent(getContext(), MyIntentService.class);
    getContext().startService(service);

}


问:
Is there a way to dynamic update variables in subclass?
I have two config classes, base class Config and subclass ProdConfig, code shows below:

class Config:
    URL = "http://127.0.0.1"
    ANOTHER_URL = URL + "/home"

class ProdConfig(Config):
    URL = "http://test.abc.com"
    # ANOTHER_URL = URL + "/home"

print(Config.URL) # "http://127.0.0.1"
print(Config.ANOTHER_URL) # "http://127.0.0.1/home"
print(ProdConfig.URL) # "http://test.abc.com"
print(ProdConfig.ANOTHER_URL) # http://127.0.0.1/home
```

If I do not override the variable ANOTHER_URL in ProdConfig or declare it as @property, the value is same with base class, I got it cus value is assigned when base class imported, but can I get it with new value align to new URL? Is there a way to use Metaclass or setattr or getattr tricks to solve this problem??

Thank you very much!

答:
If you want a way to inherit the default home directory of your site, but also be able to override it when you create instances of the subclass. You could do it this way.

This shows how a subclass can inherit default state, or customize its  state depending on the parameter passed to the constructor.

```<code>>>> class Config:
...     LOCALHOST    = "http://127.0.0.1"
...     DEFAULT_HOME = "/home"
...     def __init__(self):
...         self._home = self.DEFAULT_HOME
...
...     @property
...     def url(self):
...         return self.LOCALHOST + self.home
...
...     @property
...     def home(self):
...         return self._home
...
...     @home.setter
...     def home(self, h):
...         self._home = h
...
>>> class ProdConfig(Config):
...
...     def __init__(self, home=None):
...         super().__init__()
...         if home:
...             self.home = home
>>>
>>> c = Config()
>>> d = ProdConfig()
>>> e = ProdConfig("/someotherhome")
>>>
>>> c.url
'http://127.0.0.1/home'
>>> d.url
'http://127.0.0.1/home'
>>> e.url
'http://127.0.0.1/someotherhome'
>>>
</code>```

The idea I had above was to show a good practice for providing subclasses access to their inherited state without necessitating sensitive access directly to the base class's variables. The private _home base class variable is accessed through a property by the subclass. Using another language, I'd probably declare home as a protected property, and <base>._home as private.

Body:
I have two config classes, base class Config and subclass ProdConfig, code shows below:

class Config:
    URL = "http://127.0.0.1"
    ANOTHER_URL = URL + "/home"

class ProdConfig(Config):
    URL = "http://test.abc.com"
    # ANOTHER_URL = URL + "/home"

print(Config.URL) # "http://127.0.0.1"
print(Config.ANOTHER_URL) # "http://127.0.0.1/home"
print(ProdConfig.URL) # "http://test.abc.com"
print(ProdConfig.ANOTHER_URL) # http://127.0.0.1/home

If I do not override the variable ANOTHER_URL in ProdConfig or declare it as @property, the value is same with base class, I got it cus value is assigned when base class imported, but can I get it with new value align to new URL? Is there a way to use Metaclass or setattr or getattr tricks to solve this problem??

Thank you very much!

Body:
If you want a way to inherit the default home directory of your site, but also be able to override it when you create instances of the subclass. You could do it this way.

This shows how a subclass can inherit default state, or customize its  state depending on the parameter passed to the constructor.

>>> class Config:
...     LOCALHOST    = "http://127.0.0.1"
...     DEFAULT_HOME = "/home"
...     def __init__(self):
...         self._home = self.DEFAULT_HOME
...
...     @property
...     def url(self):
...         return self.LOCALHOST + self.home
...
...     @property
...     def home(self):
...         return self._home
...
...     @home.setter
...     def home(self, h):
...         self._home = h
...
>>> class ProdConfig(Config):
...
...     def __init__(self, home=None):
...         super().__init__()
...         if home:
...             self.home = home
>>>
>>> c = Config()
>>> d = ProdConfig()
>>> e = ProdConfig("/someotherhome")
>>>
>>> c.url
'http://127.0.0.1/home'
>>> d.url
'http://127.0.0.1/home'
>>> e.url
'http://127.0.0.1/someotherhome'
>>>

The idea I had above was to show a good practice for providing subclasses access to their inherited state without necessitating sensitive access directly to the base class's variables. The private _home base class variable is accessed through a property by the subclass. Using another language, I'd probably declare home as a protected property, and <base>._home as private.