xml - 如何在 XSLT Muenchian 分组中包含空节点集?
问题描述
我想通过 和 将我的 XML 转换为多组行和<RowBreak>
列<ColumnBreak>
。我现在可以使用 Muenchian 分组成功地对其进行转换,但是如何包含空节点?在下面的示例中,中间有两个<ColumnBreak />
空字符串,因此预期的输出是:
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>
XSLT:
<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
模板以检查前面的同级是否不是Item
. 在这种情况下,您可以添加一个“空”单元格
<xsl:template match="ColumnBreak" mode="sibling">
<xsl:param name="row"/>
<xsl:param name="col"/>
<xsl:if test="not(preceding-sibling::*[1][self::Item])">
<cell row="{$row}" col="{$col}" empty="true" />
</xsl:if>
<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: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
如果您对空单元格中有<br />
标签感到高兴,您甚至可以忽略此处的检查。
推荐阅读
- c++ - Qt与webRTC如何实现
- javascript - React 在第三方库的回调中丢失“this”
- wordpress - WP_CLI - 如何获取/设置路径
- import - gcloud AttributeError:“模块”对象没有属性“aes_key_wrap_with_padding”
- javascript - React Bootstrap 列未填充整个宽度
- powershell - PowerShell Invoke-RestMethod 查询 Token
- c - printf("%s 缓冲区[k]",缓冲区[k],k); 仅打印 > 缓冲区而不是 %s
- typescript - FirestoreDataConverter 为 Admin SDK 引发错误,但不为 JavaScript 或 NodeJS 作为客户端
- bash - 在文件中的位置周围打印行
- python-3.x - MongoDB Flatten Embedded Document from find() 操作