Re: [xsl] problems with match

Subject: Re: [xsl] problems with match
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 25 Jul 2002 11:39:02 +0100
Hi Jochen,

> this is quite a simple problem, but I don't understand it.
> Why does this not work?
>
> <xsl:template match="/">
>     the main body
>     <xsl:call-template name="title"/>
> </xsl:template>
>
> <xsl:template name="title" match="/root/output">
>     <b>Auftrag #<xsl:value-of select="data/ordernr" /> (<xsl:value-of
> select="data/shopname" />)       </b>
> </xsl:template>

When you call a template by name, the current node at the point where
you call the template (in your case, the root node since the template
call is in a template that's processing a root node) is still the
current node in the called template. So when it's called by name, all
the paths in your 'title' template will be interpreted relative to the
root node. So if you want to call the template by name then it should
look like:

<xsl:template name="title">
  <b>Auftrag #<xsl:value-of select="root/output/data/ordernr" />
  (<xsl:value-of select="root/output/data/shopname" />)       </b>
</xsl:template>

Adding a match attribute doesn't change the current node, it just
means that the template will be used if you ever apply templates to
the node that the template matches. If you applied templates to the
output element then the template would be used to process that output
element, the output element would be the current node, and the paths
would be resolved relative to that node.

If you want the template to stay the same you should either change the
call to the template so that the current node at the point of the call
is an output element:

<xsl:template match="/">
  the main body
  <xsl:for-each select="root/output">
    <xsl:call-template name="title"/>
  </xsl:for-each>
</xsl:template>

or you should "call" the template by apply templates to the output
element:

<xsl:template match="/">
  the main body
  <xsl:apply-templates name="root/output"/>
</xsl:template>

Cheers,

Jeni

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


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


Current Thread