Xml xslt分组和删除重复项 A. 20141112 2. 1. 12 1. a项案文 B 20141012 5. 1. 13 5. 项目b案文 C 20140912 6. 1. 12 2. 项目c案文 A. 20140612 9 B 20140212 7. D 20140712 9 A. 1. 12 1. a项案文 B 1. 13 5. 项目b案文 D 1. 13 .9 d项案文

Xml xslt分组和删除重复项 A. 20141112 2. 1. 12 1. a项案文 B 20141012 5. 1. 13 5. 项目b案文 C 20140912 6. 1. 12 2. 项目c案文 A. 20140612 9 B 20140212 7. D 20140712 9 A. 1. 12 1. a项案文 B 1. 13 5. 项目b案文 D 1. 13 .9 d项案文,xml,xslt,xslt-1.0,xslt-grouping,muenchian-grouping,Xml,Xslt,Xslt 1.0,Xslt Grouping,Muenchian Grouping,我有这个xml,我需要将它分组到下面的输出中,并添加inno和onno元素以提供sumno。并检查indate和ondate,以提供更大的延迟。并在inno/onno/indate/ondate不存在时提供空元素。并删除重复项。如何用木琴的方法写这个 输出: <Items> <array> <item name="A"> <name>A</name> <ind

我有这个xml,我需要将它分组到下面的输出中,并添加inno和onno元素以提供sumno。并检查indate和ondate,以提供更大的延迟。并在inno/onno/indate/ondate不存在时提供空元素。并删除重复项。如何用木琴的方法写这个

输出:

<Items>
    <array>
        <item name="A">
            <name>A</name>
            <indate>20141112</indate>
            <inno>2</inno>
            <status>1</status>
            <level>12</level>
            <size>.1</size>
            <text>item a text </text>
        </item>
        <item name="B">
            <name>B</name>
            <indate>20141012</indate>
            <inno>5</inno>
            <status>1</status>
            <level>13</level>
            <size>.5</size>
            <text>item b text </text>
        </item>
        <item name="C">
            <name>C</name>
            <indate>20140912</indate>
            <inno>6</inno>
            <status>1</status>
            <level>12</level>
            <size>.2</size>
            <text>item c text </text>
        </item>
        <item name="A">
            <name>A</name>
            <ondate>20140612</ondate>
            <onno>9</onno>
        </item>
        <item name="B">
            <name>B</name>
            <ondate>20140212</ondate>
            <inno>7</inno>
        </item>
        <item name="D">
            <name>D</name>
            <indate>20140712</indate>
            <inno>9</inno>
        </item>
        <item name="A">
            <name>A</name>
            <status>1</status>
            <level>12</level>
            <size>.1</size>
            <text>item a text </text>
        </item>
        <item name="B">
            <name>B</name>
            <status>1</status>
            <level>13</level>
            <size>.5</size>
            <text>item b text </text>
        </item>
        <item name="D">
            <name>D</name>
            <status>1</status>
            <level>13</level>
            <size>.9</size>
            <text>item d text </text>
        </item>
    </array>
</Items>

A.
20141112
2.
20140612
9
20141112
11
1.
12
1.
a项案文
B
20141012
5.
20140212
7.
20141012
12
1.
13
5.
项目b案文
C
20140912
6.
20140912
6.
1.
12
2.
项目c案文
D
20140712
7.
20140712
7.
1.
13
.9
d项案文
我使用这个xsl进行分组,但无法添加和删除重复项

<Items>
    <array>
        <item name="A">
            <name>A</name>
            <indate>20141112</indate>
            <inno>2</inno>
            <ondate>20140612</ondate>
            <onno>9</onno>
            <latedate>20141112</latedate>
            <sumno>11</sumno>
            <status>1</status>
            <level>12</level>
            <size>.1</size>
            <text>item a text </text>
        </item>
        <item name="B">
            <name>B</name>
            <indate>20141012</indate>
            <inno>5</inno>
            <ondate>20140212</ondate>
            <onno>7</onno>
            <latedate>20141012</latedate>
            <sumno>12</sumno>
            <status>1</status>
            <level>13</level>
            <size>.5</size>
            <text>item b text </text>
        </item>
        <item name="C">
            <name>C</name>
            <indate>20140912</indate>
            <inno>6</inno>
            <ondate/>
            <onno/>
            <latedate>20140912</latedate>
            <sumno>6</sumno>
            <status>1</status>
            <level>12</level>
            <size>.2</size>
            <text>item c text </text>
        </item>
        <item name="D">
            <name>D</name>
            <indate/>
            <inno/>
            <ondate>20140712</ondate>
            <onno>7</onno>
            <latedate>20140712</latedate>
            <sumno>7</sumno>
            <status>1</status>
            <level>13</level>
            <size>.9</size>
            <text>item d text </text>
        </item>
    </array>
</Items>

我认为您的代码确实正确地将
元素按其
名称
子元素分组。您似乎还想消除重复的子元素,我不确定您是只想通过元素名还是通过元素名和元素内容来实现这一点。如果要按元素名称执行此操作,则可以定义第二个键

至于添加空元素,您需要检查它们是否存在

下面是带有此类检查和第二个键的样式表:

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

            <xsl:key name="items-by-id" match="item[@name]" use="name"/>
            <xsl:template match="@* | node()">
                        <xsl:copy>
                                    <xsl:apply-templates select="@* | node()"/>
                        </xsl:copy>
            </xsl:template>
            <xsl:template match="array">
                        <xsl:copy>
                                    <xsl:apply-templates select="item[@name][generate-id() = generate-id(key('items-by-id', name)[1])]" mode="group"/>
                        </xsl:copy>
            </xsl:template>
            <xsl:template match="item[@name]" mode="group">
                        <xsl:copy>
                                    <xsl:copy-of select="name"/>
                                    <xsl:apply-templates select="key('items-by-id', name)"/>
            </xsl:copy>
            </xsl:template>
            <xsl:template match="item[@name]">
                        <xsl:apply-templates select="node()[not(self::name)]"/>
            </xsl:template>
</xsl:stylesheet>

还有一些问题,如检查不存在的日期,但我希望你能解决它

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

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="items-by-id" match="item[@name]" use="name"/>
  <xsl:key name="duplicated-elements" match="item/*" use="concat(../name, '|', local-name())"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="array">
    <xsl:copy>
      <xsl:apply-templates select="item[@name][generate-id() = generate-id(key('items-by-id', name)[1])]" mode="group"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="item[@name]" mode="group">
    <xsl:copy>
      <xsl:variable name="current-group" select="key('items-by-id', name)"/>
      <xsl:copy-of select="name"/>

      <xsl:choose>
        <xsl:when test="$current-group/indate">
          <xsl:apply-templates select="$current-group/indate"/>
        </xsl:when>
        <xsl:otherwise>
          <indate/>
        </xsl:otherwise>
      </xsl:choose>

      <xsl:choose>
        <xsl:when test="$current-group/inno">
          <xsl:apply-templates select="$current-group/inno"/>
        </xsl:when>
        <xsl:otherwise>
          <inno/>
        </xsl:otherwise>
      </xsl:choose>

      <xsl:choose>
        <xsl:when test="$current-group/ondate">
          <xsl:apply-templates select="$current-group/ondate"/>
        </xsl:when>
        <xsl:otherwise>
          <ondate/>
        </xsl:otherwise>
      </xsl:choose>

      <xsl:choose>
        <xsl:when test="$current-group/onno">
          <xsl:apply-templates select="$current-group/onno"/>
        </xsl:when>
        <xsl:otherwise>
          <onno/>
        </xsl:otherwise>
      </xsl:choose>

      <latedate>
        <xsl:choose>
          <xsl:when test="$current-group/indate > $current-group/ondate">
            <xsl:value-of select="$current-group/indate"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$current-group/ondate"/>
          </xsl:otherwise>
        </xsl:choose>
      </latedate>

      <sumno>
        <xsl:value-of select="sum($current-group/inno | $current-group/onno)"/>
      </sumno>

      <xsl:apply-templates select="key('items-by-id', name)/*[not(self::name | self::indate | self::inno | self::ondate | self::onno)][generate-id() = generate-id(key('duplicated-elements', concat(../name, '|', local-name()))[1])]"/>
  </xsl:copy>
</xsl:template>


</xsl:stylesheet>