【发布时间】: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
现在,如果我想在<Item> 中引入一个名为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