[xsl] Re: comparing strings

Subject: [xsl] Re: comparing strings
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Tue, 2 Jan 2001 07:03:07 -0800 (PST)
Dimitre Novatchev wrote:
> The node-set() extension function has to be used -- however it will
be
> standard in XSLT 1.1.

OK,
Taking into account that strings do not exist "by themselves" in an XML
document, but are always values of some kind of node (e.g. text,
attribute, comment, namespace) -- then here's a solution that compares
the string value of two nodes -- ***no extension functions are used***.

xml document (testStringCompare.xml):
------------------------------------
<cities>
    <city>Paris</city>
    <city>London</city>
    <city>Sydney</city>
    <city>New York</city>
</cities>


stylesheet(StringCompare.xsl):
----------------------------
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
>

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:variable name="node1" select="/cities/city[3]"/>
  <xsl:variable name="node2" select="/cities/city[2]"/>
  
  <xsl:value-of select="$node1"/>
  <xsl:text> is</xsl:text>
	
  <xsl:call-template name="node-stringCompare">
    <xsl:with-param name="node1" select="$node1"/>
    <xsl:with-param name="node2" select="$node2"/>
  </xsl:call-template>
	
  <xsl:value-of select="$node2"/>
</xsl:template>

<xsl:template name="node-stringCompare">
  <xsl:param name="node1"/>
  <xsl:param name="node2"/>
	
  <xsl:choose>
    <xsl:when test="string($node1)=string($node2)">
      <xsl:text> equal to </xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:for-each select="$node1 | $node2">
        <xsl:sort select="."/>

        <xsl:if test="position()=1">
          <xsl:choose>
            <xsl:when test="string(.) = string($node1)">
              <xsl:text> less than </xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text> greater than </xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:if>
      </xsl:for-each>
    </xsl:otherwise>
  </xsl:choose>
	
</xsl:template>
</xsl:stylesheet>


Result:
--------
Sydney is greater than London


Cheers,
Dimitre Novatchev.


__________________________________________________
Do You Yahoo!?
Yahoo! Photos - Share your holiday photos online!
http://photos.yahoo.com/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread