Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Whitespace in <xsl:attribute> tags

Question posted by: John Gordon (Guest) on July 2nd, 2008 04:35 PM
My XSLT files have many occurrences of this general pattern:

<a>
<xsl:attribute name="href">
<xsl:value-of select="xyz" />
</xsl:attribute>
</a>

When I execute an XSL transform, the resulting HTML looks like this:

<a href="xyz%0A%09%09">

.... because *everything* between the opening and closing attribute tags
is being included, even the carriage return after the opening attribute
tag and the tabs before the value-of tag.

Is there a way to avoid this behavior? I tried adding
<xsl:strip-space elements="*"/at the top of the file, but it appeared to
have no effect.

--
John Gordon A is for Amy, who fell down the stairs
Join Bytes! B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
July 2nd, 2008
05:05 PM
#2

Re: Whitespace in <xsl:attribute> tags
John Gordon wrote:
Quote:
Originally Posted by
My XSLT files have many occurrences of this general pattern:
>
<a>
<xsl:attribute name="href">
<xsl:value-of select="xyz" />
</xsl:attribute>
</a>
>
When I execute an XSL transform, the resulting HTML looks like this:
>
<a href="xyz%0A%09%09">
>
... because *everything* between the opening and closing attribute tags
is being included, even the carriage return after the opening attribute
tag and the tabs before the value-of tag.
>
Is there a way to avoid this behavior? I tried adding
<xsl:strip-space elements="*"/at the top of the file, but it appeared to
have no effect.


Which XSLT processor are you using?
For the stylesheet itself (see http://www.w3.org/TR/xslt#strip) "the set
of whitespace-preserving element names consists of just xsl:text" so the
white space between those xsl element should not result in white space
in the result tree.

Obviously the snippet you posted can be reduced to
<a href="{xyz}">
</a>
using an literal result element and an attribute value template but what
you have should not result in whitespace problems.

Are you sure the whitespace and the escaping does not result from
evaluating xyz and applying HTML href escaping?




--

Martin Honnen
http://JavaScript.FAQTs.com/

Richard Tobin's Avatar
Richard Tobin
Guest
n/a Posts
July 2nd, 2008
08:05 PM
#3

Re: Whitespace in <xsl:attribute> tags
In article <g4gae4$66m$1@reader1.panix.com>,
John Gordon <gordon@panix.comwrote:
Quote:
Originally Posted by
>My XSLT files have many occurrences of this general pattern:
>
><a>
> <xsl:attribute name="href">
> <xsl:value-of select="xyz" />
> </xsl:attribute>
></a>
>
>When I execute an XSL transform, the resulting HTML looks like this:
>
><a href="xyz%0A%09%09">


Do you have xml:space="preserve" on some ancestor of the <xsl:attribute>
element?

-- Richard
--
Please remember to mention me / in tapes you leave behind.

John Gordon's Avatar
John Gordon
Guest
n/a Posts
July 2nd, 2008
09:15 PM
#4

Re: Whitespace in <xsl:attribute> tags
In <486bb4f6$0$6607$9b4e6d93@newsspool2.arcor-online.netMartin Honnen <mahotrash@yahoo.dewrites:
Quote:
Originally Posted by
Which XSLT processor are you using?


I'm using the libxslt package in python.
Quote:
Originally Posted by
Obviously the snippet you posted can be reduced to
<a href="{xyz}">
</a>


Yes, I've changed to this shorthand method and it has helped.
Quote:
Originally Posted by
Are you sure the whitespace and the escaping does not result from
evaluating xyz and applying HTML href escaping?


I thought it might be an href escaping issue, but then I noticed the
same thing happening when I was setting a variable and then using the
value in a hidden form input element, like so:

<xsl:variable name="actionType">
<xsl:choose>
<xsl:when test="something">
someValue
</xsl:when>
<xsl:otherwise>
someOtherValue
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

....

<form>
<input type="hidden" name="actionType">
<xsl:attribute name="value">
<xsl:value-of select="$actionType" />
</xsl:attribute>
</input>
</form>

The value of the form element contained the embedded newline and tabs.
When I changed the hidden form element to use the shorthand {} notation
the problem still persisted; it was only fixed by editing the variable
declaration and removing all the whitespace between the beginning and
ending <xsl:whenand <xsl:otherwisetags, like so:

<xsl:variable name="actionType">
<xsl:choose>
<xsl:when test="something">someValue</xsl:when>
<xsl:otherwise>someOtherValue</xsl:otherwise>
</xsl:choose>
</xsl:variable>

--
John Gordon A is for Amy, who fell down the stairs
Join Bytes! B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


John Gordon's Avatar
John Gordon
Guest
n/a Posts
July 2nd, 2008
09:15 PM
#5

Re: Whitespace in <xsl:attribute> tags
In <g4gn68$17tt$1@pc-news.cogsci.ed.ac.ukJoin Bytes! (Richard Tobin) writes:
Quote:
Originally Posted by
Quote:
Originally Posted by
My XSLT files have many occurrences of this general pattern:

<a>
<xsl:attribute name="href">
<xsl:value-of select="xyz" />
</xsl:attribute>
</a>

When I execute an XSL transform, the resulting HTML looks like this:

<a href="xyz%0A%09%09">

Quote:
Originally Posted by
Do you have xml:space="preserve" on some ancestor of the <xsl:attribute>
element?


No, not that I know of. (I inherited this project from someone else; it's
possible that such an element is present and I'm just not aware of it.)

--
John Gordon A is for Amy, who fell down the stairs
Join Bytes! B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"


Joseph J. Kesselman's Avatar
Joseph J. Kesselman
Guest
n/a Posts
July 2nd, 2008
09:45 PM
#6

Re: Whitespace in <xsl:attribute> tags
<xsl:when test="something">
Quote:
Originally Posted by
someValue
</xsl:when>


If someValue is literal text, the whitespace is assumed to be
meaningful. See XML's rules regarding "whitespace in element content";
whitespace adjacent to non-whitespace is assumed to be part of the same
text as that non-whitespace and should be retained. Your original
question was about
Quote:
Originally Posted by
<input type="hidden" name="actionType">
<xsl:attribute name="value">
<xsl:value-of select="$actionType" />
</xsl:attribute>
</input>


Here the whitespace is adjacent only to an element, and hence XSLT
should conclude that it's just stylesheet indentation and can be
discarded, unless you have instructed it otherwise.

If your XSLT processor isn't giving you the expected behavior, it may be
time to switch processors. Or to send a bug report to its authors and
see if they can tell you what you're doing that they didn't anticipate.

 
Not the answer you were looking for? Post your question . . .
182,494 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors