By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 956 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

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

P: n/a
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:ifin 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: st*********@jabber.i-pobox.net
GPG: 0xF534E231
Aug 26 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.