xsl-list
[Top] [All Lists]

Re: grouping probs

2002-09-16 03:23:00
I do not know what the attribute would be called like in my example , for one set , its called 'type', and for the other set, its called 'catagory'..
so what i want to do is..
go through each element, and if it has the same attribute value and same set of children..( children with same attribute value and same set of their children), then eliminate the duplicate.
example
<element-name att = "att-name1">
    <child>
         <name>childname1</name>
         <age>childage1</age>
    </child>
    <child>
         <name>childname2</name>
         <age>childage2</age>
     </child>
    <child>
          <name>childname1</name>
         <age>childage1</age>
    </child>
</element-name>
<element-name att = "att-name1">
    <child>
         <name>childname1</name>
         <age>childage1</age>
    </child>
    <child>
         <name>childname2</name>
         <age>childage2</age>
     </child>
    <child>
          <name>childname1</name>
         <age>childage1</age>
    </child>
</element-name>
<element-name second-att = "second-att-name1">
    <child>
         <name>childname1</name>
         <age>childage1</age>
    </child>
    <child>
         <name>childname2</name>
         <age>childage2</age>
     </child>
    <child>
          <name>childname1</name>
         <age>childage1</age>
    </child>
</element-name>



the result shud be..
<element-name att = "att-name1">
    <child>
         <name>childname1</name>
         <age>childage1</age>
    </child>
    <child>
         <name>childname2</name>
         <age>childage2</age>
     </child>
    <child>
          <name>childname1</name>
         <age>childage1</age>
    </child>
</element-name>

<element-name second-att = "second-att-name1">
    <child>
         <name>childname1</name>
         <age>childage1</age>
    </child>
    <child>
         <name>childname2</name>
         <age>childage2</age>
     </child>
    <child>
          <name>childname1</name>
         <age>childage1</age>
    </child>
</element-name>

Thanks.

From: Jeni Tennison <jeni(_at_)jenitennison(_dot_)com>
Reply-To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
To: "Laura Jenkins" <xsl_list(_at_)hotmail(_dot_)com>
CC: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] grouping probs
Date: Mon, 16 Sep 2002 10:40:46 +0100

Hi Laura,

> Essentially it should eleminate the duplicates out of "any" element
> it comes across.i cant give conditions based on a fixed element. so
> if the xsl comes across elements with same "id" and "text()".. then
> it should eleminate the duplicating element "where ever found in the
> document"

Set up a key that matches all elements that doesn't hold any child
elements and assigns them a value based on their id (presumably an id
attribute? Or did you mean the name of the element?) and string value:

<xsl:key name="duplicates" match="*[not(*)]"
         use="concat(@id, '+', string())" />

Use an identity template to do a recursive copy of the whole document:

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

and override this template for elements that don't have element
children:

<xsl:template match="*[not(*)]">
  ...
</xsl:template>

You only want to copy these elements if they are the first element in
the document with that id and value. Use the classic Muenchian test to
see whether they're the first element in the document with that id and
string value:

<xsl:template match="*[not(*)]">
  <xsl:if test="generate-id() =
                generate-id(key('duplicates',
                                concat(@id, '+', string()))[1])">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
  </xsl:if>
</xsl:template>

And there you go.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list




_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx


XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>