[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: (Possible) pitfall: XSLT 2, 9.4 Creating implicit

Subject: RE: (Possible) pitfall: XSLT 2, 9.4 Creating implicit document nodes
From: Jakub Malý <jakub@xxxxxxx>
Date: Wed, 9 May 2012 17:21:58 +0200
RE:  (Possible) pitfall: XSLT 2
You can give assign a type to v1 (and v2 as well) to make both variables
behave the same. Notice, however, that the value of select in
apply-templates was changed as well (this time, both variables hold a
sequence, not a temporary tree). 

  <xsl:template match="/">
    <xsl:variable name="v1" as="item()*">
      <xsl:sequence select="a"/>
    <xsl:variable name="v2" as="item()*" select="a"/>

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

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

You can choose the type of the variables to be item(), element(), item()* or
element()*, whichever serves best your needs.

> -----Original Message-----
> From: Christian Roth [mailto:roth@xxxxxxxxxxxxxx]
> Sent: Wednesday, May 09, 2012 5:02 PM
> To: XSL List
> Subject:  (Possible) pitfall: XSLT 2, 9.4 Creating implicit document
> Hello,
> this is a heads-up for a pitfall (that at least I have fallen into several
> now...) with respect to XSLT 2, 9.4 "Creating implicit document nodes":
> <http://www.w3.org/TR/xslt20/#temporary-trees>
> With the source document:
> --src.xml--
> <a>
>   <b/>
> </a>
> -----------
> and this transformation
> --transformation.xsl--
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>   xmlns:xs="http://www.w3.org/2001/XMLSchema"
>   exclude-result-prefixes="xs"
>   version="2.0">
>   <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>&#xa;</xsl:text>
>     <xsl:text>v2: </xsl:text>
>     <xsl:apply-templates select="$v2/a" mode="out"/>
>   </xsl:template>
>   <xsl:template match="*" mode="out">
>     <xsl:value-of select="name()"/>
>     <xsl:apply-templates mode="#current"/>
>   </xsl:template>
>   <xsl:template match="text()" mode="#all"/> </xsl:stylesheet>
> -----------
> the output is:
> --output--
> v1: ab
> v2:
> ----------
> 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
> defined using <xsl:sequence> (because some complex content construction
> takes place which is either not doable or hardly readable using a single
> 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>?
> Thanks,
> Christian

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.