[xsl] Imagine that the semantics of comparison operations was this

Subject: [xsl] Imagine that the semantics of comparison operations was this
From: "Roger L Costello costello@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 2 Mar 2025 15:24:41 -0000
Hi Folks,

Here is XPath that compares two string variables to determine if they differ:

$FIRST ne $SECOND

The result of evaluating the expression is true or false.

The programming language called String Oriented Symbolic Language (SNOBOL) can
also compare two string variables to determine if they differ. SNOBOL has a
built-in function-DIFFER-for this. Here's how the comparison is expressed:

DIFFER(FIRST, SECOND)

If the FIRST and SECOND variables are not different, no value is returned and
statement execution terminates. If FIRST and SECOND are different, then the
null string is returned. That later part (returning a null string) is
interesting because null strings usually vanish from computations without
having any effect. The typical example is concatenation of the null string
onto another string. The result is the same as if the null string had not been
there. For this reason, a comparison operation can be inserted into an
expression conditional on the success of the comparison operation.

As an example of this, suppose we want to do this: If FIRST and SECOND are
different, then assign to variable APPEND the concatenation of FIRST and
SECOND. If FIRST and SECOND are not different, then leave APPEND unaffected.
Here's how to do it in SNOBOL:

APPEND = DIFFER(FIRST, SECOND) FIRST SECOND

If FIRST and SECOND differ, then DIFFER(FIRST,SECOND) returns the null string
and APPEND is assigned the concatenation of the null string, the value of
FIRST, and the value of SECOND. In SNOBOL, concatenation is such a common
operation that simply juxtaposing two strings means concatenation. If FIRST
and SECOND do not differ, then execution of the statement containing
DIFFER(FIRST,SECOND) terminates and APPEND is unaffected.

To achieve the same functionality with XSLT and XPath, we would do this:

<xsl:variable name="isDifferent" select="$FIRST ne $SECOND" as="xs:boolean"/>
<xsl:variable name="APPEND" as="xs:string?">
    <xsl:if test="$isDifferent eq true()">
        <xsl:sequence select="concat($FIRST, $SECOND)"/>
    </xsl:if>
</xsl:variable>

Lessons Learned: how we define the semantics of comparison operations can have
a profound influence on programming style. Thus, if you are creating a new
programming language, be aware that there are other ways to define the
semantics of comparison operations-you might decide to define the semantics as
XSLT/XPath does--true or false--but alternatively, you might decide to define
the semantics as SNOBOL does-terminate the statement or null string.

/Roger

P.S. I got the information about SNOBOL from the wonderful book, "A SNOBOL4
Primer" by Ralph E. Griswold and Madge T. Griswold, pages 18-19. Some of the
above sentences are excerpts from the book.

Current Thread