[xsl] two at a time, using a sequence expression

Subject: [xsl] two at a time, using a sequence expression
From: "John Cavalieri" <john.cavalieri@xxxxxxxxx>
Date: Thu, 5 Jun 2008 11:13:22 -0500
Hi XSLers,

I'm curious if there is a more elegant way to use a sequence
expression to process two adjacent elements at a time.  With XSLT 1.0
I would have used recursion, but with XSLT/XPath 2.0 I'm wondering if
I can exploit sequences.  Below is something I've come up with so far.

An interesting side note, I thought the sequence expression would
error because of divide by zero, but it appears to be side effect
free, at least with Saxon.


<?xml version="1.0" encoding="UTF-8"?>
  <Abbott />
  <Costello />
  <Amos />
  <Andy />
  <George />
  <Gracie />
  <Einstein />


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">

  <xsl:output indent="yes" />

  <xsl:template match="/pairs">
      <xsl:for-each select="for $i in 1 to (count(*) idiv 2 +
(count(*) mod 2)) return *[($i*2)-1]">
          <xsl:copy-of select="." />
          <xsl:copy-of select="./following-sibling::*[1]" />


John Cavalieri

Current Thread