Doing a double-pass in XSL?

六月ゝ 毕业季﹏ 提交于 2019-12-01 08:21:20

XSLT 2.0 Solution :

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

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

The trick here, is to use mode="firstPassResult" for the first pass while all the templates for the sedond pass should have mode="secondPass".

Edit:

Example :

<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>

Output :

[xslt] <test><firstPass>Init</firstPass></test>

So the first pass creates some elements with the content of root/a and the second one prints the created elements to std out. Hopefully this is enough to get you going.

Yes, with XSLT 2.0 it is easy. With XSLT 1.0 you can of course also use modes and store a temporary result in a variable the same way as in XSLT 2.0 but the variable is then a result tree fragment, to be able to process it further with apply-templates you need to use an extension function like exsl:node-set on the variable.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!