Re: [xsl] Content constructors and sequences

Subject: Re: [xsl] Content constructors and sequences
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 10 Jan 2002 13:25:29 +0000
Hi Mike,

>> Given a node, you can therefore find other nodes in the same
>> document, but you can't find other items in the same sequence.
>
> Which reminds me, that one of the reasons I have resisted allowing
> documentless nodes in XSLT is that people are going to assume that a
> sequence of such nodes are siblings of each other, which of course
> they aren't.

I agree that's what people will assume, but I think that being able to
pass around documentless nodes has its advantages - construct a set of
attributes within a function and then copy them onto an element, for
example.

Also, I think that people are going to assume you can get access to
other items in other sequences too - it already happens with node sets
in XSLT, and with more sequence processing going on it's going to get
a lot worse.

For example, $coordinates holds a sequence of integers, conceptually
pairs of x,y coordinates:

  <xsl:for-each select="$coordinates">
    <xsl:if test="position() mod 2 = 1">
      <xsl:variable name="x" select="." />
      <xsl:variable name="y" select="..." />
      ...
    </xsl:if>
  </xsl:for-each>

How do I get the value for $y?

Possibly solutions are:

  <xsl:for-each select="$coordinates[position() mod 2 = 1]">
    <xsl:variable name="i" select="position()" />
    <xsl:variable name="x" select="." />
    <xsl:variable name="y" select="$coordinates[$i * 2]" />
    ...
  </xsl:for-each>

or:

  <xsl:for-each select="(1 to count($coordinates) div 2)">
    <xsl:variable name="x" select="$coordinates[($i * 2) - 1]" />
    <xsl:variable name="y" select="$coordinates[$i * 2]" />
    ...
  </xsl:for-each>

Neither is particularly inspiring (both involve visiting items in the
same sequence multiple times), and the latter is the only approach if
you're processing the sequence in XPath with a for expression, I
think.

One possibility would be to offer next() and previous() functions that
returned the sequence following and before the current item in the
sequence being processed. For example:

  <xsl:for-each select="$coordinates">
    <xsl:if test="position() mod 2 = 1">
      <xsl:variable name="x" select="." />
      <xsl:variable name="y" select="next()[1]" />
      ...
    </xsl:if>
  </xsl:for-each>

But I haven't thought it through and this would probably cause its own
problems.
  
Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread