У меня есть рабочий XSLT, который группирует мои элементы в столбцы при обнаружении <ColumnBreak>
. Он следует концепции рекурсии брата, описанной в этой ссылке .
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 должен иметь возможность соответственно расширять столбец и смещать другие столбцы вниз при необходимости. См. Атрибут ColumnSpan
в пункте 1 выше XML.
Ожидаемые результаты
Элемент 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? Спасибо!