【问题标题】:XSL transformation failing due to invalid formatting?由于格式无效,XSL 转换失败?
【发布时间】: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。
  • 请注意,您可以简单地使用&lt;xsl:value-of select="translate(cac:PayeeFinancialAccount/cbc:ID, 'NO', '')"/&gt; 而无需测试。
  • @michael.hor257k 这适用于这个例子,但一个例子并不构成规范......

标签: xml xslt


【解决方案1】:

我对您的要求“我正在尝试从 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    相关资源
    最近更新 更多