加载外部XSL代码块

时间:2012-02-15 13:38:52

标签: xslt xslt-1.0

是否可以加载外部XSL代码块,类似于如何加载代码块,例如aspx include? EG:

    <xsl:if test="$ShowNextButton='No'">
        <!-- A Block of external code would be loaded here -->
    </xsl:if>

如果有所不同,我正在使用XSLT 1.0。

1 个答案:

答案 0 :(得分:7)

如果您的“外部XSL代码”块可以放在命名模板中,您可以轻松完成。

这是一个使用主XSLT样式表(base.xsl)并包含外部XSLT样式表(include.xsl)的通用示例。

<强> input.xml中

<test>
  <foo trigger-template="yes">
    <bar>Original "bar".</bar>
  </foo>
  <foo trigger-template="no">
    <bar>Original "bar".</bar>
  </foo>
</test>

<强> base.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:include href="include.xsl"/>
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="foo">
    <foo>
      <xsl:if test="@trigger-template='yes'">
        <xsl:call-template name="external-template">
          <xsl:with-param name="statement" select="'Successfully called external XSL code!'"/>
        </xsl:call-template>
      </xsl:if>
      <xsl:apply-templates/>
    </foo>
  </xsl:template>

</xsl:stylesheet>

<强> include.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template name="external-template">
    <xsl:param name="statement"/>
    <bar><xsl:value-of select="$statement"/></bar>
  </xsl:template>  

</xsl:stylesheet>

<强>的Output.xml

<test>
   <foo>
      <bar>Successfully called external XSL code!</bar>
      <bar>Original "bar".</bar>
   </foo>
   <foo>
      <bar>Original "bar".</bar>
   </foo>
</test>
相关问题