【发布时间】:2020-09-17 13:22:19
【问题描述】:
我正在尝试从 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>
【问题讨论】:
-
您在 XSLT 的第 23 行缺少右括号。应该是:包含($accountnumber,'NO')。
-
@Sebastien 非常感谢!一定是瞎了眼……
-
我的建议:根据我的经验,Notepad++ 中的消息很神秘,很难在 Notepad++ 中使用 XSLT 进行开发。获取一些 XSLT 开发工具,或使用 xsltfiddle.liberty-development.net 或简单地在命令行中使用 Saxon。
-
请注意,您可以简单地使用
<xsl:value-of select="translate(cac:PayeeFinancialAccount/cbc:ID, 'NO', '')"/>而无需测试。 -
@michael.hor257k 这适用于这个例子,但一个例子并不构成规范......