使用 xsl 有条件地创建空文件/输出

Conditionally creating empty file /output with xsl

对于 SAP PO,当我的一个节点具有特定值时,我想禁止创建 xml 输出。

我检查了 this link 但我仍然在我的 xml 中得到输出(信封/父节点)我现在不明白...

我的 XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ns0="http://schemas.microsoft.com/dynamics/2008/01/documents/Message"
  xmlns:ns1="http://schemas.microsoft.com/dynamics/2008/01/documents/IntertourExtTasks">
     <xsl:output method="xml" indent="yes" />

    <xsl:template match="@*|node()">
    <xsl:choose>
        <xsl:when test="ns1:CustAccount = '13262'">
            
        </xsl:when>
        <xsl:otherwise>
            <!-- ... output ... -->
            <xsl:copy>
                <xsl:apply-templates/>
            </xsl:copy>            
        </xsl:otherwise>
    </xsl:choose>
    
</xsl:template>
</xsl:stylesheet>

来源:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:Envelope xmlns:ns0="http://schemas.microsoft.com/dynamics/2008/01/documents/Message">
    <ns0:Header>
        <ns0:MessageId>{00000000-0000-0000-0000-000010411998}</ns0:MessageId>
    </ns0:Header>
    <ns0:Body>
        <ns0:MessageParts>
            <ns1:IntertourExtTasks xmlns:ns1="http://schemas.microsoft.com/dynamics/2008/01/documents/IntertourExtTasks">
                <ns1:IntertourExternalTasks class="entity">
                    <ns1:BringGet>Get</ns1:BringGet>
                    <ns1:CustAccount>13262</ns1:CustAccount>
                </ns1:IntertourExternalTasks>
                <ns1:IntertourExternalTasks class="entity">
                    <ns1:BringGet>Get</ns1:BringGet>
                    <ns1:CustAccount>13262</ns1:CustAccount>
                </ns1:IntertourExternalTasks>
            </ns1:IntertourExtTasks>
        </ns0:MessageParts>
    </ns0:Body>
</ns0:Envelope>

CustAccount = 13262 时的预期结果:

<?xml version="1.0" encoding="UTF-8"?>

我得到的结果:

<?xml version="1.0" encoding="UTF-8"?><ns0:Envelope xmlns:ns0="http://schemas.microsoft.com/dynamics/2008/01/documents/Message">
    <ns0:Header>
        <ns0:MessageId>{00000000-0000-0000-0000-000010411998}</ns0:MessageId>
    </ns0:Header>
    <ns0:Body>
        <ns0:MessageParts>
            <ns1:IntertourExtTasks xmlns:ns1="http://schemas.microsoft.com/dynamics/2008/01/documents/IntertourExtTasks">
                
                
            </ns1:IntertourExtTasks>
        </ns0:MessageParts>
    </ns0:Body>
</ns0:Envelope>

更新:因为我需要一个没有 xml 声明的空白输出/空文件,所以我不得不使用 omit-xml-declaration="yes"。但是我不确定我的所有目标系统都可以在没有声明的情况下处理 xml 文件,所以我在 'regular output' 的开头添加了条件 xsl:text。为了完整起见,我的 xsl:

 <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:ns0="http://schemas.microsoft.com/dynamics/2008/01/documents/Message"
      xmlns:ns1="http://schemas.microsoft.com/dynamics/2008/01/documents/IntertourExtTasks">
         <xsl:output method="xml" indent="no" omit-xml-declaration="yes" />

        <!-- If one of the nodes have a custaccount of 13262 then NO OUTPUT otherwise output XML declaration-->
        <xsl:template match="/">
          <xsl:choose>
            <xsl:when test="/*[.//ns1:CustAccount[. = '13262']]">
            </xsl:when>
            <xsl:otherwise> <xsl:text disable-output-escaping="yes">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
            </xsl:text></xsl:otherwise>
        </xsl:choose>
         <xsl:apply-templates/>
       </xsl:template>
      
        <!-- If one of the nodes have a custaccount of 13262 then NO OUTPUT-->
        <xsl:template match="/*[.//ns1:CustAccount[. = '13262']]"/>
       
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>

此致,迈克

如果这通常是您需要的,我会保留身份转换,但随后会阻止对包含该元素和值的文档的任何处理

  <xsl:template match="/*[.//ns1:CustAccount[. = '13262']]"/>