Re: [xsl] A nice way to implement the ANDing of the comparison of pairs of items in a sequence?

Subject: Re: [xsl] A nice way to implement the ANDing of the comparison of pairs of items in a sequence?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 05 Sep 2013 15:12:57 -0400
At 2013-09-05 18:51 +0000, Costello, Roger L. wrote:
The value of this variable is a sequence of strings:

<xsl:variable name="checkPairs"
select="('Sally', 'Sally', 'Beth', 'Beth', 'Mary', 'Mary')" as="xs:string*"/>


I want to compare consecutive items and then AND the result of the comparisons.

That is, compare the first string to the second string, the third string to the fourth string, the fifth string to the sixth string, and so on. Then AND the results together.

Remember that an "AND" is the same as "not( OR )" (DeMorgan's Law) and XSLT 2 has a built-in "OR" with the comparison of a singleton to a sequence.


Thus,

('Sally' eq 'Sally') and ('Beth' eq 'Beth') and ('Mary' eq 'Mary')

returns true.

But if the variable is this:

<xsl:variable name="checkPairs"
select="('Sally', 'Beth', 'Beth', 'Beth', 'Mary', 'Mary')" as="xs:string*"/>


the result is false since

('Sally' eq 'Beth') and ('Beth' eq 'Beth') and ('Mary' eq 'Mary')

is false.

Is there a nice way to implement this?

I hope the code below helps.


. . . . . . . . Ken

t:\ftemp>xslt2 roger.xsl roger.xsl
For: Sally Sally Beth Beth Mary Mary: true
For: Sally Beth Beth Beth Mary Mary: false

t:\ftemp>type roger.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:xs="http://www.w3.org/2001/XMLSchema";
                version="2.0">

<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:call-template name="checkPairs">
    <xsl:with-param name="checkPairs"
         select="('Sally', 'Sally', 'Beth', 'Beth', 'Mary', 'Mary')"
         as="xs:string*"/>
  </xsl:call-template>
  <xsl:call-template name="checkPairs">
    <xsl:with-param name="checkPairs"
         select="('Sally', 'Beth', 'Beth', 'Beth', 'Mary', 'Mary')"
         as="xs:string*"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="checkPairs">
  <xsl:param name="checkPairs" as="xs:string*"/>
  <xsl:text>For: </xsl:text>
  <xsl:value-of select="$checkPairs"/>
  <xsl:text>: </xsl:text>
  <xsl:value-of
    select="not( false() =
                 ( for $n in 1 to count($checkPairs) div 2
                   return ( $checkPairs[$n*2-1]=$checkPairs[$n*2] ) ) )"/>
  <xsl:text>&#xa;</xsl:text>
  </xsl:template>

</xsl:stylesheet>
t:\ftemp>

--
Public XSLT, XSL-FO, and UBL classes in the Netherlands     Oct 2013 |
Public XSLT, XSL-FO, UBL and code list classes in Australia Oct 2013 |
Contact us for world-wide XML consulting and instructor-led training |
Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm |
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/s/ |
G. Ken Holman                   mailto:gkholman@xxxxxxxxxxxxxxxxxxxx |
Google+ profile: https://plus.google.com/116832879756988317389/about |
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal |

Current Thread