xsl-list
[Top] [All Lists]

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

2008-06-05 09:14:03
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.

INPUT

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


XSLT

<?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:copy>
      <xsl:for-each select="for $i in 1 to (count(*) idiv 2 +
(count(*) mod 2)) return *[($i*2)-1]">
        <pair>
          <xsl:copy-of select="." />
          <xsl:copy-of select="./following-sibling::*[1]" />
        </pair>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>


--
John Cavalieri
john(_dot_)cavalieri(_at_)gmail(_dot_)com

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>