Solved

XSLT variables

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
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…
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…
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…

707 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

17 Experts available now in Live!

Get 1:1 Help Now