Re: [xsl] Using values from one node tree to iterate/recurse over another set of nodes. (Newbie Question)

Subject: Re: [xsl] Using values from one node tree to iterate/recurse over another set of nodes. (Newbie Question)
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Wed, 27 Feb 2008 17:35:34 +0100
Richard Dyce wrote:

Nothing too dramatic really -

<table>
  <tr>
    <th>ID</th>
    <th>Headline</th>
    <th>Bodycopy</th>
    <th>Picture</th>
  </tr>
  <tr>
    <td>2</th>
    <td>Fog in Channel, Europe Cut Off</td>
    <td>There was fog in the channel. No French cheese for us.</td>
    <td>cheese.png</td>
  </tr>
  <tr>
    <th>1</th>
    <td>Man Bite Dogs</td>
    <td>Today a man bit a dog</td>
    <td>dog.png</td>
  </tr>
</table>

But the point is that the structure needs to be dynamic...

This stylesheet achieves the described result, additionally adding thead and tbody:


<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="1.0">

<xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <html lang="en">
      <head>
        <title>Example</title>
      </head>
      <body>
        <xsl:apply-templates select="view"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="view">
    <xsl:apply-templates select="record_list"/>
  </xsl:template>

<xsl:template match="record_list">
<h2><xsl:value-of select="@type"/> Listing</h2>
<p>Showing <xsl:value-of select="@begin_record"/> to <xsl:value-of select="@end_record" /> of <xsl:value-of select="@total_found" /> records</p>
<table>
<thead>
<xsl:apply-templates select="structure"/>
</thead>
<tbody>
<xsl:apply-templates select="record"/>
</tbody>
</table>
</xsl:template>


  <xsl:template match="structure">
    <tr>
      <xsl:apply-templates select="field"/>
    </tr>
  </xsl:template>

  <xsl:template match="field">
    <th>
      <xsl:value-of select="@label"/>
    </th>
  </xsl:template>

  <xsl:template match="record">
    <tr>
      <xsl:apply-templates select="*"/>
    </tr>
  </xsl:template>

  <xsl:template match="record/*">
    <td>
      <xsl:value-of select="."/>
    </td>
  </xsl:template>

</xsl:stylesheet>

It processes all child elements of record elements (match="record/*") so in that regard it is dynamic, it does however simply process those elements in the order they appear and does not look at the field elements, those are just used to create the thead.

As at least in your sample the field and record/* elements are in the same order the above might suffice. If not then post back.


--


	Martin Honnen
	http://JavaScript.FAQTs.com/

Current Thread