问题描述
我只有基本的 XSLT 技能,所以如果这是基本的或不可能的,我深表歉意.
I have only basic XSLT skills so apologies if this is either basic or impossible.
我有一个分页器模板,它在我正在查看的网站上随处可见.存在一个错误,即一个特定的搜索需要将 categoryId 参数附加到页面链接的 href 中.我无法更改分页器样式表,否则我只会向其中添加一个参数.我想做的是按原样应用模板,然后根据其输出进行第二次转换.这可能吗?其他人通常如何扩展库模板?
I have a paginator template which is used everywhere on the site I'm looking at. There's a bug where one particular search needs to have a categoryId parameter appended to the href of the page links. I can't alter the paginator stylesheet or else i would just add a param to it. What I'd like to do is apply the template as is then do a second transform based on its output. Is this possible? How do others normally go about extending library templates?
到目前为止,我已经考虑过对输出进行递归复制,并在处理 hrefs 时将模板应用于它们.它的语法让我有些不知所措,特别是因为我什至不确定这是否可能.
So far I've thought about doing a recursive copy of the output and applying a template to the hrefs as they are processed. The syntax for that escapes me somewhat, particularly as I'm not even sure it's possible.
编辑 - 在 Dabbler 的回答和 Michael Kay 的评论之间,我们到达了那里.这是我的完整测试.
Edit - Between Dabbler's answer and Michael Kay's comment we got there. Here is my complete test.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common">
<!-- note we require the extensions for this transform -->
<!--We call the template to be extended here and store the result in a variable-->
<xsl:variable name="output1">
<xsl:call-template name="pass1"/>
</xsl:variable>
<!--The template to be extended-->
<xsl:template name="pass1">
<a href="url?param1=junk">foo</a>
</xsl:template>
<!--the second pass. we lock this down to a mode so we can control when it is applied-->
<xsl:template match="a" mode="pass2">
<xsl:variable name="href" select="concat(@href, '&', 'catid', '=', 'stuff')"/>
<a href="{$href}"><xsl:value-of select="."/></a>
</xsl:template>
<xsl:template match="/">
<html><head></head><body>
<!--the node-set extension function turns the first pass back into a node set-->
<xsl:apply-templates select="ext:node-set($output1)" mode="pass2"/>
</body></html>
</xsl:template>
</xsl:stylesheet>
推荐答案
在 XSLT 2 中是可能的;您可以将数据存储在变量中,然后调用 apply-templates.
It's possible in XSLT 2; you can store data in a variable and call apply-templates on that.
基本示例:
<xsl:variable name="MyVar">
<xsl:element name="Elem"/> <!-- Or anything that creates some output -->
</xsl:variable>
<xsl:apply-templates select="$MyVar"/>
在你的样式表的某个地方有一个与 Elem 匹配的模板.您还可以使用单独的模式来明确区分两个阶段(构建变量和处理变量),尤其是当两个阶段都使用匹配相同节点的模板时.
And somewhere in your stylesheet have a template that matches Elem. You can also use a separate mode to keep a clear distinction between the two phases (building the variable and processing it), especially when both phases use templates that match the same nodes.
这篇关于如何对 XSLT 模板的输出进行二次转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!