Whitespace in <xsl:attribute> tags

J

John Gordon

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.
 
M

Martin Honnen

John said:
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?
 
R

Richard Tobin

John Gordon said:
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
 
J

John Gordon

In said:
Which XSLT processor are you using?

I'm using the libxslt package in python.
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.
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:eek:therwise>
someOtherValue
</xsl:eek:therwise>
</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:when> and <xsl:eek:therwise> tags, like so:

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

John Gordon

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.)
 
J

Joseph J. Kesselman

said:
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
> <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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top