【发布时间】: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"><?xml version="1.0" encoding="UTF-8"?>
</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>
问候,迈克
【问题讨论】: