我的目标是首先将节点按<RowBreak>
分组,然后在每个<RowBreak>
组中,我想按<ColumnBreak>
分组。
这是我的XML。
<?xml version="1.0" encoding="utf-8" ?>
<Tree>
<Item>
<Label>Item 1</Label>
</Item>
<Item>
<Label>Item 2</Label>
</Item>
<ColumnBreak />
<Item>
<Label>Item 3</Label>
</Item>
<Item>
<Label>Item 4</Label>
</Item>
<Item>
<Label>Item 5</Label>
</Item>
<RowBreak />
<Item>
<Label>Item 6</Label>
</Item>
<Item>
<Label>Item 7</Label>
</Item>
<ColumnBreak />
<Item>
<Label>Item 8</Label>
</Item>
<RowBreak />
<Item>
<Label>Item 9</Label>
</Item>
<Item>
<Label>Item 10</Label>
</Item>
</Tree>
输出应为:
Item 1 Item 3
Item 2 Item 4
Item 5
Item 6 Item 8
Item 7
Item 9
Item 10
我当前的XSLT是这样的:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:key name="rowGroups" match="Tree/*[not(self::RowBreak)]" use="count(preceding-sibling::RowBreak)" />
<xsl:template match="Tree">
<xsl:variable name="rowGroupings" select="*[not(self::RowBreak)][generate-id() = generate-id(key('rowGroups', count(preceding-sibling::RowBreak))[1])]" />
<xsl:variable name="position" select="position()" />
<table>
<xsl:for-each select="$rowGroupings">
<xsl:variable name="rowId" select="generate-id()"/>
<xsl:variable name="colGroupings" select="*[not(self::ColumnBreak)][generate-id()=$rowId][1]" />
<tr>
<xsl:for-each select="$colGroupings">
<!--Do logic here to group by ColumnBreak-->
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
但是,我在提取每个<ColumnBreak>
中的<RowBreak>
个组时遇到问题(请参阅colGroupings变量)。我想为循环中的每个<key>
创建一个<RowBreak>
(类似于rowGroups),但是根据我对<xsl:key>
元素的理解,必须将其声明为顶层并进行匹配应该在实际节点上工作,而不是在变量上工作。
答案 0 :(得分:2)
这是我第一次通过时要做的事情:
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:strip-space elements="*"/>
<xsl:template match="/Tree">
<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>
</cells>
</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 version="1.0" encoding="UTF-8"?>
<cells>
<cell row="1" col="1">Item 1</cell>
<cell row="1" col="1">Item 2</cell>
<cell row="1" col="2">Item 3</cell>
<cell row="1" col="2">Item 4</cell>
<cell row="1" col="2">Item 5</cell>
<cell row="2" col="1">Item 6</cell>
<cell row="2" col="1">Item 7</cell>
<cell row="2" col="2">Item 8</cell>
<cell row="3" col="1">Item 9</cell>
<cell row="3" col="1">Item 10</cell>
</cells>
这是可以实际使用的东西。
这是一个完整的样式表,可通过两遍处理输入:
XSLT 1.0(具有EXSLT node-set()
扩展功能)
<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 version="1.0" encoding="utf-8"?>
<table border="1">
<tr>
<td>Item 1<br/>Item 2<br/></td>
<td>Item 3<br/>Item 4<br/>Item 5<br/></td>
</tr>
<tr>
<td>Item 6<br/>Item 7<br/></td>
<td>Item 8<br/></td>
</tr>
<tr>
<td>Item 9<br/>Item 10<br/></td>
</tr>
</table>
已渲染