XSL: Retrieve text value as currency and add it

I have the following input XML.

<root>
      <Envelope>
            <Body>
             <Response>
              <Itin>
                  <Services Item="005"">
                        <Service Code="SOS">
                        <Extensions>
                            <Type>PAYMENT</Type>
                        </Extensions>
                                    <Text>RA UK1 YVRYYZ0702P01DEC/SEAT/ACCPT/USD1500/075XG CCVIXXXXXXXXXXXX1237E</Text>                        
                        </Service>
                  </Services>
             <Itin>                  
            </Response>
            </Body>
      </Envelope>
</root>                  

I want to get the Text value when the Type="PAYMENT" and @Code="SOS" and store it into a variable.
Then in the variable grab 2 substring values (after USD) and add it.
In this case, 1500 and 075; With decimal place 2 places from the right, so 15.00 and 00.75
Then the total would be 15.75

I want to store all 3 currencies into 3 separate variables.
badtz7229Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
I made a named template that does the sum calculation,
you can pass it a different node and a different currency if you want

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    
    <xsl:variable name="USD-sum">
        <xsl:call-template name="get-sum">
            <xsl:with-param name="text" select="//Service[@Code='SOS'][Extensions/Type = 'PAYMENT']/Text"></xsl:with-param>
            <xsl:with-param name="curr" select="'USD'"/>
        </xsl:call-template>
    </xsl:variable>
    
    <xsl:template match="/">
        <xsl:value-of select="$USD-sum"/>
    </xsl:template>
    
    <xsl:template name="get-sum">
        <xsl:param name="text"/>
        <xsl:param name="curr"/>
        <xsl:variable name="waste-deleted" select="substring-after(normalize-space($text), $curr)"/>
        <xsl:variable name="am1" select="number(substring-before($waste-deleted, '/'))"/>
        <xsl:variable name="after-slash" select="substring-after($waste-deleted, '/')"/>
        <xsl:variable name="first-non-numeric-2" select="substring(translate($after-slash, '1234567890.', '') , 1,1)"/>
        <xsl:variable name="am2" select="number(substring-before($after-slash, $first-non-numeric-2))"/>
        <xsl:value-of select="format-number(($am1 + $am2) div 100, '0.00')"/>
        
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
badtz7229Author Commented:
thanks that worked great.
also, how would i pad the values with empty spaces?
for instance am1 and am2 has to be enclosed within 11 spaces regardless of amount.
For example
<15.00      > 11 spaces or
<125.00     > 11 spaces
0
Geert BormansInformation ArchitectCommented:
I think this should work
        <xsl:value-of select="format-number(($am1 + $am2) div 100, '          0.00')"/>
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

badtz7229Author Commented:
that prefixes the total with 11 spaces + value of sum. instead of have the whole value be contained within 11 spaces.
0
Geert BormansInformation ArchitectCommented:
mmh right, maybe you can rewrite the named template like this

    <xsl:template name="get-sum">
        <xsl:param name="text"/>
        <xsl:param name="curr"/>
        <xsl:variable name="spaces" select="'           '"></xsl:variable>
        <xsl:variable name="waste-deleted" select="substring-after(normalize-space($text), $curr)"/>
        <xsl:variable name="am1" select="number(substring-before($waste-deleted, '/'))"/>
        <xsl:variable name="after-slash" select="substring-after($waste-deleted, '/')"/>
        <xsl:variable name="first-non-numeric-2" select="substring(translate($after-slash, '1234567890.', '') , 1,1)"/>
        <xsl:variable name="am2" select="number(substring-before($after-slash, $first-non-numeric-2))"/>
        <xsl:variable name="sum-str" select="format-number(($am1 + $am2) div 100, '0.00')"/>
        <xsl:value-of select="substring($spaces, 1, 11 -  string-length(string($sum-str)))"/>
        <xsl:value-of select="$sum-str"/>
    </xsl:template>

Open in new window

0
badtz7229Author Commented:
thanks. all of it worked. i appreciate it.
0
Geert BormansInformation ArchitectCommented:
welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
CSS

From novice to tech pro — start learning today.