我有xsl:include和xsl:apply-templates的问题 . 我正在使用xsl:call-template中的动态QName进行一些实验,我找到了一种方法,但是......它只能“内联”而不能包含在文件中 .
如果我这样做,它运作良好(一个文件 - file.xsl) - 它从匹配模板显示文本“test”:
<xsl:variable name="template" select="document('')/*/xsl:template"/>
<xsl:template match="xsl:template[@name='test']" name="test">
test
</xsl:template>
<xsl:template match="root">
<xsl:apply-templates select="$template[@name = $show]"/>
</xsl:template>
($ show ='test')
但是,如果我把第一部分放到不同的文件中,例如template.xsl然后我将用xsl:include调用它,它不起作用 . 我不知道为什么 .
<xsl:variable name="template" select="document('')/*/xsl:template"/>
<xsl:include href="template.xsl" />
<xsl:template match="root">
<xsl:apply-templates select="$template[@name = $show]"/>
</xsl:template>
请问有谁可以帮助我吗? xsl:include应该将filename.xsl的内容放到父模板中,但它看起来没有
注意:我有XSLT 1.0
Edit 2015-12-10: 谢谢你们指出一个问题 . 我正在设置文件路径('') dynamically now and it works like a charm! Problem was obviously on that blank document(''),非常感谢Michael和其他人 .
<xsl:variable name="path"><xsl:value-of select="$show"/>.xsl</xsl:variable>
<xsl:variable name="templatePath" select="document($path)/*/xsl:template"/>
...
...
<xsl:template match="root">
<xsl:apply-templates select="$template[@name = $show]"/>
</xsl:template>
1 回答
它不起作用的原因是表达式
仅在包含文档中选择xsl:template元素 .
您正在使用的设计模式实质上是对Dimitre Novatchev的FXSL的第一个(XSLT 1.0)版本的重新设计 . 我建议你仔细阅读 . IIRC,Dimitre的方法与xsl:template元素不匹配,而是与名称对应于您要执行的模板名称的元素相匹配:
这里可以在所有样式表模块中访问变量
$names
.当然,在XSLT 3.0中,所有这些tomfoolery都不再需要高阶函数 .