如何编写一个捕获所有 XSL 模板并传递所有参数?
我正在编写一个“扩展”模板的模板,但它有一些问题。
<xsl:template match="*[@condition]" mode="#all">
<xsl:element name="condition">
<xsl:attribute name="name">
<xsl:value-of select="@condition"></xsl:value-of>
</xsl:attribute>
<xsl:apply-imports>
</xsl:apply-imports>
</xsl:element>
问题是使用
调用的模板缺少参数。 参数列表未知,因为此模板尝试扩展许多不同的模板(因此是 mode="#all"
)。
有没有好的办法解决这个问题?
其他示例:
考虑两个最终模板(只读):
<xsl:template match="*" mode="mode1">
<param name="p1"/>
</xsl:template>
<xsl:template match="*" mode="mode2">
<param name="p2"/>
</xsl:template>
它们在某处被调用(只读):
<xsl:apply-templates mode="mode1">
<xsl:with-param name="mode1" select="$mode1"/>
</xsl:apply-templates>
<xsl:apply-templates mode="mode2">
<xsl:with-param name="mode2" select="$mode2"/>
</xsl:apply-templates>
可能有 100 个模式 1、模式 2、模式 3、模式 4 ...,并且名称没有模式。
我想要一个全局模板,其中包含最终模板的附加信息。类似的:
<xsl:template match="*" mode="#all">
<xsl:next-match/>
</xsl:element>
问题是上面的全局模板没有将参数传递给模板。
I am writing a template that "extends" templates, but it has some issues.
<xsl:template match="*[@condition]" mode="#all">
<xsl:element name="condition">
<xsl:attribute name="name">
<xsl:value-of select="@condition"></xsl:value-of>
</xsl:attribute>
<xsl:apply-imports>
</xsl:apply-imports>
</xsl:element>
The problem with this is templates called using <xsl:apply-imports>
are missing params.
The list of params are not known since there are many different templates that this template is trying to extend (hence the mode="#all"
).
Is there a good way around this?
Additional example:
Consider two final templates (read-only):
<xsl:template match="*" mode="mode1">
<param name="p1"/>
</xsl:template>
<xsl:template match="*" mode="mode2">
<param name="p2"/>
</xsl:template>
they are called somewhere (read-only):
<xsl:apply-templates mode="mode1">
<xsl:with-param name="mode1" select="$mode1"/>
</xsl:apply-templates>
<xsl:apply-templates mode="mode2">
<xsl:with-param name="mode2" select="$mode2"/>
</xsl:apply-templates>
There might be 100s of mode1, mode2, mode3, mode4 ... and the names do not have a pattern.
I would like to have a global template that wraps additional info around the final templates. Something like:
<xsl:template match="*" mode="#all">
<xsl:next-match/>
</xsl:element>
The problem is the above global template does not pass the params to the templates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的问题是,当您调用
时,它不包含传递给当前模板的参数,您可以使用隧道参数。在您要扩展的模板中,像这样标记您的参数:此外,当您传递参数时:
我还建议使用
而不是
。If your problem is that when you call
<xsl:apply-imports>
it doesn't include the parameters passed to the current template, you can use tunnel parameters. In the template you are extending mark your parameters like this:Also, when you pass the parameters:
I also suggest using
<xsl:next-match>
instead of<xsl:apply-imports>
.您可以按照 Max Toro 的建议使用隧道参数,也可以只传递单个参数,其子参数是不同模板需要并识别的参数。
类似这样的:
You can either use tunnel parameters as suggested by Max Toro, or you could just pass a single parameter, whose children are the parameters that different templates will need and will recognize.
Something like this: