XSLT Muenchian分组嵌套行和列

时间:2018-12-18 05:59:06

标签: xml xslt muenchian-grouping

我正在使用XSLT 1.0。我的XML文件中有<RowBreak><ColumnBreak>元素。 RowBreak将按行对我的数据进行分组,而ColumnBreak将转换为列。只要没有ColumnBreak或RowBreak的匹配项,显示就应该继续向下。

这是我的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="$rowGroupings[not(self::ColumnBreak)][generate-id()=$rowId][1]" />
        <tr>
          <td>
            <xsl:for-each select="$colGroupings">
              <xsl:sort select="$colGroupings[preceding-sibling::ColumnBreak]" order="descending" />
              <xsl:if test="position() = 1">
                <xsl:variable name="maxRows" select="$colGroupings[preceding-sibling::ColumnBreak]" />
                <xsl:call-template name="ColumnGroupTemplate">
                  <xsl:with-param name="maxRows" select="$maxRows" />
                  <xsl:with-param name="colGroupings" select="$colGroupings" />
                </xsl:call-template>
              </xsl:if>
            </xsl:for-each>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>

  <xsl:template name="ColumnGroupTemplate">
    <xsl:param name="maxRows" />
    <xsl:param name="colGroupings" />
    <xsl:for-each select="$maxRows">
      <xsl:variable name="position" select="position()" />
      <tr>
        <xsl:for-each select="$colGroupings">
          <xsl:variable name="group" select="$colGroupings[preceding-sibling::ColumnBreak]" />
          <td>
            <xsl:value-of select="normalize-space($colGroupings[position() = $position])" />
          </td>
        </xsl:for-each>
      </tr>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

如何使用Muenchian分组实现预期的输出?谢谢!

1 个答案:

答案 0 :(得分:1)

如果您将此样式表应用于输入的XML

<table>
   <tr>
      <td>Item 1</td>
      <td>Item 3</td>
   </tr>
   <tr>
      <td>Item 2</td>
   </tr>
   <tr>
      <td>Item 4</td>
      <td>Item 6</td>
      <td>Item 8</td>
   </tr>
   <tr>
      <td>Item 5</td>
      <td>Item 7</td>
   </tr>
   <tr>
      <td>Item 9</td>
   </tr>
   <tr>
      <td>Item 10</td>
   </tr>
</table>

你会得到

import 'package:android_intent/android_intent.dart';

查看实际情况https://xsltfiddle.liberty-development.net/3NzcBuj/1