xsl-list
[Top] [All Lists]

Re: duplicate occurances within strings

2004-12-16 21:39:24
Don't know if my solution is easier, but here it is:

<?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(_at_)gmail(_dot_)com> 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(_at_)s-s-t(_dot_)com <JBryant(_at_)s-s-t(_dot_)com> 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(_at_)gmail(_dot_)com>
12/16/2004 02:59 PM
Please respond to
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com

To
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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



--~------------------------------------------------------------------
XSL-List info and archive: 
http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to:
http://lists.mulberrytech.com/xsl-list/
or e-mail:
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive: 
http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to:
http://lists.mulberrytech.com/xsl-list/
or e-mail:
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive: 
http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to:
http://lists.mulberrytech.com/xsl-list/
or e-mail:
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--