Newbie question: XSLT Variable scope

C

Colin Walls

Fencing tournaments use a programme called Engarde to run tournaments. One
of the features of the programme is the generation of a "transfer" file in
XML format. This contains lines with ranking information.

<Tireur ID="17" Nom="AJAMI" Prenom="Rashid" DateNaissance="00.04.1990"
Sexe="M" Club="SUSSEX HOUSE" Licence="46379" Classement="12" Statut="N" />

I want to transform the set of ranks into an HTML file, with the medal
winning positions highlighted using a CSS class statement on a <TR> tag to
do this.

The easiest thing would weem to be to create a variable inside an
<xsl:choose> and put the name of the class into this. One could then use
<xsl:attribute> to set the class. Unfortunately the variable only seems to
have scope inside the <xsl:choose> and I am reduced to the following:

<xsl:choose>
<xsl:when test="@Classement = $first">
<tr class="first">
<xsl:apply-templates select="@Classement" />
<xsl:apply-templates select="@Nom" />
<xsl:apply-templates select="@Prenom" />

<xsl:choose>
<xsl:when test="@Club != $empty_string">
<xsl:apply-templates select="@Club" />
</xsl:when>

<xsl:eek:therwise>
<td> </td>
</xsl:eek:therwise>
</xsl:choose>
</tr>
</xsl:when>

<xsl:when test="@Classement = $second">
<tr class="second">
<xsl:apply-templates select="@Classement" />

etc.

I would be grateful to anyone who could point me to a better way. I can
provide a full copy of the XML file and my XSLT file if required.
 
D

Dimitre Novatchev

This is a FAQ.

A variable is set conditionally by either a smart XPath expression in its
"select" attribute, or, more commonly, by using an xsl:choose *in the body*
of the variable.

For example:

<xsl:variable name="vClass">
<xsl:choose>
<xsl:when test="@Classement = $first">first</xsl:when>
<xsl:when test="@Classement = $second">second</xsl:when>
<xsl:eek:therwise>someDefaultValue</xsl:eek:therwise>
</xsl:choose>
</xsl:variable>

Now, this variable can be used to generate output like this:

<tr class="{$vClass}">
</tr>


Hope this helped.


Cheers,

Dimitre Novatchev.
 
J

Joris Gillis

<Tireur ID="17" Nom="AJAMI" Prenom="Rashid" DateNaissance="00.04.1990"
Sexe="M" Club="SUSSEX HOUSE" Licence="46379" Classement="12"
Statut="N" />

I want to transform the set of ranks into an HTML file, with the medal
winning positions highlighted using a CSS class statement on a <TR> tag
to
do this.

Hi,
I wouldn't use any variables to solve the problem.
I think this is the easiest way:

<tr class="rank{@Classement}">
<td><xsl:apply-templates select="@Classement"/></td>
<td><xsl:apply-templates select="@Nom"/></td>
<td><xsl:apply-templates select="@Prenom/></td>
<td><xsl:apply-templates select="@Club"/></td>
</tr>

an example css could then be:

td.rank1 {color:gold;font-size:x-large}
td.rank2 {color:silver;font-size:large}


Is this of any use?
 
C

Colin Walls

Colin said:
Fencing tournaments use a programme called Engarde to run tournaments. One
of the features of the programme is the generation of a "transfer" file in
XML format. This contains lines with ranking information.

<Tireur ID="17" Nom="AJAMI" Prenom="Rashid" DateNaissance="00.04.1990"
Sexe="M" Club="SUSSEX HOUSE" Licence="46379" Classement="12" Statut="N"
/>

I want to transform the set of ranks into an HTML file, with the medal
winning positions highlighted using a CSS class statement on a <TR> tag to
do this.


Thanks for the two answers to my query. A combination of both is what I
needed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top