如何将使用for-each-group的XSLT 2.0代码转换为XSLT 1.0版本
XSLT 2.0 转 1.0 实现方案
原2.0版本中for-each-group的分组逻辑在1.0中可通过递归遍历+参数传值的方式实现,完全兼容所有XSLT 1.0运行环境,最终代码如下:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/rows"> <xsl:copy> <xsl:apply-templates select="row[1]"> <xsl:with-param name="current-title" select="''"/> </xsl:apply-templates> </xsl:copy> </xsl:template> <xsl:template match="row"> <xsl:param name="current-title"/> <xsl:variable name="new-title"> <xsl:choose> <xsl:when test="item='****************'"> <xsl:value-of select="preceding-sibling::row[1]/item"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$current-title"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:if test="starts-with(item[1], '#')"> <row> <item><xsl:value-of select="$new-title"/></item> <xsl:copy-of select="*"/> </row> </xsl:if> <xsl:apply-templates select="following-sibling::row[1]"> <xsl:with-param name="current-title" select="$new-title"/> </xsl:apply-templates> </xsl:template> </xsl:stylesheet>
实现逻辑说明
- 采用递归逐行处理所有
<row>节点,通过模板参数传递当前生效的标题值 - 遇到
item为16个星号的分隔行时,自动更新当前标题为分隔行前一行的item值 - 仅对
item以#开头的行进行输出,输出时会将当前标题作为额外节点插入行内,和原2.0版本输出结果完全一致 - 常规数据量场景下性能足够稳定,若处理超大数据量可改用Muenchian分组方案进一步优化
内容的提问来源于stack exchange,提问作者Fatema
相关产品推荐
相关产品推荐

