【问题标题】:How to do a second transform on the output of an XSLT template如何对 XSLT 模板的输出进行第二次转换
【发布时间】:2011-10-03 13:24:23
【问题描述】:

我只有基本的 XSLT 技能,如果这是基本的或不可能的,敬请见谅。

我有一个分页器模板,它在我正在查看的网站上随处使用。存在一个错误,其中一个特定搜索需要将 categoryId 参数附加到页面链接的 href。 我无法更改分页器样式表,否则我只会为其添加一个参数。我想做的是按原样应用模板,然后根据其输出进行第二次转换。这可能吗?其他人通常如何扩展库模板?

到目前为止,我一直在考虑对输出进行递归复制,并在处理 href 时将模板应用于它们。它的语法让我有些不知所措,特别是因为我什至不确定它是否可能。


编辑 - 在 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>

【问题讨论】:

    标签: xslt


    【解决方案1】:

    这是一个完整的示例,如何使用 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>
    

    当此转换应用于以下 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>
    

    产生想要的结果(每个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>
    

    【讨论】:

      【解决方案2】:

      这在 XSLT 2 中是可能的;您可以将数据存储在变量中并在其上调用应用模板。

      基本示例:

      <xsl:variable name="MyVar">
         <xsl:element name="Elem"/> <!-- Or anything that creates some output -->
      </xsl:variable>
      <xsl:apply-templates select="$MyVar"/>
      

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

      【讨论】:

      • 您可以在 XSLT 1.0 中对任何提供 exsl:node-set() 扩展的处理器执行相同的操作:只需将 apply-templates 中的 select 替换为 select="exsl:node-set ($MyVar)"。
      猜你喜欢
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多