首页 > 解决方案 > 如何在 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>

标签: xmlxslt

解决方案


您可以在不对 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 />标签感到高兴,您甚至可以忽略此处的检查。

http://xsltfiddle.liberty-development.net/jz1PuP1


推荐阅读