如何对XSLT模板的输出进行第二次转换

Oll*_*rds 16 xslt

我只有基本的XSLT技能,所以如果这是基本的或不可能的道歉.

我有一个paginator模板,可以在我正在查看的网站上的任何地方使用.有一个错误,一个特定的搜索需要将一个categoryId参数附加到页面链接的href.我不能改变paginator样式表,否则我只会添加一个param.我想要做的是按原样应用模板,然后根据其输出进行第二次转换.这可能吗?其他人通常如何扩展库模板?

到目前为止,我已经考虑过对输出进行递归复制,并在处理时将模板应用于hrefs.这种语法有点让我感到厌烦,特别是因为我甚至不确定它是否可行.


编辑 - 在Dabbler的回答和Michael Kay的评论之间,我们到了那里.这是我的完整测试.

 <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, '&amp;', '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>
Run Code Online (Sandbox Code Playgroud)

Dim*_*hev 18

以下是如何使用XSLT 1.0完成多次传递处理的完整示例:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

  <xsl:template match="node()|@*" mode="mPass2">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*" mode="mPass2"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/">
  <xsl:variable name="vrtfPass1Result">
   <xsl:apply-templates/>
  </xsl:variable>

  <xsl:apply-templates mode="mPass2"
      select="ext:node-set($vrtfPass1Result)/*"/>
 </xsl:template>

 <xsl:template match="num/text()">
  <xsl:value-of select="2*."/>
 </xsl:template>

 <xsl:template match="/*" mode="mPass2">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates mode="mPass2"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="num/text()" mode="mPass2">
  <xsl:value-of select="3 + ."/>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于以下XML文档时:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>
Run Code Online (Sandbox Code Playgroud)

想要的结果(每个num乘以2并在下一个通3被添加到每个num)产生:

<nums>
   <num>5</num>
   <num>7</num>
   <num>9</num>
   <num>11</num>
   <num>13</num>
   <num>15</num>
   <num>17</num>
   <num>19</num>
   <num>21</num>
   <num>23</num>
</nums>
Run Code Online (Sandbox Code Playgroud)


Dab*_*ler 9

这在XSLT 2中是可能的; 您可以将数据存储在变量中,并在其上调用apply-templates.

基本示例:

<xsl:variable name="MyVar">
   <xsl:element name="Elem"/> <!-- Or anything that creates some output -->
</xsl:variable>
<xsl:apply-templates select="$MyVar"/>
Run Code Online (Sandbox Code Playgroud)

样式表中的某个地方有一个与Elem相匹配的模板.您还可以使用单独的模式来明确区分两个阶段(构建变量并对其进行处理),尤其是当两个阶段都使用与相同节点匹配的模板时.

  • 您可以在XSLT 1.0中使用任何提供exsl:node-set()扩展的处理器执行相同的操作:只需使用select ="exsl:node-set($ MyVar)"替换apply-templates中的select. (5认同)