Re: [xsl] about using templates

Subject: Re: [xsl] about using templates
From: Peter Davis <pdavis152@xxxxxxxxx>
Date: Tue, 11 Jun 2002 20:34:11 -0700
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tuesday 11 June 2002 16:49, 林 子芯 wrote:
> the problem is, if key2 contain no data (ie <xsl:template
> name="key2"></xsl:template>) the output will becoming <p>key234HrsWft</p>
> and <p></p> and <p>soome key</p>
> therefore, i was wondering, how could i selectively add the " and " string
> into the output file (e.g. only add the " and " string if key2 is not
> empty). i have tried using <xsl:if test="key2!=''"> but it does not work,

Right, "key2!=''" tests the element <key2/> in the source XML, which doesn't 
exist (the test would always be true).  Named templates have no other use 
than to be called with <xsl:call-template/>.

So, since the only way to get the contents of the template is to use 
<xsl:call-template/>, you can do just that:

<xsl:variable name="key2">
  <xsl:call-template name="key2"/>
</xsl:variable>

and then use the $key2 variable in the test:

<xsl:if test="$key2 != ''">
  <p><xsl:value-of select="$key2"/></p>
</xsl:if>

And of course you can do the same with the other key1 and key3 templates.



If possible, it might also be easier to define variables, instead of defining 
named templates, in the first XSL file:

<xsl:variable name="key1">fault/unit=key234HrsWft</xsl:variable>
<xsl:variable name="key2">data</xsl:template>
<xsl:variable name="key3">some key</xsl:template>

And then you can just use <xsl:value-of select="$keyX"/> anywhere you would 
have used <xsl:call-template name="keyX"/>.  Or, if the variables/templates 
might contain XML elements (not just text), use <xsl:copy-of select="$keyX"/> 
instead of <xsl:value-of/>.  Anyway, doing this would make your life a little 
easier and also make the transformation a little more efficient.

Remember that a variable declaration can contain all of the same commands as a 
named template, so as long as the named template doesn't use any 
<xsl:param>'s, they are pretty much equivilant as far as what you can do with 
them, except variables are easier to access.

- -- 
Peter Davis
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9BsEzNSZCJx7tYycRAv0UAKCqswfDhqzZVPnWKKNCX7jscFXZZgCgvN7Q
bPDFyPsapseA/wxPl2o3cZE=
=1j1g
-----END PGP SIGNATURE-----


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


Current Thread