Re: [xsl] call-template and execute xsl command

Subject: Re: [xsl] call-template and execute xsl command
From: Eric Smith <Eric.Smith@xxxxxxxxxxxx>
Date: Mon, 15 Apr 2002 23:44:11 +0200
According to Michael Kay on Mon, Apr 15, 2002 at 08:04:43PM +0100:
> You can't add attributes to an element after adding a text node. This is so
> that XSLT processors are able to operate serially.
> 
> What you are trying to do is effectively:
> 
> <xsl:element name="e">
>   text
>   <xsl:attribute name="a">5</xsl:attribute>
> </xsl:element>
> 
> A processor trying to output
> <e a="5">text</e>
> 
> would have to delay outputting the "text" until it was sure there were no
> attributes to be added.
> 

Thanks Michael, but please bear with me and take-5.  The text
node is a complete red herring - I don't need or want it.

Use case is:
I want to call a single function from various templates that will
allow me to add various attributes to the source document and
have these out put in xsl:fo.  Things like font-size and
text-alignment etc.  I would think this quite a common requirement.

The solution I suggested (but may be entirely inappropriate)
partly works in that it will output a text node but not an xsl
instruction i.e. one that adds an attribute to the most recently
defined node.  So taking away the red herring:



<xsl:template name= "attributes">
    <xsl:choose>
     <xsl:when test="@font-size">
      <xsl:attribute name="font-size"><xsl:value-of select="@font-size"/></xsl:attribute>
     </xsl:when>
     <xsl:when test="@text-align">
      <xsl:attribute name="text-align"><xsl:value-of select="@text-align"/></xsl:attribute>
     </xsl:when>
    </xsl:choose>
</xsl:template>

called like this:
 <xsl:variable name='y'>
   <xsl:call-template name='attributes'/>
 </xsl:variable>
<!-- highly dubious statement follows
which should add an attribute if it finds
an attribute in the source matching one of
the conditions in the attributes template.  -->

<xsl:value-of select="$y"/>

<!-- I cannot immediately figure how xsl:element helps here. -->

Thanx
-- 
Eric Smith 

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


Current Thread