XSLT同级递归按列范围对列进行分组

问题描述 投票:0回答:1

我有一个有效的XSLT,当检测到<ColumnBreak>时,该元素将我的元素分为几列。它遵循此link中解释的同级递归概念。

XSLT 1.0:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name="cell-by-row" match="cell" use="@row" />
<xsl:key name="cell-by-col" match="cell" use="concat(@row, '|', @col)" />

<xsl:template match="/Tree">
    <!-- first-pass -->
    <xsl:variable name="cells">
        <xsl:apply-templates select="Item[1]" mode="sibling">
            <xsl:with-param name="row" select="1"/>
            <xsl:with-param name="col" select="1"/>
        </xsl:apply-templates>  
    </xsl:variable>
    <!-- output -->
    <table border = "1">
        <!-- for each distinct row -->
        <xsl:for-each select="exsl:node-set($cells)/cell[count(. | key('cell-by-row', @row)[1]) = 1]">
            <tr>
                <!-- for each distinct cell in the current row -->
                <xsl:for-each select="key('cell-by-row', @row)[count(. | key('cell-by-col', concat(@row, '|', @col))[1]) = 1]">
                    <td>
                        <!-- get the values in the current cell -->
                        <xsl:for-each select="key('cell-by-col', concat(@row, '|', @col))">
                            <xsl:value-of select="."/>
                            <br/>
                        </xsl:for-each>
                    </td>
                </xsl:for-each>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

<xsl:template match="Item" mode="sibling">
    <xsl:param name="row"/>
    <xsl:param name="col"/>
    <cell row="{$row}" col="{$col}">
        <xsl:value-of select="Label"/>
    </cell>
    <xsl:apply-templates select="following-sibling::*[1]" mode="sibling">
        <xsl:with-param name="row" select="$row"/>
        <xsl:with-param name="col" select="$col"/>
    </xsl:apply-templates>  
</xsl:template>

<xsl:template match="ColumnBreak" mode="sibling">
    <xsl:param name="row"/>
    <xsl:param name="col"/>
    <xsl:apply-templates select="following-sibling::*[1]" mode="sibling">
        <xsl:with-param name="row" select="$row"/>
        <xsl:with-param name="col" select="$col + 1"/>
    </xsl:apply-templates>  
</xsl:template>

<xsl:template match="RowBreak" mode="sibling">
    <xsl:param name="row"/>
    <xsl:param name="col"/>
    <xsl:apply-templates select="following-sibling::*[1]" mode="sibling">
        <xsl:with-param name="row" select="$row + 1"/>
        <xsl:with-param name="col" select="1"/>
    </xsl:apply-templates>  
</xsl:template>
</xsl:stylesheet>

XML:

<?xml version="1.0" encoding="utf-8" ?>
<Tree>
  <Item ColumnSpan="2">
    <Label>Item 1</Label>
  </Item>
  <Item>
    <Label>Item 2</Label>
  </Item>
  <Item>
    <Label>Item 3</Label>
  </Item>
  <ColumnBreak />
  <Item>
    <Label>Item 4</Label>
  </Item>
  <Item>
    <Label>Item 5</Label>
  </Item>
  <ColumnBreak />
  <Item>
    <Label>Item 6</Label>
  </Item>
  <Item>
    <Label>Item 7</Label>
  </Item>
</Tree>

当前输出:

Item 1      Item 4      Item 6
Item 2      Item 5      Item 7  
Item 3

现在,如果要在ColumnSpan中引入一个称为<Item>的新属性,XSLT应该能够相应地扩展该列,并在必要时向下移动其他列。请参阅上述XML的第1项中的ColumnSpan属性。

预期结果

项目1 ColumnSpan = 2:

Item 1                  Item 6
Item 2      Item 4      Item 7  
Item 3      Item 5

项目1 ColumnSpan = 3:

Item 1                  
Item 2      Item 4      Item 6  
Item 3      Item 5      Item 7

项目2 ColumnSpan = 2:

Item 1      Item 4      Item 6                  
Item 2                  Item 7                  
Item 3      Item 5      

这个概念在XSLT 1.0中可行吗?谢谢!

xml recursion xslt xslt-1.0 muenchian-grouping
1个回答
0
投票

这当然是可行的,而且肯定是具有挑战性的!

我想到的方法是在递归调用中添加一个额外的参数,以捕获某些(行,列)组合不再可用的事实,并在为以下行分配行/列时使用此信息下一个条目。

您使用的是1.0,因此,如何表示此数据结构的选择当然非常有限;一种可能性是字符串,如果单元格可用,则字符位置($ row * N + $ column)为空格,否则为“ X”。

© www.soinside.com 2019 - 2024. All rights reserved.