[Home] [By Thread] [By Date] [Recent Entries]

Subject: Re: duplicate occurances within strings
From: JBryant@xxxxxxxxx
Date: Thu, 16 Dec 2004 15:51:25 -0600
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
 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

Current Thread
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member