在XSLT 1.0中,将当前上下文节点传递给被调用模板并使该节点成为被调用模板内的上下文节点的最短/最干净/推荐方法是什么?
如果一个没有xsl:param并且由空调用模板调用的模板只是拿起调用者的上下文节点,那将是很好的(它会,对吧?),但我能想到的最好的是:
<xsl:call-template name="sub">
<xsl:with-param name="context" select="." />
</xsl:call-template>
Run Code Online (Sandbox Code Playgroud)
同
<xsl:template name="sub">
<xsl:param name="context" />
<xsl:for-each select="$context">
</xsl:for-each>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
Dim*_*hev 27
如果一个没有
xsl:param并且被空调用的模板call-template只是选择调用者的上下文节点,那将是很好的(它会,对吧?).
这正是xsl:call-templateW3C XSLT 1.0(和2.0)规范中定义的,并且由任何兼容的XSLT处理器实现.
这是一个小例子:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="a">
<xsl:call-template name="currentName"/>
</xsl:template>
<xsl:template name="currentName">
Name: <xsl:value-of select="name(.)"/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
将此转换应用于以下XML文档时:
<t>
<a/>
</t>
Run Code Online (Sandbox Code Playgroud)
产生了想要的正确结果:
Name: a
Run Code Online (Sandbox Code Playgroud)
换一种方式解释Dimitre所说的话。
当您从某个节点调用模板时,您已经在该节点中了,
例:
假定此代码:
<xsl:template match="MyElement">
<xsl:call-template name="XYZ"/>
</xsl:template>
<xsl:template name="XYZ>
<xsl:value-of select="."/>
</xsl>
Run Code Online (Sandbox Code Playgroud)
上面的代码和编写代码一样好:
<xsl:template match="MyElement">
<xsl:value-of select="."/>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
您也可以在调用的模板中使用for-each循环。:)
但是只要确定您确切在哪里..
| 归档时间: |
|
| 查看次数: |
17707 次 |
| 最近记录: |