Solved

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

Posted on 2010-11-22
10
809 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

929 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

11 Experts available now in Live!

Get 1:1 Help Now