Subject: RE: Filtering/Removing Truly Duplicate Elements
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 18 Jul 2007 20:09:27 +0100
|
Removing duplicates is essentially the same problem as grouping (you group
the elements and then output the first one in each group). So for XSLT 2.0
you can use xsl:for-each-group, or if you really have a very good reason to
stick with 1.0 you can use Muenchian grouping, see
http://www.jenitennison.com/xslt/grouping.
In 2.0 it's
<xsl:for-each-group select="//xs:element" group-by="concat(@name, '|',
@type)">
<xs:element name="{current-group()[1]/@name}"
type="{current-group()[1]/@type}"/>
</xsl:for-each-group>
Michael Kay
http://www.saxonica.com/
> -----Original Message-----
> From: Wasiq Shaikh [mailto:wasiq911@xxxxxxxxxxx]
> Sent: 18 July 2007 19:36
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Filtering/Removing Truly Duplicate Elements
>
> Hello everyone,
>
> I have searched everywhere for a similar problem but most
> problems are actually very simple with simple solutions. But
> the techniques are the same.
> Except I cant seem to put differenct peices together. When i
> run my XSL, the desired result is not desired! :)
>
> The Objective:
> Remove truly duplicate elements, not just by name but by its
> attributes and attribute values.
>
> The Example:
> I have an xml input file (which is actually an XSD) with
> various <xsd:element>'s which may have many attributes.
> The attributes I care about are "name" and "type". Below is a
> sample input
> file:
>
> <xsd:element name="ABC" type="123" ... >
> <xsd:element name="def" type="456" .../>
> <xsd:element name="ghi" type="456" .../> </xsd:element>
>
> <xsd:element name="ABC" type="456" ...>
> <xsd:element name="ghi">
> <xsd:element name="def" type="456" .../>
> <xsd:element name="def"/>
> </xsd:element>
> <xsd:element name="ABC"/>
> <xsd:element name="ABC"/>
> <xsd:element name="ghi" type="456" .../> </xsd:element>
>
> The desired result should be a flat list of elements:
>
> <uniqueElements>
> <element name="ABC"/>
> <element name="ABC" type="123"/>
> <element name="ABC" type="456"/>
> <element name="def"/>
> <element name="def" type="456"/>
> <element name="ghi"/>
> <element name="ghi" type="456"/>
> </uniqueElements>
>
> The order of the result doesn't matter. But understand that
> all those elements are distinctly unique. I'm using Xalan
> Java 2.7/XPath 1.0.
>
> My XSL Logic:
> Since i need a flat list i will interate through "//xsd:element".
> So, for every element encountered, check to see if there are
> any elements before it with the same name, and same type.
>
> Sounds simple enough! But it isn't. I cant get the right
> combination of conditional logic with correct XPath syntax to
> execute this task. Maybe this task can not be done at all? I
> would like to know first if I'm wasting my time trying to
> figure this out or if there is any hope.
>
> Please help as I'm sure this will benefit anyone else wanting
> to do the same task. Thank you!
>
> Wasiq Shaikh
>
> _________________________________________________________________
> Windows Live Hotmail. Even hotter than before. Get a better look now.
> www.newhotmail.ca?icid=WLHMENCA148
|