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

Discussion in 'XML' started by Thomas Wittek, Aug 26, 2007.

  1. 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: -pobox.net
    GPG: 0xF534E231
    Thomas Wittek, Aug 26, 2007
    #1
    1. Advertising

  2. 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/
    Martin Honnen, Aug 26, 2007
    #2
    1. Advertising

  3. 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: -pobox.net
    GPG: 0xF534E231
    Thomas Wittek, Aug 26, 2007
    #3
  4. 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/
    Martin Honnen, Aug 26, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jordan Willms
    Replies:
    2
    Views:
    821
    Richard Tobin
    Nov 9, 2004
  2. Bostonasian
    Replies:
    1
    Views:
    809
    Joris Gillis
    Sep 18, 2005
  3. bprimuslevy
    Replies:
    1
    Views:
    427
    Soren Kuula
    Aug 23, 2006
  4. Stefan_W
    Replies:
    1
    Views:
    8,159
    Stefan_W
    Nov 5, 2008
  5. Replies:
    4
    Views:
    130
Loading...

Share This Page