Search code examples
htmlxsltforeachxslt-1.0contains

in xslt match decimal with in text and write after it


I have a problem. I think it is simple but I havent found it. like below code I want to find and match multiplier factor value like "0.05" with "005" in the note value dynamically and take text "AAA" after "_ 005 _"(without spaces) and write it and others too.

I tried to use together both format number and concatenate to "concat(format-number(***))" but failed because of newby about this.

<cbc:Note>40.00 BT</cbc:Note>
        <cbc:Note>17_                       2005.00</cbc:Note>
        <cbc:Note>11_005_AAA</cbc:Note>
        <cbc:Note>11_002_BBB</cbc:Note>
        <cbc:Note>11_003_CCC</cbc:Note>
        <cbc:InvoicedQuantity unitCode="CS">1.000</cbc:InvoicedQuantity>
        <cbc:LineExtensionAmount currencyID="TRY">200.00</cbc:LineExtensionAmount>
        <cac:AllowanceCharge>
            <cbc:ChargeIndicator>false</cbc:ChargeIndicator>
            <cbc:MultiplierFactorNumeric>0.03</cbc:MultiplierFactorNumeric>
            <cbc:Amount currencyID="TRY">6.00</cbc:Amount>
            <cbc:BaseAmount currencyID="TRY">200.00</cbc:BaseAmount>
        </cac:AllowanceCharge>
        <cac:AllowanceCharge>
            <cbc:ChargeIndicator>false</cbc:ChargeIndicator>
            <cbc:MultiplierFactorNumeric>0.05</cbc:MultiplierFactorNumeric>
            <cbc:Amount currencyID="TRY">10.00</cbc:Amount>
            <cbc:BaseAmount currencyID="TRY">200.00</cbc:BaseAmount>
        </cac:AllowanceCharge>

current xslt block

<xsl:for-each select="./cac:AllowanceCharge/cbc:MultiplierFactorNumeric">
                    <br/>
                    <xsl:text> %</xsl:text>
                    <xsl:value-of select="format-number(. * 100, '###.##0,00', 'european')"/>,
            </xsl:for-each>

I appreciate if you help me.


Solution

  • It is very difficult to understand your question.

    The following minimal example is mostly based on a guess. It formats the MultiplierFactorNumeric value as a 3-digit whole number and uses it as a key to retrieve the corresponding Note value:

    XML

    <root>
        <Note>40.00 BT</Note>
        <Note>17_                       2005.00</Note>
        <Note>11_005_AAA</Note>
        <Note>11_002_BBB</Note>
        <Note>11_003_CCC</Note>
        <AllowanceCharge>
            <MultiplierFactorNumeric>0.03</MultiplierFactorNumeric>
        </AllowanceCharge>
        <AllowanceCharge>
            <MultiplierFactorNumeric>0.05</MultiplierFactorNumeric>
        </AllowanceCharge>
    </root>
    

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <xsl:key name="note" match="Note" use="substring-before(substring-after(., '_'), '_')" />
    
    <xsl:template match="/root">
        <result>
            <xsl:for-each select="AllowanceCharge">
                <item>
                    <factor>
                        <xsl:value-of select="MultiplierFactorNumeric"/>
                    </factor>
                    <value>
                        <xsl:value-of select="substring-after(substring-after(key('note', format-number(100*MultiplierFactorNumeric, '000')), '_'), '_')"/>
                    </value>
                </item>
            </xsl:for-each>
        </result>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Result

    <?xml version="1.0" encoding="UTF-8"?>
    <result>
      <item>
        <factor>0.03</factor>
        <value>CCC</value>
      </item>
      <item>
        <factor>0.05</factor>
        <value>AAA</value>
      </item>
    </result>