首页 > 解决方案 > 即使缺少分组键,也可以在 XSLT 中对元素进行分组

问题描述

我有以下 XML:

<cars>
  <car type='toyota'>
   <model>Yaris</model>
   <year>1998</year>
   <company>TOYOTA</company>
  </car>
  <car type='kia'>
   <model>Optima</model>
   <year>2002</year>
   <company>KIA</company>
  </car>
  <car type='kia'>
   <model>CERATO</model>
   <year>2009</year>
   <company>KIA</company>
  </car>
  <car type='bmw'>
   <model>M3</model>
   <year>2016</year>
   <company>BMW</company>
  </car>
  <car type='bmw'>
   <model>X5</model>
   <year>2010</year>
  </car>
  <car type='bmw'>
   <model>335i</model>
   <year>2010</year>
   <company>BMW</company>
  </car>
 </cars>

我想按公司元素对汽车进行分组,并在同一元素上进行排序(alpha,升序)。输出应该是这样的:

BMW: M3, X5, 335i
KIA: Optima, CERATO
TOYOTA: Yaris

问题是 car 元素可能不包含公司节点,在这种情况下,必须使用 car/@type 值将元素添加到正确的组中。如何将@type 属性的值映射到基于公司价值的正确组?

标签: xmlxsltxslt-grouping

解决方案


XSLT 的一个鲜为人知的特性(至少在 2.0 版中)是,当您创建一个列表时,例如(xx, yy, zz),这个列表实际上只包含现有值。如果例如xxvalue 为空,则它将不是结果列表的一部分。

所以如果你[1]在它后面写,你实际上是从括号之间的表达式列表中得到第一个非空元素

在您的评论中,从 5.08 开始,您要求提供一个other类型应该被视为TOYOTA. if ... then ... else ...在这种情况下 ,可以使用,: if (@type = 'other') then 'TOYOTA' else @type

因此,您可以通过以下方式编写脚本:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template match="cars">
    <xsl:for-each-group select="car" group-by="upper-case(
      (company, if (@type = 'other') then 'TOYOTA' else @type)[1])">
      <xsl:sort select="current-grouping-key()"/>
      <xsl:value-of select="current-grouping-key()"/>
      <xsl:text>: </xsl:text>
      <xsl:value-of select="string-join(current-group()/model, ', ')"/>
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each-group>
  </xsl:template>
</xsl:transform>

如你看到的:

  • (company, if (@type = 'other') then 'TOYOTA' else @type) 是源列表(大写的参数),
  • [1]从已创建的内容中获取第一个元素。

我将调用移至upper-case“外层”,假设model也可以用小写字母编写。


推荐阅读