Solved

XSLT variables

Posted on 2006-06-15
4
493 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

775 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