按xml字母顺序对数据进行排序

时间:2013-07-24 21:33:03

标签: xslt xslt-1.0 xslt-grouping

输入XML:

<?xml version="1.0" encoding="utf-8" ?>
<infoset>
  <info>
    <title>Bill</title>
    <group>
      <code>state</code>
    </group>
  </info>
  <info>
    <title>Auto</title>
    <group>
      <code>state</code>
    </group>
  </info>
  <info>
    <title>Auto2</title>
  </info>
  <info>
    <title>Auto3</title>
  </info>
  <info>
    <title>Auto5</title>
  </info>
  <info>
    <title>Certificate</title>
    <group>
      <code>Auto4</code>
    </group>
  </info>
  </infoset>

预期产出:

A

Auto2
Auto3
Auto4
   Certificate
Auto5

S
state
   Auto
   Bill

我需要按字母顺序排列标题和代码。如果信息有组,则磁贴应位于组下。我正在使用visual studio2010,xslt1.0处理器和xml编辑器。

1 个答案:

答案 0 :(得分:1)

XSLT中的排序很简单。你真正需要知道的是如何“分组”项目。正如Michael Kay评论的那样,这在XSLT 2.0中要比在XSLT 1.0中容易得多。在XSLT 1.0中,您倾向于使用Muenchian分组方法,当您第一次看到它时看起来很混乱,但通常是最有效的方法。

从输出的外观来看,您正在进行两次分组。首先,按第一个字母,然后是组/代码(如果存在)或标题

Muenchian Grouping通过定义一个键来实现,以便快速查找“组”中的所有项目。对于组/代码标题的第一个字母,您可以这样定义

<xsl:key name="letter" match="info" use="substring(concat(group/code, title), 1, 1)"/>

(注意:这是区分大小写的,所以如果你可以混合使用大写和小写的开头字母,你可能需要使用'translate'功能。)

如果群组/代码存在,它将使用该字母的第一个字母,否则它将获取标题的第一个字母。

对于群组/代码标题本身,密钥如下

<xsl:key name="info" match="info" use="title[not(../group)]|group/code"/>

因此,它只使用“title”元素,而不存在“group”元素。

要获得第一个分组的不同首字母,请选择所有 info 元素,并检查它们是否是其给定字母的键中的第一个元素。这样就完成了

<xsl:apply-templates 
     select="info
             [generate-id() 
              = generate-id(key('letter', substring(concat(group/code, title), 1, 1))[1])]" 
     mode="letter">
  <xsl:sort select="substring(concat(group/code, title), 1, 1)" />
</xsl:apply-templates>

此处使用“模式”,因为最终的XSLT必须匹配信息的模板。

在匹配模板中,要按代码/群组标题分组,您可以执行此操作

<xsl:apply-templates 
     select="key('letter', $letter)
            [generate-id() = generate-id(key('info', title[not(../group)]|group/code)[1])]">
  <xsl:sort select="title[not(../group)]|group/code" />
</xsl:apply-templates>

最后,要输出最终组中的所有元素,您只需再次使用该键

<xsl:apply-templates select="key('info', $value)[group/code=$value]/title">

这是完整的XSLT。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>

  <xsl:key name="letter" match="info" use="substring(concat(group/code, title), 1, 1)"/>
  <xsl:key name="info" match="info" use="title[not(../group)]|group/code"/>

  <xsl:template match="/*">
    <xsl:apply-templates select="info[generate-id() = generate-id(key('letter', substring(concat(group/code, title), 1, 1))[1])]" mode="letter">
      <xsl:sort select="substring(concat(group/code, title), 1, 1)" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="info" mode="letter">
    <xsl:variable name="letter" select="substring(concat(group/code, title), 1, 1)" />
    <xsl:value-of select="concat($letter, '&#10;')" />
    <xsl:apply-templates select="key('letter', $letter)[generate-id() = generate-id(key('info', title[not(../group)]|group/code)[1])]">
      <xsl:sort select="title[not(../group)]|group/code" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="info">
    <xsl:variable name="value" select="title[not(../group)]|group/code" />
    <xsl:value-of select="concat($value, '&#10;')" />
    <xsl:apply-templates select="key('info', $value)[group/code=$value]/title">
      <xsl:sort select="." />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="title">
    <xsl:value-of select="concat('   ', ., '&#10;')" />
  </xsl:template>
</xsl:stylesheet>

应用于XML时,输出以下内容

A
Auto2
Auto3
Auto4
   Certificate
Auto5
s
state
   Auto
   Bill
相关问题