RE: [xsl] newbie prob - Template and navigation confusion???

Subject: RE: [xsl] newbie prob - Template and navigation confusion???
From: "Nick Roberts" <nick_roberts5@xxxxxxxxxxx>
Date: Thu, 03 Feb 2005 16:32:35 +0000
Thanks for the speedy response.

[quote]
apply-templates select=X

template match=X[condition]

template match=X[not(condition)]
[/quote]

That makes perfect sense, except i'm not too sure how to apply a condition that way. I'm only aware of xsl:choose, xsl:when and xsl:otherwise logic. How can a condition be applied in a template match?

From: "Michael Kay" <mike@xxxxxxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: RE: [xsl] newbie prob - Template and navigation confusion???
Date: Thu, 3 Feb 2005 14:39:32 -0000

Instead of writing

if (condition)
  apply-templates select=X mode=A
else
  apply-templates select=X mode=B

template match=X mode=A

template match=X mode=B

write instead:

apply-templates select=X

template match=X[condition]

template match=X[not(condition)]

That is: let the template rules do the work.

You haven't shown your source document but it seems a little odd. Usually
documents with a ROWSET and ROW are rather flat, they tend only to have one
level of element below the ROW. You appear to have

TEAM/TEAM_ROW/NAME/NAME_ROW/SALARY

within each ROW. Can each of these elements only appear once in each row? If
so, why all the nesting? But if not, your stylesheet logic looks wrong,
because the apply-templates processes all the elements in the set, while the
when condition tests if ANY element satisfies the condition. (But it's
possible this is what you intended, who knows?)


Michael Kay
http://www.saxonica.com/



> -----Original Message-----
> From: Nick Roberts [mailto:nick_roberts5@xxxxxxxxxxx]
> Sent: 03 February 2005 13:44
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] newbie prob - Template and navigation confusion???
>
> I'm quite new the XSL and what i've have produced so far has
> taken some
> considerable time. I'm slowly getting used to the way
> templates work but i'm
> still having some probs with my code.
>
> In the below code, there is a 'when' condition which states
> that the when
> the salary is greater than 30000, match the 'dark' template..
> which sets the
> background colour of a table-box to grey.
>
> Although this sort-of works, it seems to pick random boxes to
> turn grey,
> some which aren't above 30000, and others which are. Also,
> some which are
> over 30000 aren't turned grey??
>
> I presume it must be something to do with the way i'm
> navigating through the
> xml document.
>
> Can anyone spot any problems with the code?
>
> Cheers, Nick
>
>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>
>   <xsl:template match="page">
>     <html>
>       <head><title>Footy stuff</title></head>
>        <body>
>         <h1>Football Report</h1><hr/>
>          <xsl:for-each select="ROWSET/ROW">
>           <h2><xsl:apply-templates
> select="TEAM/TEAM_ROW/TEAMNAME"/></h2>
>           <h3>Stadium: <xsl:apply-templates select="STADIUM"/></h3>
>            <table border="1">
>             <th><b>Last Name</b></th><th><b>Salary</b></th>
>             <xsl:choose>
>              <xsl:when
> test="TEAM/TEAM_ROW/NAME/NAME_ROW/SALARY&gt;'30000'">
>              <xsl:apply-templates
> select="TEAM/TEAM_ROW/NAME/NAME_ROW"
> mode="dark"/>
>              </xsl:when>
>              <xsl:otherwise>
>              <xsl:apply-templates
> select="TEAM/TEAM_ROW/NAME/NAME_ROW"
> mode="light"/>
>             </xsl:otherwise>
>             </xsl:choose>
> 	   </table>
>           <xsl:call-template name="break"/>
>          </xsl:for-each>
>       </body>
>     </html>
> </xsl:template>
>
>   <xsl:template match="NAME_ROW" mode="dark">
>    <tr>
>     <td><xsl:value-of select="LAST_NAME"/></td><td
> bgcolor="gray"><xsl:value-of select="SALARY"/></td>
>    </tr>
>   </xsl:template>
>
>   <xsl:template match="NAME_ROW" mode="light">
>    <tr>
>     <td><xsl:value-of select="LAST_NAME"/></td><td><xsl:value-of
> select="SALARY"/></td>
>    </tr>
>   </xsl:template>
>
>   <xsl:template name="break">
>    <hr/>
>   </xsl:template>
>
> </xsl:stylesheet>

Current Thread