Re: [xsl] mixing recursive template calls and for-each

Subject: Re: [xsl] mixing recursive template calls and for-each
From: Yvan Peter <Yvan.Peter@xxxxxxxxxxxxxx>
Date: Thu, 04 Jul 2002 14:15:09 +0200

Thanks very much, it works.


I fell into the "all in one template" solution while struggling with my problem...

I could not think of passing the current item all along during the process.

Also i thought xsl:with-param could only be used with xsl:call-template.

Let me recall the problem and its solution for those interested :

Original file :

<page xmlns:place="http://place.noce.fr/1.0";>

<place:list-of node="workitem"> <!-- iterate over the workitem node which is inside source -->
<source>
<workitems>
<workitem>
<name>wi_1</name>
<description>desc_wi_1</description>
</workitem>
<workitem>
<name>wi_2</name>
<description>desc_wi_2</description>
</workitem>
</workitems>
</source>


<link>
<text><place:value-of name="description" /></text> <!-- replace with value of description node of workitem -->
<url><place:value-of name="name" /></url>
</link>
</place:list-of>


</page>

I would like to transform this file into an XML file which looks like this :

<page>
 <link>
   <text>desc_wi_1</text>
   <url>wi_1</url>
 </link>
 <link>
   <text>desc_wi_2</text>
   <url>wi_2</url>
 </link>
</page>

The correct stylesheet has the following templates :

  <xsl:template match="place:list-of">
    <xsl:variable name="element" select="@node" />
    <xsl:variable name="items"
                  select="source//*[name() = $element]" />
    <xsl:variable name="template" select="*[position() >= 2]" />
    <xsl:for-each select="$items">
      <xsl:apply-templates select="$template">
        <xsl:with-param name="item" select="." />
      </xsl:apply-templates>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="place:value-of">
    <xsl:param name="item" select="/.." />
    <xsl:variable name="element" select="@name" />
    <xsl:value-of select="$item/*[name() = $element]" />
  </xsl:template>

  <xsl:template match="node()|@*">
    <xsl:param name="item" select="/.." />
    <xsl:copy>
      <xsl:apply-templates select="node()|@*">
        <xsl:with-param name="item" select="$item" />
      </xsl:apply-templates>
   </xsl:copy>
 </xsl:template>


Yvan


--
------------------------------------------------------------------
| Yvan Peter                    | phone (33) 3.20.43.32.64       |
| CUEEP/Laboratoire TRIGONE     | fax   (33) 3.20.43.32.79       |
| Bat. B6                       | mail Yvan.Peter@xxxxxxxxxxxxxx |
| Cite Scientifique             |                                |
| 59655 Villeneuve d'Ascq Cedex |                                |
------------------------------------------------------------------


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



Current Thread