xsl-list
[Top] [All Lists]

Re: duplicate occurances within strings

2004-12-16 20:14:48
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>
--~--