xslt-1.0 - XSLT 1.0 在一个或多个级别上分组
问题描述
今天的挑战是在 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 开发的一种简单方法......
解决方案
受这篇文章的启发,我找到了一个巧妙的解决方案:
我已经包含了将其用于单组或双组的注释,请参阅代码中的注释。请注意我如何使用第一个键(索引)作为第二个 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>
推荐阅读
- python - ImportError: No module named psycopg2.extensions 在 Cloud DataFlow 上运行我的代码时
- python - Weasy Print 的 Unicode 解码
- httprequest - 检查服务中的 HTTP 状态 - Angular6
- r - 如何在 ggplot 中绘制由数据框的变量之一分面的面板时间序列
- c# - 是什么让新项目的 ids 自动增加(asp.net core 的 DbContext + InMemoryDatabase)?
- c# - Net Core 解决“不包含内容错误的定义”并在 API 中读取为字符串
- ruby - 懒惰地枚举,直到块为假
- javascript - Angular 6 嵌套数组在 ngfor 中的使用
- ruby-on-rails - 在 Rails 应用程序中使用 Facebook In rails 应用程序登录,无需 gem
- mongoose - 无论如何,猫鼬都会将我的布尔值返回为真