[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
> standard in XSLT 1.1.

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):
    <city>New York</city>

<xsl:stylesheet version='1.0'

<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:value-of select="$node2"/>

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

        <xsl:if test="position()=1">
            <xsl:when test="string(.) = string($node1)">
              <xsl:text> less than </xsl:text>
              <xsl:text> greater than </xsl:text>

Sydney is greater than London

Dimitre Novatchev.

Do You Yahoo!?
Yahoo! Photos - Share your holiday photos online!

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

Current Thread