[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] (Possible) pitfall: XSLT 2, 9.4 Creating implicit document nodes

Subject: [xsl] (Possible) pitfall: XSLT 2, 9.4 Creating implicit document nodes
From: Christian Roth <roth@xxxxxxxxxxxxxx>
Date: Wed, 9 May 2012 17:02:14 +0200


this is a heads-up for a pitfall (that at least I have fallen into several
times now...) with respect to XSLT 2, 9.4 "Creating implicit document nodes":


With the source document:


and this transformation

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

  <xsl:template match="/">
    <xsl:variable name="v1"><xsl:sequence select="a"/></xsl:variable>
    <xsl:variable name="v2" select="a"/>

    <xsl:text>v1: </xsl:text>
    <xsl:apply-templates select="$v1/a" mode="out"/>

    <xsl:text>v2: </xsl:text>
    <xsl:apply-templates select="$v2/a" mode="out"/>

  <xsl:template match="*" mode="out">
    <xsl:value-of select="name()"/>
    <xsl:apply-templates mode="#current"/>

  <xsl:template match="text()" mode="#all"/>

the output is:

v1: ab

I understand now why this is so per the spec, although I was puzzled at

However, I've spent considerable time debugging stylesheets where accessing a
tunnel variable with $var/elem sometimes yielded nothing (see v2 in my example
above) until I found that in some places, the variable was defined using
<xsl:sequence> (because some complex content construction takes place which is
either not doable or hardly readable using a single XPath expression), and
using a select attribute at other places.

Is there a technique or pattern I could employ (maybe utilizing the @as
attribute somehow?) to unify the access to variable contents where I know that
the sequences are node sequences, regardless of their content construction
using @select or <xsl:sequence>?


Current Thread