我想根据标签cbc:tax豁免reasoncode的值从XML更改输入值,如果此类标签的值类似于11、12、13、14、15、16、21、31、32、33、34、35、36、37,则标签cac:taxScheme中的值必须更改为另一个值,如下面将显示的值。
我尝试使用xslt 3.0进行模板匹配(也可以使用xslt 2.0)
我的代码:
<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>
使用第一个模板匹配的缺点是以下模板没有生效。如果我评论第一个模板,那么下面的模板可以正常工作。我需要第一个模板的另一个原因是它在这篇文章中没有解释的意义。
输入:
<?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>
期望输出:
<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>
我该怎么做才能得到所需的输出?
给我一些建议。