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
  #1 (permalink)  
Old August 1st, 2005, 12:09 AM
Registered User
Join Date: Aug 2005
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default <xsl:for-each> with a variable select="attribute"

Can anyone provide any advice on how to achieve this an XSLT.

        <xsl:when test="//customfield[@name='LHSMenu']">
            <xsl:variable name="menulisting" select="//customfield[@name='LHSMenu']/sections/rwplistitem[@lvl='1']/sections/rwplistitem" />
            <xsl:variable name="menulisting" select="/rwpdoc/rwplist[@name='stdmenu']/rwplistitem[@lvl='2']" />

    <xsl:for-each select="$menulisting">


I don't believe you are able to use a variable in the select attribute of a <xsl:for-each>. Any suggestions on work arounds? Aside from repeating the for-each code twice within the <xsl:when/otherwise>...

Many thanx in advance!
  #2 (permalink)  
Old August 1st, 2005, 03:36 AM
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

Instead of

  when c1
    variable x select=1
    variable x select=2

you can in most cases do

variable x
     when c1 1
     otherwise 2

The exception (which unfortunately applies here) is that the resulting variable is always a result-tree-fragment, which means you can't use this technique to select existing nodes.

When the variable is a node-set, you can do:

xsl:for-each select="EXP1[C1] | EXP2[not(C1)]"

that is, process the nodes selected by EXP1 if C1 is true, otherwise process the nodes selected by EXP2.

Looking at your specific conditions, you could reduce this to:

<xsl:for-each select="
/rwpdoc[not(//customfield[@name='LHSMenu'])]/rwplist[@name='stdmenu']/rwplistitem[@lvl='2']" ">...

In XSLT 2.0, of course, this all becomes vastly easier. For example, you could declare the variable like this:

<xsl:variable name="menulisting" select="
if (//customfield[@name='LHSMenu'])
then //customfield[@name='LHSMenu']/sections/rwplistitem[@lvl='1']/sections/rwplistitem
else /rwpdoc/rwplist[@name='stdmenu']/rwplistitem[@lvl='2']"/>

Michael Kay
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

Similar Threads
Thread Thread Starter Forum Replies Last Post
When <xsl:value-of select="Tooltip" /> = value ismailc XSLT 3 October 17th, 2008 04:43 AM
<xsl:attribute> vs {} berna_isct XSLT 1 April 6th, 2006 03:30 PM
name attribute for <xsl:element> rushman XSLT 2 June 9th, 2005 09:04 AM
HELP: XSL -> HTML <select selected=true> jedbartlett XSLT 4 October 7th, 2004 11:16 PM
problem using <xsl:attribute> arvin XSLT 2 July 21st, 2003 03:13 AM

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