XSL转换因格式无效而失败



我正试图从cbc:ID元素中删除字符NO,但只能在找到这些字符的文件中删除。

<cbc:ID>NO123456789</cbc:ID>

应该变成:

<cbc:ID>123456789</cbc:ID>

我已经取得了一些进步,但遇到了困难,我可以看出我做错了什么。Notepad++XML工具插件抱怨XSL无效。

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cac:PaymentMeans>
<cbc:PaymentMeansCode>31</cbc:PaymentMeansCode>
<cac:PayeeFinancialAccount>
<cbc:ID>NO60210517971</cbc:ID>
</cac:PayeeFinancialAccount>
</cac:PaymentMeans>
</Invoice>

XSL文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<xsl:template match="cac:PaymentMeans">
<Invoice version="1.0"
xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cac:PaymentMeans>
<cac:PayeeFinancialAccount>
<cbc:ID>
<xsl:variable name="accountnumber" select="cac:PayeeFinancialAccount/cbc:ID"/>
<xsl:choose>
<xsl:when test="contains($accountnumber, 'NO'">
<xsl:value-of select="translate($accountnumber, 'NO', '')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$accountnumber"/>
</xsl:otherwise>
</xsl:choose>
</cbc:ID>
</cac:PayeeFinancialAccount>
</cac:PaymentMeans>
</Invoice>
</xsl:template>
</xsl:stylesheet>

我对您的需求的解释"我正在尝试从cbc:ID元素中删除字符NO"是下面的样式表解决方案。只需要一个替换,其余的可以由复制模板处理:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="//cbc:ID" priority="2">
<xsl:copy>
<xsl:value-of select="replace(.,'NO','')"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

以下是xslt应用于xml时的结果:

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cac:PaymentMeans>
<cbc:PaymentMeansCode>31</cbc:PaymentMeansCode>
<cac:PayeeFinancialAccount>
<cbc:ID>60210517971</cbc:ID>
</cac:PayeeFinancialAccount>
</cac:PaymentMeans>
</Invoice>

最新更新