Re: [xsl] How does one merge sequences?

Subject: Re: [xsl] How does one merge sequences?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Mon, 11 Oct 2004 10:03:24 +0100
Hi Kenneth,

> ....indicating that the "merged" sequence is still recognized as two
> sequences instead of a single merged one. How can one do a proper
> merge?

It's not that the sequences are still separate, as such. In your code,
you create a sequence of five elements, two from one document and
three from another. Just because the sequence contains all those
elements doesn't mean that they've been moved into another document.
Importantly, they still have their original siblings. So if you do:

  count(preceding-sibling::item) = 0

then it's true for two elements: the first element in the first
document and the first element in the second document.

If you want to create a new document that contains all the elements
from both documents, then you can do something like:

  <xsl:variable name="merged">
    <xsl:sequence select="$seq1/* | $seq2/*" />
  </xsl:variable>

Note that the elements in the new document are *copies* of the
elements from the original documents.

If you want to create the sequence of the original elements, as in:

  <xsl:variable name="merged" select="$seq1/* | $seq2/*" />

and test whether you're on the first one in that sequence, then use
the position() function:

  <xsl:for-each select="$merged">
    <xsl:value-of select="." />
    <xsl:if test="position() = 1"> = first</xsl:if>
    <xsl:text>&#xA;</xsl:text>
  </xsl:for-each>

By the way, note that if you merge sequences with the union operator
then there's no guarantee that the elements from the first document
you create will actually be before the elements from the second
document you create in the sequence. (Per the spec, the order has to
be consistent throughout the transformation but it's implementation
dependent what the ordering of nodes from different documents actually
is.) So if you care about the order of the elements in the merged
sequence, it's better to use the "," operator to merge the sequences,
as in:

  $seq1/*, $seq2/*
  
Cheers,

Jeni

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

Current Thread