Removing white-space in xsl:attribute elements Options
jMosbech
Posted: Thursday, June 05, 2008 9:29:12 AM
Rank: Newbie

Joined: 6/5/2008
Posts: 4
Location: Copenhagen
Hi there,

Inspired by the inputs given by Casey at the Umbraco Gems CG session yesterday I did some testing of the white-space problem I had, and I just wanted to share my findings (although they may not be groundbreaking).

The problem is that if you try to build the content of an xsl:attribute element in this way:

Code:
    <xsl:attribute name="alt">
        id:
        <xsl:value-of select="$currentPage/node/@id"/>        
    </xsl:attribute>


The indentation is shown in the output like this:

Code:
alt="&#xD;&#xA;&#x9;&#x9;id: &#xD;&#xA;&#x9;&#x9;1073"


While this is not critical for alt-attributes is is really annoying if you are trying to build a source-attribute in the same way. This can be fixed in (at least) two ways:

1) Remove all indentation from your xsl:attribute element. While this solves the problem it is also reduces the readability of the code. This was how I used to do it.
2) Wrap all hard-coded text in <xsl:text> tags. The following works like a charm and your precious indentation is preserved.
Code:
    <xsl:attribute name="alt">
        <xsl:text>id: </xsl:text>
        <xsl:value-of select="$currentPage/node/@id"/>        
    </xsl:attribute>


I also tried to solve the problem using the <xsl:strip-space>/<xsl:preserve-space> tags by adding the following as children of the <xsl:stylesheet> element:

Code:
<xsl:preserve-space elements="*"/>
<xsl:strip-space elements="xsl:attribute" />


But not matter how I did it I ended up getting an xslt parsing error. Does any of you have some inputs on using these elements?

Anyways - thanks for a couple of interesting days at Codegarden.

Best Regards
Jonas
jMosbech
Posted: Thursday, June 05, 2008 10:16:15 AM
Rank: Newbie

Joined: 6/5/2008
Posts: 4
Location: Copenhagen
A small addition to my post above: You can of course also avoid the problem by using the shorthand syntax:

Code:
<img alt="id: {$currentPage/node/@id}" .../>


But this will only be suitable if you don't need complex stuff like <xsl:choose> inside your attribute.
Users browsing this topic
Guest


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.