T
Thomas Wittek
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
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