[xsl] mixing recursive template calls and for-each

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

Hello,


I hope i have not missed an obvious answer in the list.

I have got the following XML file :

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

<place:list-of node="workitem"> <!-- iterate over the workitem node which 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>

The <source> node has been generated and introduced inside the <place:list-of> node. 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>

Here his an example of the template i could manage to write...

<xsl:template match="place:list-of">
<!-- the node to iterate on -->
<xsl:variable name="node" select="@node" />
<!-- the nodes which are not under source node, that will be copied -->
<xsl:variable name="present"
select=".//*[not( ancestor-or-self::source)]" />
<!-- iterate on workitems -->
<xsl:for-each select="source//*[name() = $node]">
<!-- keep info on the current workitem -->
<xsl:variable name="currentWI" select="xalan:nodeset(.)" />
<!-- go through the nodes to treat -->
<xsl:for-each select="$present">
<xsl:choose>
<!-- if it is a place:value-of, replace with the value in the workitem -->
<xsl:when test="name() = 'place:value-of'">
<xsl:value-of select="$currentWI/*[name() = current()/@name]" />
</xsl:when>
<xsl:otherwise>
<!-- else, how to copy as is other nodes ? -->
<!-- for-each and apply-templates brought no success -->
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:for-each>
</xsl:template>


Here is my problem, for each given node under <source>, i would like to generate the tags inside place:list-of and at the same time change place:value-of for the corresponding value.

Of course, <place:list-of> is intended to be generic and should work for any tags under <source> and i don't know at what depth i will find <place:value-of> tags...

Any hint whould be very welcome.

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