XSLT 1.0如何在条件为真时产生空结果和/或中止转换

时间:2019-01-16 10:01:32

标签: xml xslt xslt-1.0

如果HeaderInformation / InvoiceType = CreditNote,我如何中止xslt转换?

XML:

<?xml version="1.0" encoding="utf-8"?>
<SALESINVOICE>
    <Interchange>
		<Recipient>1234</Recipient>
		<Sender>5678</Sender>
		<CreationDate>2019-01-11:09:16:43</CreationDate>
		<Test>No</Test>
		<Interchange_Control_Number>123584</Interchange_Control_Number>
		<HeaderInformation>
			<OrigInvoiceNumber>1</OrigInvoiceNumber>
			<InvoiceType>CreditNote</InvoiceType>
		</HeaderInformation>
	</Interchange>
</SALESINVOICE>

最好的问候 朱利安

2 个答案:

答案 0 :(得分:1)

取决于“中止”的含义:

您可以像这样使用<xsl:message>

<xsl:if test="HeaderInformation/InvoiceType='CreditNote'">
    <xsl:message terminate="yes"/>
</xsl:if>

这将以错误终止。

如果您不希望出错,也可以执行以下操作:

<xsl:template match="/">
    <xsl:if test="SALESINVOICE[not(Interchange/HeaderInformation/InvoiceType='CreditNote')]">
        <!-- Here goes your XSLT code -->
        <xsl:apply-templates/>
    </xsl:if>
</xsl:template>

如果找到具有值<InvoiceType>的{​​{1}},将输出一个空文档。

答案 1 :(得分:1)

这是一种方法(很多):

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/">
    <xsl:apply-templates select="SALESINVOICE[not(Interchange/HeaderInformation/InvoiceType='CreditNote')]"/>
</xsl:template>

<xsl:template match="SALESINVOICE">
    <!-- instructions to process the invoice -->
</xsl:template>

</xsl:stylesheet>