Xml 如何在XSLT Muenchian分组中包含空节点集?
我想通过Xml 如何在XSLT Muenchian分组中包含空节点集?,xml,xslt,Xml,Xslt,我想通过和将XML转换为一组行和列。我现在可以使用Muenchian分组成功地对其进行转换,但是如何包含空节点呢?在下面的示例中,有两个,中间有空字符串,因此预期输出为: XML: <?xml version="1.0" encoding="utf-8" ?> <Tree> <Item> <Label>Item 1</Label> </Item> <Item> <Label&g
和
将XML转换为一组行和列。我现在可以使用Muenchian分组成功地对其进行转换,但是如何包含空节点呢?在下面的示例中,有两个
,中间有空字符串,因此预期输出为:
XML:
<?xml version="1.0" encoding="utf-8" ?>
<Tree>
<Item>
<Label>Item 1</Label>
</Item>
<Item>
<Label>Item 2</Label>
</Item>
<ColumnBreak />
<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>
<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: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">
<xsl:variable name="cells">
<xsl:apply-templates select="*[1]" mode="sibling">
<xsl:with-param name="row" select="1"/>
<xsl:with-param name="col" select="1"/>
</xsl:apply-templates>
</xsl:variable>
<table border = "1">
<xsl:for-each select="exsl:node-set($cells)/cell[count(. | key('cell-by-row', @row)[1]) = 1]">
<tr>
<xsl:for-each select="key('cell-by-row', @row)[count(. | key('cell-by-col', concat(@row, '|', @col))[1]) = 1]">
<td>
<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="*" 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>
项目1
项目2
项目3
项目4
项目5
项目6
项目7
项目8
项目9
项目10
XSLT:
<?xml version="1.0" encoding="utf-8" ?>
<Tree>
<Item>
<Label>Item 1</Label>
</Item>
<Item>
<Label>Item 2</Label>
</Item>
<ColumnBreak />
<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>
<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: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">
<xsl:variable name="cells">
<xsl:apply-templates select="*[1]" mode="sibling">
<xsl:with-param name="row" select="1"/>
<xsl:with-param name="col" select="1"/>
</xsl:apply-templates>
</xsl:variable>
<table border = "1">
<xsl:for-each select="exsl:node-set($cells)/cell[count(. | key('cell-by-row', @row)[1]) = 1]">
<tr>
<xsl:for-each select="key('cell-by-row', @row)[count(. | key('cell-by-col', concat(@row, '|', @col))[1]) = 1]">
<td>
<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="*" 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>
在不对XSLT进行重大更改的情况下,可以进行的一个修复是修改ColumnBreak
模板,以检查前面的同级项是否不是项
。在这种情况下,可以添加一个“空”单元格
然后,在主模板中,可以检查空单元格
<xsl:for-each select="key('cell-by-col', concat(@row, '|', @col))">
<xsl:if test="not(@empty)">
<xsl:value-of select="."/>
<br/>
</xsl:if>
</xsl:for-each>
事实上,如果您希望空单元格中有一个
标记,您甚至可以在此处省略对@empty
的检查
请参见举一个简单的例子,专注于手头的问题,排除所有其他问题,怎么样?--通常,可以使用string(node)
键选择空节点。