Solved

XSLT variables

Posted on 2006-06-15
4
508 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
[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
  • 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

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…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

751 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