tags:

views:

1479

answers:

4

I had data in XML that had line feeds, spaces, and tabs that I wanted to preserve in the output HTML (so I couldn't use <p>) but I also wanted the lines to wrap when the side of the screen was reached (so I couldn't use <pre>).

A: 

I and a co-worker (Patricia Eromosele) came up with the following solution: (Is there a better solution?)

    <p>
<xsl:call-template name="prewrap">
<xsl:with-param name="text" select="text"/>
</xsl:call-template>
</p>



<xsl:template name="prewrap">
<xsl:param name="text" select="."/>
<xsl:variable name="spaceIndex" select="string-length(substring-before($text, ' '))"/>
<xsl:variable name="tabIndex" select="string-length(substring-before($text, '&#x09;'))"/>
<xsl:variable name="lineFeedIndex" select="string-length(substring-before($text, '&#xA;'))"/>
<xsl:choose>
<xsl:when test="$spaceIndex = 0 and $tabIndex = 0 and $lineFeedIndex = 0"><!-- no special characters left -->
<xsl:value-of select="$text"/>
</xsl:when>
<xsl:when test="$spaceIndex > $tabIndex and $lineFeedIndex > $tabIndex"><!-- tab -->
<xsl:value-of select="substring-before($text, '&#x09;')"/>
<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
<xsl:call-template name="prewrap">
<xsl:with-param name="text" select="substring-after($text,'&#x09;')"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="$spaceIndex > $lineFeedIndex and $tabIndex > $lineFeedIndex"><!-- line feed -->
<xsl:value-of select="substring-before($text, '&#xA;')"/>
<br/>
<xsl:call-template name="prewrap">
<xsl:with-param name="text" select="substring-after($text,'&#xA;')"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="$lineFeedIndex > $spaceIndex and $tabIndex > $spaceIndex"><!-- two spaces -->
<xsl:value-of select="substring-before($text, ' ')"/>
<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
<xsl:call-template name="prewrap">
<xsl:with-param name="text" select="substring-after($text,' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise><!-- should never happen -->
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Source: http://jamesjava.blogspot.com/2008/06/xsl-preserving-line-feeds-tabs-and.html

James A. N. Stauffer
A: 

Really, I'd choose an editor which supports this correctly, rather than wrangling it through more XML.

Richard Franks
I was using XSLT to convert XML to HTML so an editor isn't being used.
James A. N. Stauffer
A: 

Not sure if this relevant, but isn't there a preservespace attribute and whatnot for xml?

RWendi
+1  A: 

Another way of putting this is that you want to turn all pairs of spaces into two non-breaking spaces, tabs into four non-breaking spaces and all line breaks into <br> elements. In XSLT 1.0, I'd do:

<xsl:template name="replace-spaces">
  <xsl:param name="text" />
  <xsl:choose>
    <xsl:when test="contains($text, '  ')">
      <xsl:call-template name="replace-spaces">
        <xsl:with-param name="text" select="substring-before($text, '  ')"/>
      </xsl:call-template>
      <xsl:text>&#xA0;&#xA0;</xsl:text>
      <xsl:call-template name="replace-spaces">
        <xsl:with-param name="text" select="substring-before($text, '  ')" />
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="contains($text, '&#x9;')">
      <xsl:call-template name="replace-spaces">
        <xsl:with-param name="text" select="substring-before($text, '&#x9;')"/>
      </xsl:call-template>
      <xsl:text>&#xA0;&#xA0;&#xA0;&#xA0;</xsl:text>
      <xsl:call-template name="replace-spaces">
        <xsl:with-param name="text" select="substring-before($text, '&#x9;')" />
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="contains($text, '&#xA;')">
      <xsl:call-template name="replace-spaces">
        <xsl:with-param name="text" select="substring-before($text, '&#xA;')" />
      </xsl:call-template>
      <br />
      <xsl:call-template name="replace-spaces">
        <xsl:with-param name="text" select="substring-after($text, '&#xA;')" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$text" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Not being able to use tail recursion is a bit of a pain, but it shouldn't be a real problem unless the text is very long.

An XSLT 2.0 solution would use <xsl:analyze-string>.

JeniT
Correction: You have two substring-before in your code that should be substring-after.
MrFox