A.xsl 导入 B.xsl,其中包含 A.xsl 中使用的函数。A.xsl 包含标识模板。B.xsl的函数需要将模板规则应用于变量;但是,A.xsl 中的标识模板将覆盖它们。
我的想法是xsl:apply-imports
B 中的变量,但与xsl:apply-templates
不同的是,没有办法select=
将其指向变量。原始函数不能替换为模板规则。有没有办法在不xsl:include
-ing B.xsl 的情况下做到这一点?
A.xsl:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="http://me"
version="2.0">
<xsl:import href="B.xsl"/>
<xsl:template match="X">
<xsl:sequence select="my:do-stuff(.)"/>
</xsl:template>
<xsl:template match="@*|node()" mode="#all" priority="-1">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="#current"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
B.xsl:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="http://me"
version="2.0">
<xsl:template match="X" mode="cleanup">
<clean><xsl:apply-templates/></clean>
</xsl:template>
<xsl:function name="my:do-other-stuff">
<xsl:param name="x" as="element(X)"/>
...
</xsl:function>
<xsl:function name="my:do-stuff">
<xsl:param name="x" as="element(X)"/>
<xsl:variable name="x-updated" select="my:do-other-stuff($x)"/>
<xsl:apply-templates select="$x-updated" mode="cleanup"/>
</xsl:function>
</xsl:stylesheet>
一种选择是通过更改mode="#all"
来列出除cleanup
之外的所有模式来防止标识模板覆盖:
<xsl:template match="@*|node()" mode="#default not-cleanup1 not-cleanup2" priority="-1">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="#current"/>
</xsl:copy>
</xsl:template>