【问题标题】:Conditionally creating empty file /output with xsl使用 xsl 有条件地创建空文件/输出
【发布时间】:2021-01-10 09:54:23
【问题描述】:

对于 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 文件,所以我在“常规输出”的开头添加了一个条件 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>

问候,迈克

【问题讨论】:

    标签: xslt xslt-1.0


    【解决方案1】:

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

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

    【讨论】:

    • 马丁,一如既往,你是救命稻草,非常感谢!
    • 还有一个小问题 Martin:我想省略 xml 声明 (omit-xml-declaration="yes") 。我知道我可以像这样使用: 但是我如何实现它以仅添加一行并结合建议的解决方案?
    • 我不确定我是否理解你想要实现的目标,因为你说你想省略 XML 声明并且似乎知道 &lt;xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/&gt; 但随后显示代码以输出一个。也许提出一个新问题,解释细节。
    • 目标是在 custaccount 为 13262 时写入一个空文件。多亏了你,我成功地做到了这一点,并且使用 omit 语句,输出文件为空。但是大多数时候都会有输出(custaccount!= 13262),在这些情况下,我确实需要在我的输出中声明 xml。
    • 我不确定纯 XSLT 1 中有没有办法,您的 XSLT 1.0 处理器是否支持 exsl:document?然后,您也许可以使用 XML 声明有条件地设置输出。
    猜你喜欢
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    相关资源
    最近更新 更多