Newbie question: XSLT Variable scope

Discussion in 'XML' started by Colin Walls, Oct 9, 2004.

  1. Colin Walls

    Colin Walls Guest

    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.

    --
    Colin Walls
    Removed the pink meat to mail me
    Colin Walls, Oct 9, 2004
    #1
    1. Advertising

  2. 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.

    "Colin Walls" <> wrote in message
    news:ck8971$ir1$1$...
    > 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.
    >
    > --
    > Colin Walls
    > Removed the pink meat to mail me
    Dimitre Novatchev, Oct 9, 2004
    #2
    1. Advertising

  3. Colin Walls

    Joris Gillis Guest

    On Sat, 09 Oct 2004 09:58:19 +0100, Colin Walls
    <> wrote:

    > <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?

    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    Ceterum censeo XML omnibus esse utendum
    Joris Gillis, Oct 9, 2004
    #3
  4. Colin Walls

    Colin Walls Guest

    Colin Walls wrote:

    > 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.

    --
    Colin Walls
    Removed the pink meat to mail me
    Colin Walls, Oct 10, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Benjamin Hillsley
    Replies:
    3
    Views:
    1,647
    Dimitre Novatchev
    Sep 25, 2003
  2. Hoi Wong
    Replies:
    8
    Views:
    1,080
    P. Lepin
    Feb 21, 2008
  3. Steve Cooper
    Replies:
    2
    Views:
    117
    Kenta MURATA
    Jan 18, 2004
  4. David Filmer
    Replies:
    19
    Views:
    216
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    187
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page