Solved

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

Posted on 2010-11-22
10
804 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
  • 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 18

Accepted Solution

by:
zc2 earned 450 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
 
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 50 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 18

Expert Comment

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

Expert Comment

by:Geert Bormans
ID: 34192393
welcome
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to count occurrences of each item in an array.

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now