使用<xsl:import>应用<xsl:call-template>程序非常简单:
示例 XML 命名为f.xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<a>abc</a>
<b>cde</b>
</root>
Run Code Online (Sandbox Code Playgroud)
主要示例 XSLT f.xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:import href="f1.xslt"/>
<xsl:template match="/root">
A: <xsl:value-of select="a/text()" />
<xsl:call-template name="secondTemplate" />
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
包括示例 XSLT f1.xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="secondTemplate">
B: <xsl:value-of select="b/text()" />
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
输出:
<?xml version="1.0"?>
A: abc
B: cde
Run Code Online (Sandbox Code Playgroud)
所以第一个 XSLT( f.xslt) 确实调用了第二个 XSLT( f1.xslt) - 它是<xsl:import ...>通过一个命名模板导入的,该模板通过该<xsl:call-template name="secondTemplate" />行访问。