Re: [xsl] Re-visiting a Child Node

Subject: Re: [xsl] Re-visiting a Child Node
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 10 Apr 2001 11:36:23 +0100
Hi Daniel,

Thanks for pointing to the samples.  It would be good to see the
filled in values so that it's clearer how the information in the table
should be derived from the information in the XML.

> My only thought at this time is to process the
> RetrieveNameAndAddressRSResponse node first, storing the values I
> want as local variables, and then print them to the page when
> needed?

Remember that XSLT operates on the *tree*.  You don't need to worry
about streaming issues like recording information from further up the
tree to use later on, because the entire tree is *always* accessible
to you from within the XSLT.

> But, I can't see why I can't use my initial function/template to
> generate the table rows:
>
> <xsl:template name="PrintRows">
>         <xsl:param name="ChildNode" />
>         <xsl:param name="SelectNode" />
>         <xsl:param name="RowColor" />
>         <xsl:param name="DisplayText" />
>           <tr>
>             <td bgcolor="{$RowColor}" valign="top" width="45%">
>                 <b><font face="Arial, Helvetica, sans-serif" size="2">
>                         <xsl:value-of select="$DisplayText" />
>                 </font></b>
>             </td>
>                     <td bgcolor="{$RowColor}" width="45%"><font face="Arial, Helvetica,
> sans-serif" size="2">
>                         <xsl:apply-templates select="*[name()=$ChildNode/$SelectNode]" />
>                     </font></td>
>             <td bgcolor="#000066" width="5%"><br/></td>
>             <td bgcolor="#FF9933" width="5%"><br/></td>
>           </tr>
> </xsl:template>
>
> As this would make my code so much easier!

It certainly looks as though having a single template that creates a
row for the table would be a good idea, because of all that messy
deprecated HTML.

Probably the best approach is to have the template like you have it
above, but to actually pass in the value that you want to use in the
table cell as a value rather than trying to retrieve it by applying
templates to the node. So the template would look like:

<xsl:template name="PrintRows">
   <xsl:param name="RowColor" />
   <xsl:param name="DisplayText" />
   <xsl:param name="Value" />
   <tr>
      <td bgcolor="{$RowColor}" valign="top" width="45%">
         <xsl:attribute name="bgcolor">
         </xsl:attribute>
         <b><font face="Arial, Helvetica, sans-serif" size="2">
            <xsl:value-of select="$DisplayText" />
         </font></b>
      </td>
      <td bgcolor="{$RowColor}" width="45%">
         <font face="Arial, Helvetica, sans-serif" size="2">
            <xsl:copy-of select="$Value" />
         </font>
      </td>
      <td bgcolor="#000066" width="5%"><br/></td>
      <td bgcolor="#FF9933" width="5%"><br/></td>
   </tr>
</xsl:template>

You can then call the template, passing the appropriate value in as a
parameter rather than trying to access it from within the table:

<xsl:template match="Shareholder_Summary">
  <table>
  ...
  <xsl:call-template name="PrintRows">
     <xsl:with-param name="RowColor" select="'#FFEBBF'" />
     <xsl:with-param name="DisplayText" select="'Holder Name: '" />
     <xsl:with-param name="Value">
        <xsl:apply-templates
           select="RetrieveNameAndAddressRSResponse/NameAddress1" />
     </xsl:with-param>
  </xsl:call-template>
  ...
  <xsl:call-template name="PrintRows">
     <xsl:with-param name="RowColor" select="'#FFEBBF'" />
     <xsl:with-param name="DisplayText" select="'Holding Balance: '" />
     <xsl:with-param name="Value">
        <xsl:apply-templates
           select="msg_holding_information/items/item/balance" />
     </xsl:with-param>
  </xsl:call-template>
  ...
  </table>
</xsl:template>

But perhaps I'm missing something - do you have some XML elsewhere
that specifies what the table should look like (e.g. order of the
rows, colour of the rows, what the headings should be, what the XPath
to each value is)?
  
I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread