Solved

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

Posted on 2010-11-22
10
813 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 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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
 

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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

738 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