Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 24th, 2005, 02:04 PM
Registered User
 
Join Date: Jun 2005
Location: , , .
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>


Reply With Quote
  #2 (permalink)  
Old July 6th, 2005, 06:21 AM
Authorized User
 
Join Date: May 2005
Location: bangalore, karnataka, India.
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.
Reply With Quote
  #3 (permalink)  
Old July 6th, 2005, 07:09 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:37 AM.


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