Solved

XSLT variables

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET XML Processing a XMLNodeList with Namespace 16 67
I starting with php 12 115
CSS HELP 6 84
Problem to picture file 3 68
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

867 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

12 Experts available now in Live!

Get 1:1 Help Now