0
votes

I want to change an input value from a XML depending on value from the tag cbc:TaxExemptionReasonCode, if such tag have values like 11,12,13,14,15,16,21,31,32,33,34,35,36,37 then the value inside the tag cac:TaxScheme have to change to another values like I'm gonna show below.

I tried with template match using XSLT 3.0(It could be also with XSLT 2.0) My code:

<xsl:mode on-no-match="shallow-copy" />
<xsl:template match="cac:TaxTotal">
    <xsl:variable name="taxTotal" select="../cac:TaxTotal"/>
    <xsl:copy>
        <xsl:copy-of select="$taxTotal[cac:TaxSubtotal/cac:TaxCategory/cac:TaxScheme/cbc:ID[text()='1000']]/cbc:TaxAmount"/>
        <xsl:for-each select="$taxTotal">
            <xsl:copy-of select="cac:TaxSubtotal"/>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>
<xsl:template match="cac:TaxScheme[../cbc:TaxExemptionReasonCode[matches(text(), '^(11|12|13|14|15|16|21|31|32|33|34|35|36|37)$')]]">
    <xsl:copy>
        <cbc:ID>9996</cbc:ID>
        <cbc:Name>GRA</cbc:Name>
        <cbc:TaxTypeCode>FRE</cbc:TaxTypeCode>
    </xsl:copy>
</xsl:template>

The drawback using the first template match is that the following templates aren't taking effect. If I comment the first template then the following templates working fine. I need the first template for another reason that it has no sense to explain in this post.

Input:

    <?xml version="1.0" encoding="utf-8" standalone="no"?>
<Invoice
    xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
    xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
    xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
    <cac:TaxTotal>
        <cbc:TaxAmount currencyID="PEN">198.00</cbc:TaxAmount>
        <cac:TaxSubtotal>
            <cbc:TaxAmount currencyID="PEN">198.00</cbc:TaxAmount>
            <cbc:Percent>18.00</cbc:Percent>
            <cac:TaxCategory>
                <cbc:TaxExemptionReasonCode>12</cbc:TaxExemptionReasonCode>
                <cac:TaxScheme>
                    <cbc:ID>1000</cbc:ID>
                    <cbc:Name>IGV</cbc:Name>
                    <cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>
                </cac:TaxScheme>
            </cac:TaxCategory>
        </cac:TaxSubtotal>
    </cac:TaxTotal>
</Invoice>

Output desired:

    <Invoice
    xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
    xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
    xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
    <cac:TaxTotal>
        <cbc:TaxAmount currencyID="PEN">198.00</cbc:TaxAmount>
        <cac:TaxSubtotal>
            <cbc:TaxAmount currencyID="PEN">198.00</cbc:TaxAmount>
            <cbc:Percent>18.00</cbc:Percent>
            <cac:TaxCategory>
                <cbc:TaxExemptionReasonCode>12</cbc:TaxExemptionReasonCode>
                <cac:TaxScheme>
                    <cbc:ID>9996</cbc:ID>
                    <cbc:Name>GRA</cbc:Name>
                    <cbc:TaxTypeCode>FRE</cbc:TaxTypeCode>
                </cac:TaxScheme>
            </cac:TaxCategory>
        </cac:TaxSubtotal>
    </cac:TaxTotal>
</Invoice>

what should I do to get the output desired? Give me any suggestions.

1

1 Answers

2
votes

The solution is simple: in the first template change the line

<xsl:copy-of select="cac:TaxSubtotal"/>

to

<xsl:apply-templates select="cac:TaxSubtotal"/>

Then all subsequent templates will be applied and your stylesheet will work as desired.

This works because you have the XSLT-3.0 equivalent of the identity template defined in you code with the line

<xsl:mode on-no-match="shallow-copy" />

which takes care of copying the nodes.