首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在XSL中做双重传递吗?

在XSL中做双重传递吗?
EN

Stack Overflow用户
提问于 2011-11-22 01:28:06
回答 2查看 1.4K关注 0票数 3

是否可以将XSL转换的输出存储在某种类型的变量中,然后对变量的内容执行额外的转换?(所有内容都在一个XSL文件中)

(优先考虑XSLT-2.0)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-22 01:35:45

XSLT2.0解决方案:

代码语言:javascript
复制
<xsl:variable name="firstPassResult">
  <xsl:apply-templates select="/" mode="firstPass"/>
</xsl:variable>

<xsl:template match="/">
  <xsl:apply-templates select="$firstPassResult" mode="secondPass"/>
</xsl:template>

这里的诀窍是使用mode="firstPassResult“作为第一次传递,而用于第二次传递的所有模板都应该有mode="secondPass”。

编辑:

示例:

代码语言:javascript
复制
<root>
  <a>Init</a>
</root>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:variable name="firstPassResult">
    <xsl:apply-templates select="/" mode="firstPass"/>
  </xsl:variable>

  <xsl:template match="/" mode="firstPass">
      <test>
        <firstPass>
          <xsl:value-of select="root/a"/>
        </firstPass>
      </test>
  </xsl:template>

  <xsl:template match="/">
    <xsl:apply-templates select="$firstPassResult" mode="secondPass"/>
  </xsl:template>

  <xsl:template match="/" mode="secondPass">
    <xsl:message terminate="no">
      <xsl:copy-of select="."/>
    </xsl:message>
  </xsl:template>

</xsl:stylesheet>

输出:

代码语言:javascript
复制
[xslt] <test><firstPass>Init</firstPass></test>

因此,第一遍创建了一些内容为root/a的元素,第二遍将创建的元素打印到std输出。希望这足以让你继续前进。

票数 9
EN

Stack Overflow用户

发布于 2011-11-22 01:39:09

是的,使用XSLT 2.0很容易。在XSLT1.0中,当然也可以使用模式并将临时结果存储在变量中,方法与在XSLT2.0中相同,但是变量是结果树片段,为了能够使用apply-templates进一步处理它,需要在变量上使用exsl:node-set等扩展函数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8215892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档