XSLT分组并省略重复

时间:2012-05-30 10:43:00

标签: xslt xslt-2.0 saxon xslt-grouping

我有以下XML输入文件:

<mappings>
    <mapping>
        <key>6718</key>
        <value attribute="content_type">Info Page</value>
    </mapping>
    <mapping>
        <key>35905</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>6718</key>
        <value attribute="content_type">Info Page</value>
    </mapping>
    <mapping>
        <key>36941</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>24920</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>40244</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>36639</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>1861</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>2280</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>42062</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
</mappings>

我想生成以下XML:

<reductions>
    <reduction>
        <group>Info Page</group>
        <key>6718</key>
    </reduction>
    <reduction>
        <group>Press releases</group>
        <key>35905</key>
        <key>36941</key>
        <key>24920</key>
        <key>40244</key>
        <key>36639</key>
        <key>1861</key>
        <key>2280</key>
        <key>42062</key>
    </reduction>
</reductions>

因此,输入XML中的键需要根据输入XML中值节点的值在输出XML中进行分组。 此外,需要删除重复项,输入XML将键值为6718两次,输出XML仅一次。

我正在使用XSLT 2.0和Saxon 9 HE。

有人可以帮助我吗?

[编辑]

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform 
    version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs">

    <xsl:template match="/">
        <xsl:variable name="mappings">
            <mappings>
                <xsl:apply-templates select="resource/R"/>
            </mappings>
        </xsl:variable>
        <reductions>
            <xsl:apply-templates select="$mappings/*"/>
        </reductions>
    </xsl:template>

    <xsl:template match="R">
        <mapping>
            <key><xsl:value-of select="MT[@N='content_id']/@V"/></key>
            <value>
                <xsl:attribute name="attribute">content_type</xsl:attribute>
                <xsl:value-of select="MT[@N='content_type']/@V"/>
            </value>
        </mapping>
        <!--mapping>
            <key><xsl:value-of select="MT[@N='content_id']/@V"/></key>
            <value>
                <xsl:attribute name="attribute">modified_timestamp</xsl:attribute>
                <xsl:value-of select="format-dateTime(MT[@N='modified_timestamp']/@V, '[Y0001]-[M01]-[D01]')"/>
            </value>
        </mapping-->
    </xsl:template>

    <xsl:template match="mapping">
        <xsl:for-each-group select="." group-by="value">
            <reduction>
                <group><xsl:value-of select="current-grouping-key()"/></group>
                <xsl:for-each-group select="current-group()/key" group-by=".">
                    <xsl:copy-of select="."/>
                </xsl:for-each-group>
            </reduction>
        </xsl:for-each-group>
    </xsl:template>

</xsl:transform>

1 个答案:

答案 0 :(得分:4)

以下是使用for-each-group两次的样式表:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:template match="mappings">
  <reductions>
    <xsl:for-each-group select="mapping" group-by="value">
      <reduction>
        <group><xsl:value-of select="current-grouping-key()"/></group>
        <xsl:for-each-group select="current-group()/key" group-by=".">
          <xsl:copy-of select="."/>
        </xsl:for-each-group>
      </reduction>
    </xsl:for-each-group>
  </reductions>
</xsl:template>

</xsl:stylesheet>

[编辑] 当我使用Saxon 9.4 HE对输入

运行发布的样式表时
<mappings>
    <mapping>
        <key>6718</key>
        <value attribute="content_type">Info Page</value>
    </mapping>
    <mapping>
        <key>35905</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>6718</key>
        <value attribute="content_type">Info Page</value>
    </mapping>
    <mapping>
        <key>36941</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>24920</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>40244</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>36639</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>1861</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>2280</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
    <mapping>
        <key>42062</key>
        <value attribute="content_type">Press releases</value>
    </mapping>
</mappings>

我得到了结果

<reductions>
   <reduction>
      <group>Info Page</group>
      <key>6718</key>
   </reduction>
   <reduction>
      <group>Press releases</group>
      <key>35905</key>
      <key>36941</key>
      <key>24920</key>
      <key>40244</key>
      <key>36639</key>
      <key>1861</key>
      <key>2280</key>
      <key>42062</key>
   </reduction>
</reductions>

根据我的理解,这是正确的。