views:

32

answers:

1

Hello,

i would like to get distinct nodes from my xml on multiple levels. Can anyone please give me some hints how to do this? The methods i googled (Muenchian method, for-each-group) were explained with single grouping keys and plain hearchie.

Here's an example of my xml:

<persons>
 <person>
  <name>Tom</name>
  <age>20</age>
  <mails>
   <mail>[email protected]</mail>
   <mail>[email protected]</mail>
  </mails>
 </person>
 <person>
  <name>Tom</name>
  <age>20</age>
  <mails>
   <mail>[email protected]</mail>
   <mail>[email protected]</mail>
  </mails>
 </person> 
</persons>

I would like to have distinct person nodes based on name and age, and also a distinct set of mail-nodes. So for the example the desired output would be:

<persons>
 <person>
  <name>Tom</name>
  <age>20</age>
  <mails>
   <mail>[email protected]</mail>
   <mail>[email protected]</mail>
   <mail>[email protected]</mail>
  </mails>
 </person>
</persons>

Is there a way to do this? Thanks a lot in advance.

+3  A: 

I. XSLT 1.0 solution:

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kPersByNameAndAge" match="person"
  use="concat(name, '+', age)"/>

 <xsl:key name="kmailByNameAndAge" match="mail"
  use="concat(../../name, '+', ../../age)"/>

 <xsl:key name="kmailByNameAndAgeAndVal" match="mail"
  use="concat(../../name, '+', ../../age, '+', .)"/>

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

 <xsl:template match="/*">
  <persons>
   <xsl:apply-templates select=
   "person[generate-id()
          =
           generate-id(key('kPersByNameAndAge',
                          concat(name, '+', age)
                          )
                           [1]
                      )
           ]
   "/>
  </persons>
 </xsl:template>

 <xsl:template match="mails">
  <mails>
   <xsl:apply-templates select=
    "key('kmailByNameAndAge', concat(../name, '+', ../age))
        [generate-id()
        =
         generate-id(key('kmailByNameAndAgeAndVal',
                         concat(../../name, '+', ../../age, '+', .)
                         )
                          [1]
                     )
         ]
    "/>
  </mails>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<persons>
 <person>
  <name>Tom</name>
  <age>20</age>
  <mails>
   <mail>[email protected]</mail>
   <mail>[email protected]</mail>
  </mails>
 </person>
 <person>
  <name>Tom</name>
  <age>20</age>
  <mails>
   <mail>[email protected]</mail>
   <mail>[email protected]</mail>
  </mails>
 </person>
</persons>

produces the wanted, correct result:

<persons>
    <person>
        <name>Tom</name>
        <age>20</age>
        <mails>
            <mail>[email protected]</mail>
            <mail>[email protected]</mail>
            <mail>[email protected]</mail>
        </mails>
    </person>
</persons>

II. XSLT 2.0 solution

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kmailByNameAndAge" match="mail"
  use="concat(../../name, '+', ../../age)"/>

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

 <xsl:template match="/*">
  <persons>
   <xsl:for-each-group select="person" group-by="concat(name, '+', age)">
     <xsl:apply-templates select="."/>
   </xsl:for-each-group>
  </persons>
 </xsl:template>

 <xsl:template match="mails">
  <mails>
   <xsl:for-each-group select=
    "key('kmailByNameAndAge', concat(../name, '+', ../age))"
    group-by="concat(../../name, '+', ../../age, '+', .)"
    >
     <xsl:apply-templates select="."/>
   </xsl:for-each-group>
  </mails>
 </xsl:template>
</xsl:stylesheet>
Dimitre Novatchev
@Dimitre: +1 Beatifull solution. About `mails` template: I think that `for-each-group/group` could be just `"."`. I've worked in a XSLT 2.0 solution but it was such a "brick" template...
Alejandro
Thanks a lot, thats it.
Mork0075