【问题标题】:XSLT sibling recursion grouping columns by column spanXSLT 兄弟递归按列跨度对列进行分组
【发布时间】:2020-08-15 14:09:08
【问题描述】:

当检测到<ColumnBreak> 时,我有一个有效的 XSLT 将我的元素分组到列中。它遵循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

现在,如果我想在&lt;Item&gt; 中引入一个名为ColumnSpan 的新属性,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 中可行吗?谢谢!

【问题讨论】:

  • 看看这是否有帮助:stackoverflow.com/a/27217608/3016153 请注意,如果您只需要处理列跨度,您的情况会更简单。
  • 感谢此链接。当我正在研究链接中的示例代码时,我想问一下哪种方法适合我的情况?选项 A)首先执行与上述 XSLT 相同的兄弟递归,然后循环遍历单元格以调整宽度,或者选项 B)在兄弟递归期间调整宽度然后分配给单元格?

标签: xml recursion xslt xslt-1.0 muenchian-grouping


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多