This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: mixing recursive template calls and for-each


Hi Yvan,

As Mike said, you'll probably find this easier to manage if you have
separate templates for different elements. The basic template that you
need is an identity template that copies everything from the source to
the result:

<xsl:template match="node()|@*">
  <xsl:copy>
    <xsl:apply-templates select="node()|@*" />
  </xsl:copy>
</xsl:template>

Then you need to handle the elements in your place namespace
differently, so you need separate templates for them. When you find a
place:list-of element, you want to locate the elements named by its
'node' attribute within the source element child of the place:list-of
element, in order to create the list:

<xsl:template match="place:list-of">
  <xsl:variable name="element" select="@node" />
  <xsl:variable name="items"
                select="source//*[name() = $element]" />
  ...
</xsl:template>

Then you want to iterate over the items in that list, using the second
element child of the place:list-of element as a template. To do that,
you should apply templates to that element, passing the item element
(in this case the 'workitem' element) as a parameter:

<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="*[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>

The place:value-of element needs to handle the $item passed as a
parameter in order to get the value of the child element named by its
name attribute:

<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>

Finally, to make sure that the place:value-of elements actually get
the $item parameter set, you need to make sure that the $item
parameter is passed through the templates as you process the elements,
by amending the identity template as follows:

<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>

Cheers,

Jeni

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


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]