This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: outputting spaces in html table cells


"Sellmer-Brüls, Barbara" wrote:
> I solved it with the following toplevel XSL elements:
> <xsl:preserve-space elements="*"/>

I'm not sure how this really solves your problem. All this does is makes
it so that none of the whitespace-only text nodes in your source tree are
stripped before processing. This lets you have the string-value of element
'blah' be a whitespace-only string, if it is specified as such in the XML,
instead of an empty string. But if all you are doing is this:

<td class="listing"> <xsl:value-of select="blah" /> </td>

then you have said "create an element 'td' with attribute
'class'='listing', and create a text node child with ' ' followed by a
text node with the string-value of element 'blah' followed by a text
node with another ' ':

element 'td'
   |  \___attribute 'class'='listing'
   |___text ' '
   |___text '' (string(blah))
   |___text ' '

Following the rules of the XPath/XSLT data model, the 3 text nodes will be
combined into 1, which will be '  ' at the very least, and you know it
will be nothing but whitespace if blah was empty or had a whitespace-only
string-value:

element 'td'
   |  \___attribute 'class'='listing'
   |___text '  '

When serialized as HTML as per the xsl:output method specified, you get

<td class="listing">  </td>

Following the rules of HTML, adjacent spaces are collapsed and considered
to be 1 single 'word separator', and in this case there are no words to
separate, so it will be collapsed entirely, producing the same result as

<td class="listing"></td>

...which of course is technically not allowed, and I think is why you want
to use &nbsp;. &nbsp; is by definition &#160;, so as long as you put
&#160; into the result tree, you will get either &#160; or &nbsp; in the
HTML. Here is my suggestion:

<td class="listing">
  <xsl:choose>
    <xsl:when test="normalize-space(blah)">
      <xsl:value-of select="blah"/>
    </xsl:when>
    <xsl:otherwise>&#160;</xsl:otherwise>
  </xsl:choose>
</td>

   - Mike
____________________________________________________________________
Mike J. Brown, software engineer at         My XML/XSL resources:
webb.net in Denver, Colorado, USA           http://www.skew.org/xml/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]