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]

Re: Applying a named template to arbitrary content


Thanks very much for your help.

>> I'm trying to make a generic template which will, in effect,
>> simply insert stuff before and after the content. In one case, I
>> want to do it to apply font styling parameters etc. where needed:
> This is hard to do in XSLT 1.0, but quite a few people have asked for it,
> so maybe evaluating the name of the template to call will come in a future
> version of the language.

Hmm, this doesn't seem to be exactly what I want, because it would still lack the flexibility of being able to put anything inside a call to a named template, e.g.

<call-template name=fontstyling>
  ...
  text
  ...
  apply-templates
  ...
  call to another template
  ...
  apply-templates
  ...
</call-template>

and simply have whatever was generated be inserted into the template (via a param if necessary).

> For now, one approach is that of Mike Kay --- you can check the archive
> for details, but the meat of it is this:
> <xsl:stylesheet version="1.0"
>  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
> <xsl:template name="xyz" match="xsl:template[@name='xyz']">
>   <xsl:message>Hello xyz.</xsl:message>
> </xsl:template>
>
> <xsl:template match="/">
> <xsl:variable name="tname" select="'xyz'"/>
< <xsl:apply-templates select="document('')/*/xsl:template[@name=$tname]"/>
> </xsl:template>
>
> </xsl:stylesheet>

This has the same problem as I mentioned, unfortunately (besides the yuck factor).

> The other approach is to rethink your problems and solutions. For example,
> much of what you seem to want to do could probably also be achieved by use
> of xsl:attribute-set, or increased use of CSS in combination with 'class'
> attributes on your HTML output.

I am definitely open to any solution to the problem, but unfortunately this doesn't work for me since attributes can't get you <B> </B> <I> </I> tags etc, and I can't use CSS since support for older browsers is a must.


I am very surprised that there is not a straightforward way to insert stuff into a template like this in XSL, it seems that it is:
- computationally trivial
- conceptually very simple
- simple to represent syntactically
- extremely useful

After re-reading the spec for the nth time, it appeared as if my idea of using a variable would work (passing an RTF to the template as a parameter), but for some reason, I can't seem to apply templates within the variable.
I don't understand why the two generated sections aren't identical in the following:

XSL:

<xsl:template name="foo">
  Template foo was called.
</xsl:template>

<xsl:template name="fontstyle">
  <xsl:param name="param1"/>
  <B><xsl:copy-of select="$param1"/></B>
</xsl:template>

<xsl:template match="/">
  <xsl:apply-templates select="test1"/>
</xsl:template>

<xsl:template match="test1">
  Output using variable and template to make bold
  ===============================================
  <xsl:variable name="var1">
    <!-- note that following section is the same as in the section below -->
    <xsl:call-template name="foo"/>  
    <xsl:value-of select="test2"/>
    <xsl:apply-templates select="test3"/>
    test test 
  </xsl:variable>
  <xsl:call-template name="fontstyle">
    <xsl:with-param name="param1" select="$var1"/>
  </xsl:call-template>
 
  Output without using variable and template
  ==========================================
  <B>
    <!-- note that following section is the same as in the section above -->
    <xsl:call-template name="foo"/>
    <xsl:value-of select="test2"/>
    <xsl:apply-templates select="test3"/>
    test test
  </B>
</xsl:template>

<xsl:template match="test3">
  <I> <xsl:value-of select="test4"/> </I>
</xsl:template>



XML:

<test1>
  test1 data
  <test2> test2 data </test2>
  <test3> <test4> test4 data </test4> </test3>
</test1>


RESULT:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  Output using variable and template to make bold
  ===============================================
 
<B>
  Template foo was called.
 test2 data  </B>
 
  Output without using variable and template
  ==========================================
 
<B>
  Template foo was called.
 test2 data
  <I> test4 data </I>
         test test
 
</B>


Any clarifications on how the rtf for a variable is generated (and restrictions on the use of that variable) would be very much appreciated, this has got me baffled.  Or, any suggestions on how to achieve what I want via other means (CSS is not an option, though) would be great too!

Thanks again,

Iain.



//----------------------------------------------------
// Iain Huxley
// President
// Mindspike Technologies
// http://mindspike.com
// Ph: (510) 339 7789
//----------------------------------------------------
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]