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 July 27th, 2005, 11:05 AM
Registered User
 
Join Date: Jul 2005
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Deduplicating a String

I have an element with the value /123/23/22/23/123. I am able to break the string into separate components 123 23 22 23 and 123, but is there a way to remove the duplicates from this element and display them as the output shown below? I am adding both the xml and xslt that I used for it. Please give me some direction on this.

Required Output:

<resource>
<name>123</name>
</resource>
<resource>
<name>23</name>
</resource>
<resource>
<name>22</name>
</resource>

but output that I am getting is:

<resource>
<name>123</name>
</resource>
<resource>
<name>23</name>
</resource>
<resource>
<name>22</name>
</resource>
<resource>
<name>23</name>
</resource>
<resource>
<name>123</name>
</resource>

Here are the programs. Any help on this will be greatly appreciated. Thank you

********************************************
XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<ListOfItems>10/20/30/31/31
</ListOfItems>
*******************************************


*******************************************

XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="ListOfItems">

 <xsl:variable name="SpaceOrCommaOrBoth" select="normalize-space(translate(text(),'/',' '))"/>

  <xsl:call-template name="Chop">
           <xsl:with-param name="SpaceOrCommaOrBoth" select="$SpaceOrCommaOrBoth"/>
  </xsl:call-template>


</xsl:template>

<xsl:template name="Chop">

           <xsl:param name="SpaceOrCommaOrBoth"/>

 <xsl:choose>
  <xsl:when test="contains($SpaceOrCommaOrBoth,' ')">

   <xsl:call-template name="MakeItem">
    <xsl:with-param name="ItemStff" select="substring-before($SpaceOrCommaOrBoth,' ')"/>
   </xsl:call-template>

   <xsl:call-template name="Chop">
    <xsl:with-param name="SpaceOrCommaOrBoth" select="substring-after($SpaceOrCommaOrBoth,' ')"/>
   </xsl:call-template>

  </xsl:when>

  <xsl:otherwise>

   <xsl:call-template name="MakeItem">
            <xsl:with-param name="ItemStff" select="$SpaceOrCommaOrBoth"/>
   </xsl:call-template>

  </xsl:otherwise>

 </xsl:choose>
</xsl:template>

<xsl:template name="MakeItem">

     <xsl:param name="ItemStff"/>
     <xsl:element name="resource">
         <xsl:element name="name"><xsl:value-of select="$ItemStff"/></xsl:element>
    </xsl:element>
</xsl:template>
</xsl:stylesheet>
*******************************************




 
Old July 27th, 2005, 11:44 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Very easy in 2.0: call tokenize() to split the string into parts, and then distinct-values() to remove the duplicates.

In 1.0, all problems involving elimination of duplicates essentially boil down into grouping problems - see Muenchian grouping and other techniques at http://www.jenitennison.com/xslt/grouping

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
how to find a string in another string in vb6 satish_k VB How-To 3 March 30th, 2007 12:17 PM
Casting String array to string Samatha ASP.NET 1.0 and 1.1 Professional 1 December 5th, 2006 07:46 AM
syntax to find a string in a string cole SQL Server 2000 2 October 10th, 2005 06:06 PM
how to replace a string with another string/number crmpicco Javascript How-To 4 March 14th, 2005 12:59 PM





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