XSLT 1.0 在一个或多个级别上分组

XSLT 1.0 grouping on one or multiple levels

今天的挑战是在 XSLT 1.0 中分组。发现有一种叫做键和 Muenchian 分组的东西。

输入XML:

<Items>
    <Item>
        <ID>1</ID>
        <Name>A</Name>
        <Country>Sweden</Country>
        <Region>Småland</Region>
    </Item>
    <Item>
        <ID>2</ID>
        <Name>B</Name>
        <Country>Sweden</Country>
        <Region>Norrland</Region>
    </Item>
    <Item>
        <ID>3</ID>
        <Name>C</Name>
        <Country>USA</Country>
        <Region>Alaska</Region>
    </Item>
    <Item>
        <ID>4</ID>
        <Name>D</Name>
        <Country>USA</Country>
        <Region>Texas</Region>
    </Item>
    <Item>
        <ID>5</ID>
        <Name>E</Name>
        <Country>Sweden</Country>
        <Region>Norrland</Region>
    </Item>
</Items>

我需要将内容 XML 改造成更好的结构,而在此示例中 XML 我不喜欢按国家和地区构建项目。下面是对国家和地区也进行排序的通缉结果:

<Items>
  <Country Name="Sweden">
    <Region Name="Norrland">
      <Item>
        <ID>2</ID>
        <Name>B</Name>
      </Item>
      <Item>
        <ID>5</ID>
        <Name>E</Name>
      </Item>
    </Region>
    <Region Name="Småland">
      <Item>
        <ID>1</ID>
        <Name>A</Name>
      </Item>
    </Region>
  </Country>
  <Country Name="USA">
    <Region Name="Alaska">
      <Item>
        <ID>3</ID>
        <Name>C</Name>
      </Item>
    </Region>
    <Region Name="Texas">
      <Item>
        <ID>4</ID>
        <Name>D</Name>
      </Item>
    </Region>
  </Country>
</Items>

编辑:

我还想确保地区最终在他们自己的国家,即使有重复。我相应地编辑了答案。

此外,我想提示 xsltfiddle.liberty-development.net 作为进行试错 XSLT 开发的一种简单方法...

受此启发article,我找到了解决这个问题的巧妙方法:

我已经包含了将其用于单组或双组的注释,请参阅代码中的注释。请注意我如何使用第一个键(索引)作为第二个 for-each 循环的输入:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="country" match="Item" use="Country" />
  <xsl:key name="region" match="Item" use="concat(Region, '|', Country)" />

  <xsl:template match="/Items">
    <Items>
      <xsl:for-each select="Item[generate-id(.) = generate-id(key('country', Country))]">
        <xsl:sort select="Country" />
        <xsl:variable name="_country" select="Country" />

        <xsl:element name="Country">
          <xsl:attribute name="Name"><xsl:value-of select="$_country" /></xsl:attribute>

          <!-- single level grouping -->
          <!--<xsl:apply-templates select="key('country', Country)" />-->


          <!-- double grouping -->
          <!-- START -->
          <xsl:for-each select="key('country', Country)[generate-id(.) = generate-id(key('region', concat(Region, '|', Country)))]">
              <xsl:sort select="Region" />
              <xsl:variable name="_region" select="Region" />

              <xsl:element name="Region">
                <xsl:attribute name="Name"><xsl:value-of select="$_region" /></xsl:attribute>

                <xsl:apply-templates select="key('region', concat(Region, '|', Country))" />
              </xsl:element>
          </xsl:for-each>
          <!-- END -->

        </xsl:element>    
      </xsl:for-each>
    </Items>
  </xsl:template>

  <xsl:template match="Item">
    <xsl:element name="Item">
      <xsl:element name="ID"><xsl:value-of select="ID" /></xsl:element>
      <xsl:element name="Name"><xsl:value-of select="Name" /></xsl:element>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>