Solved

XSLT variables

Posted on 2006-06-15
4
499 Views
Last Modified: 2013-11-18
Hi,

I'm new to XSLT, just using it to transform some simple stuff in Java.

I'm making a table, and I want every other entry to have a colored background. However, every other entry doesn't mean every other row. There are nested for-each statements to select data. A small example might help:

  <xsl:for-each select="Skill">
     <tr>
        <!-- stuff to populate the row -->
     </tr>
     <xsl:for-each select="Student">
        <tr>
           <!-- more stuff -->
        </tr>


So, I want every other entry to be colored - that is every other loop through the outer "for-each" to be colored (and all the loops through the inner for-each to have the same color as the outer).

This is what I've tried

<xsl:variable name="posn" select="(position() mod 2)" />
   <xsl:if test="$posn = 1">
      <xsl:attribute name="style">background: #ffffff</xsl:attribute>
   </xsl:if>
   <xsl:if test="$posn = 0">
      <xsl:attribute name="style">background: #e4eef7</xsl:attribute>
   </xsl:if>

This works for the outer loop, but when I add the if's to the inner loop, it says it can't resolve $posn. I guess I don't understand XSLTs scoping rules.

I tried other ways (i.e. just testing the position and making the value of the background a variable), but it's the same variable problem.

Any help?
0
Comment
Question by:phavardel
  • 3
4 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16912798
Hi phavardel,

if you put the variable definition inside the outer loop
it should be accessible in the inner loop
so something else is the problem I guess

maybe you have to post the full example
(or the relevant part of it

Cheers!
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 75 total points
ID: 16912895
phavardel,

I made a little example

<?xml version="1.0" encoding="UTF-8"?>
<skills>
    <skill>
        <student>A</student>
        <student>B</student>
        <student>C</student>
    </skill>
    <skill>
        <student>D</student>
        <student>E</student>
        <student>F</student>
    </skill>
    <skill>
        <student>G</student>
        <student>H</student>
        <student>I</student>
    </skill>
</skills>

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <html>
            <body>
                <table>
                    <xsl:for-each select="//skill">
                        <xsl:variable name="bgrd">
                            <xsl:choose>
                                <xsl:when test="position() mod 2 = 1">#ffffff</xsl:when>
                                <xsl:otherwise>#e4eef7</xsl:otherwise>
                            </xsl:choose>
                        </xsl:variable>
                        <xsl:for-each select="student">
                            <tr bgcolor="{$bgrd}"><xsl:value-of select="."/></tr>
                        </xsl:for-each>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

I hope this comes close to what you need
you see that I use the $bgrd in the inner loop
the scope of the variable is the entire outer xsl:for-each

cheers

Geert
0
 
LVL 1

Author Comment

by:phavardel
ID: 16913045
Got it,

The problem was I had the xsl:variable declaration nested under a <tr> tag in the outer loop. Once I moved it to just under the <for-each it worked like a charm.

Thanks muchly.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16913491
welcome
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
SharePoint 2013 Blog Template 2 53
XML filtering Windows Event Viewer 10 61
Create XML 5 52
removing a class in javascript 4 47
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
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…
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…

840 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