RE: [xsl] empty attribute tags rendering in HTML

Subject: RE: [xsl] empty attribute tags rendering in HTML
From: "Bonnie Vasko" <bvasko@xxxxxxxxxxxxxxxx>
Date: Mon, 27 Feb 2006 13:49:17 -0600
This worked, thanks.

-----Original Message-----
From: Spencer Tickner [mailto:spencertickner@xxxxxxxxx]
Sent: Monday, February 27, 2006 1:42 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] empty attribute tags rendering in HTML

Hi Bonnie,

Try this out:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

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

<xsl:template match="a|td|img">
	<xsl:element name="{name()}">
	<xsl:for-each select="@*">
		<xsl:if test="not(normalize-space(.) = '')">
			<xsl:attribute name="{name()}"><xsl:value-of
select="."/></xsl:attribute>
		</xsl:if>
	</xsl:for-each>
	<xsl:apply-templates/>
	</xsl:element>
</xsl:template>

</xsl:stylesheet>

On 2/27/06, Bonnie Vasko <bvasko@xxxxxxxxxxxxxxxx> wrote:
> Hello everyone,
>
> I'm relatively new to XSL. I'm troubleshooting the templates created
by
> my former co-worker. They're outputting all the attributes for every
> instance of every tag, even though they're empty, as in this example:
>
> <td rowspan="2" colspan="" border="0" class=""><a id="" class=""
> target="" title="" href="gateway.jsp" name=""><img id="" border="0"
> alt="GuardMyHeart.com" class=""
> src="images/headerLogo.gif"></a></td>
>
>
> Here is a sample of the xsl:
>
> <xsl:template match="a">
> <a name="{@name}" href="{@href}" title="{@title}" target="{@target}"
> class="{@class}" id="{@id}">
> <xsl:value-of select="." /><xsl:apply-templates select="img" /></a>
> </xsl:template>
>
> <xsl:template match="td">
> <td valign="{@valign}" align="{@align}" width="{@width}"></td>
> </xsl:template>
>
> <xsl:template match="img">
> <img src="{@src}" class="{@class}" alt="{@alt}" border="0"
> id="{@id}"></img>
> </xsl:template>
>
>
> I'm assuming there should be some kind of test that determines if the
> attribute is empty, and if it is, don't write it. I haven't been able
to
> find an example of this. This is xml/xsl version 1. Thank you in
> advance. Also, if you can point me to any resources that explain this,
I
> would really appreciate it.
>
> Thanks,
>
> Bonnie Vasko
> Senior Developer
> WHITTMANHART
> main: 215.636.9500
> fax: 215.636.9526
> bvasko@xxxxxxxxxxxxxxxx
> www.whittmanhart.com

Current Thread