Re: [xsl] duplicate occurances within strings

Subject: Re: [xsl] duplicate occurances within strings
From: "Arun Sinha" <arunsinha666@xxxxxxxxxxx>
Date: Fri, 17 Dec 2004 04:56:16 +0000
Hi Mukul,

Don't know if my solution is easier, but here it is:

Though the initial post was not started by me but as I am in learning process, I used to try out the new logics being posted on this list.

I executed the code you posted but it isn't working.

"Hello" appears twice.

Cheers.

Arun



<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<xsl:output method="text" />

<xsl:variable name="string" select="'Hello, Hello,
Hello, test, dog, cat, cat'" />

<xsl:template match="/">
   <xsl:choose>
     <xsl:when test="not(contains($string, ','))">
       <xsl:value-of select="$string" />
     </xsl:when>
     <xsl:otherwise>
       <!-- call remove-duplicates template, only if
the string contains at-least 2 words -->
       <xsl:call-template name="remove-duplicates">
         <xsl:with-param name="string"
select="translate($string, ' ', '')" />
         <xsl:with-param name="newstring" select="''"
/>
       </xsl:call-template>
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template name="remove-duplicates">
   <xsl:param name="string" />
   <xsl:param name="newstring" />

   <xsl:choose>
     <xsl:when test="$string = ''">
       <xsl:value-of select="$newstring" />
     </xsl:when>
     <xsl:otherwise>
       <xsl:if test="contains($newstring,
substring-before($string, ','))">
         <xsl:call-template name="remove-duplicates">
           <xsl:with-param name="string"
select="substring-after($string, ',')" />
           <xsl:with-param name="newstring"
select="$newstring" />
         </xsl:call-template>
       </xsl:if>
       <xsl:if test="not(contains($newstring,
substring-before($string, ',')))">
         <xsl:variable name="temp">
           <xsl:if test="$newstring = ''">
             <xsl:value-of
select="substring-before($string, ',')" />
           </xsl:if>
           <xsl:if test="not($newstring = '')">
	     <xsl:value-of select="concat($newstring, ',',
substring-before($string, ','))" />
           </xsl:if>
         </xsl:variable>
         <xsl:call-template name="remove-duplicates">
           <xsl:with-param name="string"
select="substring-after($string, ',')" />
           <xsl:with-param name="newstring"
select="$temp" />
         </xsl:call-template>
       </xsl:if>
     </xsl:otherwise>
   </xsl:choose>

</xsl:template>

</xsl:stylesheet>

Regards,
Mukul

--- Christopher Hansen <chansen1@xxxxxxxxx> wrote:

> Jay,
> Wow.  Lots of code there.  Thank you though...just
> wish there were an
> easier way to accomplish the task!
>
> Thanks
> Chris
>
>
> On Thu, 16 Dec 2004 15:51:25 -0600,
> JBryant@xxxxxxxxx <JBryant@xxxxxxxxx> wrote:
> > We had a very similar question last week, except
> that that person was
> > getting the string from a node in the document and
> didn't care about a
> > string-compare function. Assuming you also don't
> care about a
> > string-compare function, here's a modification of
> last week's answer that
> > does the job with a recursive template. If you
> really do want to use a
> > string-compare function, someone else will have to
> answer your question,
> > as I don't use MSXSL.
> >
> > Note that my solution is case-sensitive, so Hello,
> hello, cat and Cat are
> > all different words.
> >
> > Jay Bryant
> > Bryant Communication Services
> >
> > <?xml version="1.0" encoding="UTF-8"?>
> > <xsl:stylesheet
> >   version="1.0"
> >   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> > >
> > <xsl:output
> >   method="text"
> >   omit-xml-declaration="yes"
> >   indent="no"
> > />
> >
> >   <xsl:template match="/">
> >     <xsl:variable name="theString" select="Hello,
> Hello, Hello, test, dog,
> > cat, cat"/>
> >     <xsl:call-template name="makeList">
> >       <xsl:with-param name="textIn"
> select="translate($theString, ',',
> > '')"/>
> >       <xsl:with-param name="wordsSoFar"/>
> >     </xsl:call-template>
> >   </xsl:template>
> >
> >   <xsl:template name="makeList">
> >     <xsl:param name="textIn"/>
> >     <xsl:param name="wordsSoFar"/>
> >     <xsl:choose>
> >       <xsl:when test="contains($textIn, ' ')">
> >         <xsl:variable name="firstWord"
> select="substring-before($textIn, '
> > ')"/>
> >         <xsl:choose>
> >           <xsl:when
> test="not(contains($wordsSoFar, $firstWord))">
> >             <xsl:variable name="newString">
> >               <xsl:choose>
> >                 <xsl:when
> test="string-length($wordsSoFar)=0">
> >                   <xsl:value-of
> select="$firstWord"/>
> >                 </xsl:when>
> >                 <xsl:otherwise>
> >                   <xsl:value-of
> select="$wordsSoFar"/><xsl:text>,
> > </xsl:text><xsl:value-of select="$firstWord"/>
> >                 </xsl:otherwise>
> >               </xsl:choose>
> >             </xsl:variable>
> >             <xsl:call-template name="makeList">
> >               <xsl:with-param name="textIn"
> > select="substring-after($textIn, ' ')"/>
> >               <xsl:with-param name="wordsSoFar"
> select="$newString"/>
> >             </xsl:call-template>
> >           </xsl:when>
> >           <xsl:otherwise>
> >             <xsl:call-template name="makeList">
> >               <xsl:with-param name="textIn"
> > select="substring-after($textIn, ' ')"/>
> >               <xsl:with-param name="wordsSoFar"
> select="$wordsSoFar"/>
> >             </xsl:call-template>
> >           </xsl:otherwise>
> >         </xsl:choose>
> >       </xsl:when>
> >       <xsl:otherwise>
> >         <xsl:choose>
> >           <xsl:when
> test="string-length($wordsSoFar)=0">
> >             <xsl:value-of select="$textIn"/>
> >           </xsl:when>
> >           <xsl:otherwise>
> >             <xsl:choose>
> >               <xsl:when
> test="contains($wordsSoFar, $textIn)">
> >                 <xsl:value-of
> select="$wordsSoFar"/>
> >               </xsl:when>
> >               <xsl:otherwise>
> >                 <xsl:value-of
> select="$wordsSoFar"/><xsl:text>,
> > </xsl:text><xsl:value-of select="$textIn"/>
> >               </xsl:otherwise>
> >             </xsl:choose>
> >           </xsl:otherwise>
> >         </xsl:choose>
> >       </xsl:otherwise>
> >     </xsl:choose>
> >   </xsl:template>
> >
> > </xsl:stylesheet>
> >
> > Christopher Hansen <chansen1@xxxxxxxxx>
> > 12/16/2004 02:59 PM
> > Please respond to
> > xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> >
> > To
> > xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> > cc
> >
> > Subject
> > [xsl] duplicate occurances within strings
> >
> >
> > If i have a variable $thestring   containing the
> following string:
> > "Hello, Hello, Hello, test, dog, cat, cat"
> >
> > Is there a way to use the string-compare function
> to parse it and
> > check for duplicates within the string, and then
> possibly remove those
> > extra occurrances...resulting in the string
> "Hello, test, dog, cat"
> >
> > Thanks
> > Chris
>
>




__________________________________ Do you Yahoo!? Check out the new Yahoo! Front Page. www.yahoo.com


_________________________________________________________________
Chat with 1000s of singles. http://www.bharatmatrimony.com/cgi-bin/bmclicks1.cgi?74 Let BharatMatrimony.com's Instant Messenger show you how.


Current Thread