XSLT Need to add value to same element in repeating nodes

Viewed 66

I'm trying to create XSLT to add 10 to each of the Quantity elements below

  <Lines>
    <JournalLine>
      <ItemCode>40006061</ItemCode>
      <Quantity>90</Quantity>
    </JournalLine>
    <JournalLine>
      <ItemCode>40006031</ItemCode>
      <Quantity>120</Quantity>
    </JournalLine>
    <JournalLine>
      <ItemCode>40008331</ItemCode>
      <Quantity>2400</Quantity>
    </JournalLine>
  </Lines>

Expected outcome:-

  <Lines>
    <JournalLine>
      <ItemCode>40006061</ItemCode>
      <Quantity>100</Quantity>
    </JournalLine>
    <JournalLine>
      <ItemCode>40006031</ItemCode>
      <Quantity>130</Quantity>
    </JournalLine>
    <JournalLine>
      <ItemCode>40008331</ItemCode>
    <Quantity>2410</Quantity>
    </JournalLine>
  </Lines>

Everything I have tried so far has resulted in the same value being repeated in all the nodes, any help appreciated.

EDIT:
I'm using XSLT-2.0, and this is my XSLT code so far:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="utf-8" omit-xml-declaration="no" indent="yes" />
    
    <xsl:template match="node()|@*">         
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Quantity/text()">
        <xsl:value-of select = "format-number(//Quantity, '###,###.00')"/>
    </xsl:template>
    
    <xsl:template match="Filename"></xsl:template>
    
</xsl:stylesheet>
1 Answers

You can use this XSLT-1.0/2.0 code to accomplish your goal:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="utf-8" omit-xml-declaration="no" indent="yes" />
    
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="JournalLine/Quantity">
        <xsl:copy>
          <xsl:value-of select="format-number(.+10, '###,###.00')"/>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

Its output is:

<?xml version="1.0" encoding="utf-8"?>
<Lines>
    <JournalLine>
      <ItemCode>40006061</ItemCode>
      <Quantity>100.00</Quantity>
    </JournalLine>
    <JournalLine>
      <ItemCode>40006031</ItemCode>
      <Quantity>130.00</Quantity>
    </JournalLine>
    <JournalLine>
      <ItemCode>40008331</ItemCode>
      <Quantity>2,410.00</Quantity>
    </JournalLine>
</Lines>

This output formats the number with decimal points as you specified in your question.

Related