Variables in 2.0

1. Variable access, tree or sequence?

1.

Variable access, tree or sequence?

Xia Li (Lisa)

Given

    <xsl:variable name="itSystems" as="element()*">
        <xsl:for-each select="//system"> 
            <itSystem name="{nameOfSystem}"/>
        </xsl:for-each>
        <xsl:for-each select="//replacedLegacySystem"> 
            <itSystem name="{nameOfLegacySystem}"/>
        </xsl:for-each>
    </xsl:variable>

The type of $itSystems is such that the following Xpath expression works:

	$itSystems[@name='whatever...']

However if I remove the as="element()*" attribute then the expression has to be written as:

	$itSystems/itSystem[@name='whatever...']

What is the formal type of object returned in each case? In the first I believe it is a sequence of element nodes, but in the second there seems to be an additional level of hierarchy.

The presence of the attribute "as" determines whether the value of the variable is evaluated to a sequence or a temporary tree. In this case, with the "as" attribute, the value of the variable is a sequence of the element <itSystem>. Without the "as" attribute, the value of the variable is a document node of a temporary tree, the sequence of <itSystem> are the children of the document node of the temporary tree. That's why you have to use the child axis to locate the <itSystem> elements.

DC adds:

The main difference between the usage with and without as= is that they are (or are not) copies.

If you go

<xsl:variable name="a1">
  <xsl:sequence select="a"/>
</xsl:variable>
<xsl:variable name="a2" as="element()*">
  <xsl:sequence select="a"/>
</xsl:variable>

Then in both cases the a element nodes are siblings, but

in $a1 they are _copies_ of nodes from the input copied into a new temporary tree and so are the only siblings of the new / node at the top of that tree.

in $a2 they are the original a nodes in the source document (so necessarily siblings as they are selected by the xpath "a" so all children of the current node at that point. In this case $a2 holds these nodes, and they are (still) siblings, but they may have other siblings not contained in the variable, and their parent node is similarly not contained in the variable.