Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Selecting new variable from list for each iteration of for-each loop

Posted on 2010-11-22
10
Medium Priority
?
819 Views
Last Modified: 2013-11-18
Hi All,

I'm trying to include in my XSLT file a variable containing multiple elements which I need to use during a for-each loop. The variable could be as below in theory:

<xsl:variable name="colorPalette">
   <color value="red"/>
   <color value="blue"/>
   <color value="green"/>
</xsl:variable>

During a for-each loop, I need to use the values - a different one for each iteration. Here's an example of how I thought it might work based on a similar solution I found:

<xsl:for-each select="...">
   <set>
      <xsl:attribute name="color">
         <xsl:for-each select="$colorPalette/color[not(@value = preceding::*/@value)]">
            <xsl:value-of select="@value"/>
         </xsl:for-each>
      </xsl:attribute>
   </set>
</xsl:for-each>

What would be the best way to accomplish this? The main for-each loop needs to remain a loop, but the variable process could use templates or some other approach. Thanks in advance!
0
Comment
Question by:PrecisionDotNet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 17

Expert Comment

by:sweetfa2
ID: 34191468
<set color="redbluegreen"/>
<set color="redbluegreen"/>

or


<set color="red"/>
<set color="blue"/>
<set color="green"/>
0
 
LVL 19

Accepted Solution

by:
zc2 earned 1800 total points
ID: 34191518
If you're using a MSXML processor, you can give a try to msxsl:node-set() extension, which converts the variable value to a node-set.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
	<xsl:template match="/root">
		<xsl:variable name="colorPalette">
		   <color value="red"/>
		   <color value="blue"/>
		   <color value="green"/>
		</xsl:variable>
		
		<xsl:for-each select="item">
		   <set>
		   	<xsl:variable name="p" select="position()"/>
		      <xsl:attribute name="color">
		       <xsl:value-of select="msxsl:node-set($colorPalette)/color[$p]/@value"/>
		      </xsl:attribute>
		   </set>
		</xsl:for-each> 
	</xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 17

Expert Comment

by:sweetfa2
ID: 34191582
      <xsl:for-each select=".">
      	<xsl:for-each select="$colorPalette/color">
	      <xsl:variable name="index" select="position()"/>
         <set>
	         	<xsl:attribute name="pos">
	         		<xsl:value-of select="$index"/>
	         		</xsl:attribute>
            <xsl:attribute name="color">
	            <xsl:value-of select="$colorPalette/color[$index]/@value"/>
            </xsl:attribute>
         </set>
         </xsl:for-each>
      </xsl:for-each> 

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34191591
This will only work in XSLT2.0 I am afraid.
Or you need to use an extension
a lot of processors support exslt:nodeset()

If you put some XML tags in a variable,
you can't simply use that as a lookup using XPath
because the XML elements are a Result Tree Fragment,
and XSLT1 does not cast a RFT to a node-set() automatically
(XSLT2 does)

In order for me to show you a solution that would possibly allow this,
I need to know which processor you are using.
Is this .net? let me know

Other options are
- having the values for the variable in a seperate XML document and use document() to get it
- put the values in a string with a seperator and make a recursive function to loop over the string (bit awkward, but it works)
- start using XSLT2 (there is a saxon.net version)
0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 200 total points
ID: 34191651
Coming in late I see,
- zc2 is spot on (as usual), offering a solution for msxml
- sweetfa2 fails to explain that his solution only works in XSLT2 (Saxon9)

I have adapted zc2's solution for working with Xalan, Saxon6, LibXSLT and 4Suite XSLT
(just changed the use of node-set in a different namespace, have not checked the XSLT, trusting that one)
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="xmlns:exslt="http://exslt.org/common" exclude-result-prefixes="exsl">
<xsl:output method="xml" indent="yes"/>
	<xsl:template match="/root">
		<xsl:variable name="colorPalette">
		   <color value="red"/>
		   <color value="blue"/>
		   <color value="green"/>
		</xsl:variable>
		
		<xsl:for-each select="item">
		   <set>
		   	<xsl:variable name="p" select="position()"/>
		      <xsl:attribute name="color">
		       <xsl:value-of select="exsl:node-set($colorPalette)/color[$p]/@value"/>
		      </xsl:attribute>
		   </set>
		</xsl:for-each> 
	</xsl:template>
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:PrecisionDotNet
ID: 34191754
Thanks for the responses! Sorry I didn't give more details in my question. This would be run on a SharePoint server (specifically Microsoft's SharePoint Online servers), the XSLT file stored in a document library and read in using JavaScript.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34191789
Then you are using msxml and zc2's solution should work
0
 

Author Closing Comment

by:PrecisionDotNet
ID: 34192052
It works like a charm! Thanks for the answers and explanations.
0
 
LVL 19

Expert Comment

by:zc2
ID: 34192129
you're welcome
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34192393
welcome
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to count occurrences of each item in an array.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question