Re: [xsl] Trouble removing dup elements based on attributes

Subject: Re: [xsl] Trouble removing dup elements based on attributes
From: "Sam D. Chuparkoff" <sdc@xxxxxxxxxx>
Date: Fri, 24 Jun 2005 18:40:18 -0700
Hey, 'Sam', that's me.

On Fri, 2005-06-24 at 23:14 +0000, Al Bean wrote:
> Hi,
> 
> I just started using XSLT today and I'm trying to figure out how to get rid 
> of some duplicated elements.  I know that I need to do something to the 
> "XXX" template but I am not sure what to do.
> 
> I've tried this for the XXX template but it does not work:
> <xsl:template mode="XXX" match="attrib[@name='Names']/rs/row">
>     <xsl:if test="not(@firstname = 
> preceding::attrib[@name='Names']/rs/row/@firstname)">
>         <xsl:element name="ns:name">
>             <xsl:value-of select="@firstname"/>
>         </xsl:element>
>     </xsl:if>
>   </xsl:template>

You should include output using this template and explain why it
confuses you.

>From the xpath spec (1.0):

  the preceding axis contains all nodes in the same document as the
  context node that are before the context node in document order,
  excluding any ancestors and excluding attribute nodes and namespace
  nodes

So preceding::attrib[@name='Names'] won't match the ancestor attrib of
the current row. It will match all previous attrib[@name='Names'] in
the document. But this means matching attrib s from other products,
which clearly isn't what you want.

If all the rows you need to consider are really siblings (as in your
input), your test should be:

not(@firstname=preceding-sibling::row/@firstname)

Also, I don't know why you're using xsl:element most (but not all) of
the time. Don't use xsl:element unless you have to (which means: unless
you don't know the name of the element).

And you might like to look into trying:

 <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
  version="1.0" xmlns="http://w.ns.c/ns/prod/";>

And then just writing:

<Products/>

Just didn't want you to assume namespaces have to be so clunky.

There are some faqs about removing duplicates here:

http://www.dpawson.co.uk/xsl/sect2/N2696.html

sdc

> 
> Thanks in advance for any help.
> 
> 
> 
> 
> ----------------------
> My XSL
> ----------------------
> 
> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
> version="1.0" xmlns:ns="http://w.ns.c/ns/prod/";>
>     <xsl:template match="/">
>         <ns:Products>
>             <xsl:apply-templates/>
>         </ns:Products>
>     </xsl:template>
> 
>     <xsl:template match="product">
>         <xsl:element name="ns:Product">
>             <xsl:element name="ns:Names">
>                 <xsl:apply-templates mode="XXX" 
> select="attrib[@name='Names']/rs/row"/>
>             </xsl:element>
>         </xsl:element>
>     </xsl:template>
> 
>     <xsl:template mode="XXX" match="attrib[@name='Names']/rs/row">
>         <xsl:element name="ns:name">
>             <xsl:value-of select="@firstname"/>
>         </xsl:element>
>     </xsl:template>
> </xsl:transform>
> 
> 
> ----------------------------------------------------
> Input XML :
> ----------------------------------------------------
> 
> <prods>
>     <product>
>         <attrib name="P">product 1</attrib>
>         <attrib name="Names">
>             <rs>
>                 <row firstname="Bill"/>
>                 <row firstname="Bill"/>
>             </rs>
>         </attrib>
>     </product>
>     <product>
>         <attrib name="P">product 2</attrib>
>         <attrib name="Names">
>             <rs>
>                 <row firstname="Sam"/>
>                 <row firstname="Ron"/>
>                 <row firstname="Sam"/>
>                 <row firstname="Ron"/>
>             </rs>
>         </attrib>
>     </product>
>     <product>
>         <attrib name="P">product 3</attrib>
>         <attrib name="Names">
>             <rs>
>                 <row firstname="Ron"/>
>                 <row firstname="Sam"/>
>                 <row firstname="Joe"/>
>                 <row firstname="Sam"/>
> 
>             </rs>
>         </attrib>
>     </product>
> </prods>
> 
> ----------------------------------------------------
> This is the OUTPUT that I want (note no name dups):
> ----------------------------------------------------
> 
> <ns:Products xmlns:ns="http://w.ns.c/ns/prod/";>
>     <ns:Product>
>         <ns:Names>
>             <ns:name>Bill</ns:name>
>         </ns:Names>
>     </ns:Product>
>     <ns:Product>
>         <ns:Names>
>             <ns:name>Sam</ns:name>
>             <ns:name>Ron</ns:name>
>         </ns:Names>
>     </ns:Product>
>     <ns:Product>
>         <ns:Names>
>             <ns:name>Ron</ns:name>
>             <ns:name>Joe</ns:name>
>             <ns:name>Sam</ns:name>
>         </ns:Names>
>     </ns:Product>
> </ns:Products>
> 
> 
> ----------------------------------------------------
> But this is the OUTPUT that I get (note the name dupes):
> ----------------------------------------------------
> <ns:Products xmlns:ns="http://w.ns.c/ns/prod/";>
>     <ns:Product>
>         <ns:Names>
>             <ns:name>Bill</ns:name>
>             <ns:name>Bill</ns:name>
>         </ns:Names>
>     </ns:Product>
>     <ns:Product>
>         <ns:Names>
>             <ns:name>Sam</ns:name>
>             <ns:name>Ron</ns:name>
>             <ns:name>Sam</ns:name>
>             <ns:name>Ron</ns:name>
>         </ns:Names>
>     </ns:Product>
>     <ns:Product>
>         <ns:Names>
>             <ns:name>Ron</ns:name>
>             <ns:name>Sam</ns:name>
>             <ns:name>Joe</ns:name>
>             <ns:name>Sam</ns:name>
>         </ns:Names>
>     </ns:Product>
> </ns:Products>
> 
> _________________________________________________________________
> FREE pop-up blocking with the new MSN Toolbar  get it now! 
> http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/

Current Thread