Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT: How to avoid empty attributes in attribute-sets?

Reply
Thread Tools

XSLT: How to avoid empty attributes in attribute-sets?

 
 
Thomas Wittek
Guest
Posts: n/a
 
      08-26-2007
Hi!

I'm using xsl:attribute-sets to reduce redundancy in my XSLT.
An example from a transformation to XHTML (the attribute values are
simply copied from input to output):

<xsl:attribute-set name="cellhalign">
<xsl:attribute name="align">
<xsl:value-of select="@align" />
</xsl:attribute>
<xsl:attribute name="char">
<xsl:value-of select="@char" />
</xsl:attribute>
<xsl:attribute name="charoff">
<xsl:value-of select="@charoff" />
</xsl:attribute>
</xsl:attribute-set>

Works fine, except that it generates empty attributes, if the attribute
is not defined in the source XML:

<td align="" char="" charoff="">31</td>

That's quite ugly. Especially when you have a lot of <td>'s...

Now you cannot use <xsl:if> in an <xsl:attribute-set>.
So if you like to create a "conditional attribute" you'd have to do it
within the template:

<xsl:template match="nitf:td">
<td>
<xsl:if test="@align">
<xsl:attribute name="align">
<xsl:value-of select="@align" />
</xsl:attribute>
</xsl:if>
<!-- repeat that for the other attrs -->
<xsl:apply-templates/>
</td>
</xsl:template>

But doing that will be extremely redundant if the same attributes apply
to several templates.

It there any solution to avoid redundant attribute declarations *and*
avoid empty attributes in the output XML?

Thank you very much!
Regards
--
Thomas Wittek
Web: http://gedankenkonstrukt.de/
Jabber:
GPG: 0xF534E231
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      08-26-2007
Thomas Wittek wrote:

> It there any solution to avoid redundant attribute declarations *and*
> avoid empty attributes in the output XML?


As long as you simply want to copy attributes from the input to the
output you can simply do e.g.
<xsl:copy-of select="@*"/>
or for particular attributes
<xsl:copy-of select="@align | @char"/>
Doing the usual identity transformation stuff e.g.
<xsl:apply-templates select="@align | @char"/>
and
<xsl:template match="@*">
<xsl:copy/>
</xsl:template>
should also do.

Or do I misunderstand what you want to achieve?

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Thomas Wittek
Guest
Posts: n/a
 
      08-26-2007
Martin Honnen schrieb:
> Thomas Wittek wrote:
>
>> It there any solution to avoid redundant attribute declarations *and*
>> avoid empty attributes in the output XML?

>
> As long as you simply want to copy attributes from the input to the
> output you can simply do e.g.
> <xsl:copy-of select="@*"/>
> or for particular attributes
> <xsl:copy-of select="@align | @char"/>
> Doing the usual identity transformation stuff e.g.
> <xsl:apply-templates select="@align | @char"/>
> and
> <xsl:template match="@*">
> <xsl:copy/>
> </xsl:template>
> should also do.
>
> Or do I misunderstand what you want to achieve?


Thank you very much!
That perfectly solves *my* problems!

But it wouldn't work, if I'd had to rename the attributes, would it?
Fortunately, I don't have to rename them, so it works nonetheless for me.

--
Thomas Wittek
Web: http://gedankenkonstrukt.de/
Jabber:
GPG: 0xF534E231
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      08-26-2007
Thomas Wittek wrote:

> But it wouldn't work, if I'd had to rename the attributes, would it?


If you use the identity transformation approach then you can also insert
templates in the transformation process that rename attributes e.g. do
<xsl:apply-templates select="@some-attribute"/>
and
<xsl:template match="@some-attribute">
<xsl:attribute name="new-attribute">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Avoid having a SQL express for web parts and avoid personalization Roger23 ASP .Net 2 10-12-2006 10:54 PM
How Do We Avoid the Extra Empty Line at the End of the Output File? mary C++ 6 01-19-2005 03:07 PM
Avoid wasting time or how to avoid initialization Alexander Malkis C++ 8 04-13-2004 11:23 PM
Datagrid datetime: empty datatime column shows "1/1/0001", how to avoid this? =?Utf-8?B?UmV6YQ==?= ASP .Net 3 03-02-2004 06:27 PM



Advertisments