This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Fw:
- To: xsl-list at mulberrytech dot com
- Subject: Re: Fw:
- From: Steve Tinney <stinney at sas dot upenn dot edu>
- Date: Tue, 01 Feb 2000 21:31:51 -0500
- References: <020601bf6cf3$f419af80$0701000a@ebop.com>
- Reply-To: xsl-list at mulberrytech dot com
> mohamed wrote:
>
> I'm trying to call templates based on attribute names:
>
> <?xml version="1.0"?>
> <testText >
> <text bold="yes">WITH BOLD</text>
> <text bold="yes" italic="yes"> WITH BOLD AND ITALIC</text>
> </testText>
> so I declared templates that are called and match bold and italics,
>
> <xsl:template match="bold" name="bold">
> <b>
> <xsl:apply-templates/>
> </b>
> </xsl:template>
>
> <xsl:template match="italics" name="italics">
> <i>
> <xsl:apply-templates/>
> </i>
> </xsl:template>
>
> and I want to get an output as follows:
> <b>WITH BOLD</b>
> <b><i>WITH BOLD AND ITALIC</i></b>
Here is a version which marries recursion and an in-sheet map of
attribute names to element names to produce something which is a bit
funky to read, but very easy to maintain if you have a 1:1 correlation
of attribute names to elements---you just add entries to the map.
Steve
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template name="attr-map">
<map attr="bold" elem="b"/>
<map attr="italic" elem="i"/>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="testText/text">
<xsl:call-template name="format-by-attribute"/>
</xsl:template>
<xsl:template name="format-by-attribute">
<xsl:param name="attr-set" select="@*[.='yes']"/>
<xsl:variable name="nattr" select="count($attr-set)"/>
<xsl:choose>
<xsl:when test="$nattr = 0">
<xsl:apply-templates/>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{document('')/*/xsl:template[@name='attr-map']
/map[@attr=name($attr-set[1])]/@elem}">
<xsl:choose>
<xsl:when test="$nattr = 1">
<xsl:apply-templates/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="format-by-attribute">
<xsl:with-param name="attr-set"
select="$attr-set[position()>1]"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list