xslt 仅在提供参数时调用模板

xslt invoke template only when parameter is provided

我有 xlst,只有当它的值由 java(用 transformer.setParameter(paramName, paramValue) 设置)提供时,我才想在其中调用模板。我正在使用 saxon 9 HE。

这是我的样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
    <xsl:strip-space elements="*"/>

    <xsl:param name="supplierLogo" as="xs:string" required="no" select="''"/>

    <!-- add logo -->
    <xsl:template match="/INVOICES/INVOICE/SUPPLIER_LOGO">
        <xsl:if test="not($supplierLogo = '')">
            <SUPPLIER_LOGO>
                <xsl:value-of select="$supplierLogo"/>
            </SUPPLIER_LOGO>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>
  1. 如何有条件地运行模板?
  2. 现在,当未提供值时,元素 SUPPLIER_LOGO 将从我的 xml 中删除。如何避免这种情况?

从身份转换模板开始

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

然后使用

<xsl:template match="/INVOICES/INVOICE/SUPPLIER_LOGO[$supplierLogo = ''"/>

<xsl:template match="/INVOICES/INVOICE/SUPPLIER_LOGO[$supplierLogo != '']">
    <xsl:copy>
            <xsl:value-of select="$supplierLogo"/>
    </xsl:copy>
</xsl:template>