Re: [xsl] Need better ways to do this..

Subject: Re: [xsl] Need better ways to do this..
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Fri, 1 Jun 2001 11:17:47 +0100
Hi Sudhir,

> 1. How to prevent the output being generated for td[4], td[5], td[6] and
> td[7] if td[1] fails my validation as shown in the XSL segment?

At the moment, all your templates are matching the td elements in your
source, which makes it hard to control which tr elements are processed
and which aren't.

Instead, you should have a template matching the tr element and
choosing whether or not the td elements inside it should be processed:

<xsl:template match="tr">
   <xsl:variable name="currentDate" select="td[1]"/>
   <xsl:variable name="isValid"
                                         string( $currentDate ))" />
   <xsl:if test="$isValid = 'YES'">
      <!-- If valid, print the timestamp within XData tag -->
      <XData><xsl:value-of select="$currentDate"/></XData>
      <!-- Then go for other values in tr/td[4], tr/td[5], tr/td[6]
           and tr/td[7] -->
      <xsl:apply-templates select="td[4] | td[5] | td[6] | td[7]" />
   <!-- otherwise nothing is output for this row -->

> 2. How to avoid repitition of the XSL code for processing td[4],
> td[5], td[6] and td[7]?

You can merge the templates into one using the | to separate the
individual patterns in the match attribute:

<xsl:template match="tr/td[4] | tr/td[5] | tr/td[6] | tr/td[7]">
         <xsl:when test=". = 'NA'">-9999</xsl:when>
         <xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>

In fact, using the template that I gave for the tr element above means
that the *only* td elements that get templates applied to them are the
fourth, fifth, sixth and seventh within the row.  So you could
simplify the match pattern to:

<xsl:template match="td">
         <xsl:when test=". = 'NA'">-9999</xsl:when>
         <xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread