Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 24th, 2005, 02:04 PM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default having trouble Removing Dups

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:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>


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>


 
Old July 6th, 2005, 06:21 AM
Authorized User
 
Join Date: May 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to meetnaveen4u
Default

Hi Tyler,
Please use this .
   <xsl:template mode="XXX" match="attrib[@name='Names']/rs/row">
        <xsl:element name="ns:name">
            <xsl:if test="current()[not(@firstname = preceding-sibling::row/@firstname)]">
            <xsl:value-of select="@firstname"/>
            </xsl:if>
        </xsl:element>
    </xsl:template>
Hope it solves u r problem.


Thanks &amp; Regards
Naveen.
 
Old July 6th, 2005, 07:09 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You can simplify this:

<xsl:if test="current()[not(@firstname = preceding-sibling::row/@firstname)]">

as this:

<xsl:if test="not(@firstname = preceding-sibling::row/@firstname)">


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing duplicates hewstone999 Access VBA 2 November 4th, 2008 05:26 PM
removing duplicates havey C# 9 February 26th, 2008 02:05 PM
Removing Namespace Pankaj C XSLT 2 September 22nd, 2007 03:23 AM
Removing an element izbor XSLT 3 May 8th, 2006 11:00 PM
Removing Tables p_nut33 ADO.NET 1 February 18th, 2004 04:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.