Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old March 11th, 2008, 02:58 PM
Registered User
Join Date: Feb 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Accessing nodes contained in a variable


I've created the following variable using xslt 1.0:
<xsl:variable name="colors">
        <color r="00" g="ff" b="00"/>
        <color r="ff" g="ff" b="00"/>
        <color r="ff" g="77" b="00"/>

But i can't seem to access the individual color elements correctly:

<xsl:template name="newObject">
        <xsl:param name="colors" select="$colors"/>
    <xsl:param name="i"/>
    <xsl:param name="count"/>
    <xsl:if test="$i &lt;= $count">
           <object name="{concat('Object', $i)}">
        <tests name="tests">
           <xsl:for-each select="//Test">
              <a x="1" y="0" z="0"/>
              <xsl:copy-of select="$colors/colors/color[$i]"/>
    <xsl:if test="$i &lt;= $count">
    <xsl:call-template name="newObject">
       <xsl:with-param name="colors" select="$colors"/>
       <xsl:with-param name="i">
          <xsl:value-of select="$i + 1"/>
       <xsl:with-param name="count">
        <xsl:value-of select="$count"/>

     <xsl:copy-of select="$colors/colors/color[$i]"/>
doesn't seem to work but if I use the following:
     <xsl:copy-of select="$colors/colors/color[2]"/>
I am able to index the correct color element. Any help would be appreciated.

Old March 11th, 2008, 06:53 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

In two messages already today I have criticized people for using the long-winded and inefficient construct

<xsl:variable name="x">
  <xsl:value-of select="y"/>

rather than the shorter and faster

<xsl:variable name="x" select="y"/>

This habit seems to be impossible to eradicate, I've no idea why so many people write 3 lines of code where 1 does the job much better.

In your case it's not only verbose and inefficient, it's wrong. Change

       <xsl:with-param name="i">
          <xsl:value-of select="$i + 1"/>


       <xsl:with-param name="i" select="$i + 1"/>

The resulting variable will be a number rather than a result tree fragment. When used in a predicate [$i], a number is treated as meaning [position()=$i] (which you intended), whereas anything else is treated as a boolean - and in the case of a result tree fragment, the boolean value is always true.

There's another problem though: if $colors is a result tree fragment then in XSLT 1.0 you aren't allowed to probe inside it using a path expression such as $colors/colors/color. It's allowed in XSLT 2.0, and some 1.0 processors let you get away with it, but most don't, and it's an error according to the spec.

Michael Kay
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Old March 12th, 2008, 02:57 PM
Registered User
Join Date: Feb 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts

Ok, thanks mhkay, I'll have to try and come up with a way to avoid violating the specs.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing higher level nodes from a template mikeymikey XSLT 3 January 3rd, 2008 09:14 PM
problem accessing nodes using document() and id() siris phi XSLT 3 April 12th, 2007 11:05 PM
XSLT for Variable Child Nodes jlagedo XSLT 2 September 10th, 2006 03:00 AM
Help accessing specific nodes jshl_wiz Javascript 2 June 29th, 2005 09:45 PM
Accessing a variable from different context meetjag JSP Basics 1 January 19th, 2005 05:33 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.