944,120 Members | Top Members by Rank

Ad:
Mar 12th, 2005
0

Transforming XML with XSLT

Expand Post »
How do I preserve things like tabs and carriage returns in text? when transforming XML to XHTML with XSL. For example:

XML
HTML and CSS Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <?xml-stylesheet type="text/xsl" href="article.xsl"?>
  3. <content>
  4. <article name="Test Article">
  5. <heading>Test Article</heading>
  6. <subheading>Special Chars</subheading>
  7. <paragraph>
  8. <text>
  9. a less than &lt;
  10. a greater than &gt;
  11. </text>
  12. <text type="code">
  13. some code.
  14. </text>
  15. </paragraph>
  16. </article>
  17. </content>

XSL
HTML and CSS Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3.  
  4. <xsl:template match="content">
  5. <xsl:apply-templates select="article" />
  6. </xsl:template>
  7.  
  8. <xsl:template match="article">
  9. <h2><xsl:value-of select="heading" /></h2>
  10. <xsl:value-of select="subheading" />
  11. <xsl:for-each select="paragraph">
  12. <p>
  13. <xsl:for-each select="text">
  14. <xsl:choose>
  15. <xsl:when test="@type = 'link'">
  16. <a href="{@href}" target="{@target}">
  17. <xsl:value-of select="." />
  18. </a>
  19. </xsl:when>
  20. <xsl:when test="@type = 'code'">
  21. <div class="code">
  22.  
  23. <xsl:value-of select="." />
  24.  
  25. </div>
  26. </xsl:when>
  27. <xsl:otherwise>
  28.  
  29. <xsl:value-of select="." />
  30.  
  31. </xsl:otherwise>
  32. </xsl:choose>
  33. </xsl:for-each>
  34. </xsl:for-each>
  35. </xsl:template>
  36.  
  37. </xsl:stylesheet>

The carriage return between the text line:
a less than &lt;
And
a greater than &gt;

is lost when rendered in the browser, I sort of understand why that is, but I don't know how to apply XML or XSL to include/preserve or force the carriage return.
Similar Threads
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Mar 12th, 2005
0

Re: Transforming XML with XSLT

Is there a way to make substitutions? I know nothing of xslt, but you want to substitute a <br /> tag where ever the style sheet encounters the &gt;

If you are generating the xml yourself, you might try this:

HTML and CSS Syntax (Toggle Plain Text)
  1. <text>
  2. <table>
  3. <tr>
  4. <td>a less than</td>
  5. <td>a greater than</td>
  6. </tr>
  7. </table
  8. </text>

Hope this helps..
Reputation Points: 17
Solved Threads: 14
Posting Whiz
DanceInstructor is offline Offline
355 posts
since Feb 2005
Mar 12th, 2005
0

Re: Transforming XML with XSLT

Reputation Points: 54
Solved Threads: 20
Master Poster
DaveSW is offline Offline
765 posts
since Jul 2004
Mar 14th, 2005
0

Re: Transforming XML with XSLT

Thanks guys.

I followed Daves links, it tells me XSL preserves whitespace by default. What I havn't mentioned is that in my particular scenario i'm using client javascript transformNode function and I can't be certain if that is doing what it should.

Anyway I have plenty of options to try now. If I nail it, i'll post the horrid details.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Mar 14th, 2005
0

Re: Transforming XML with XSLT

You need to replace the line carriage characters with <br/> tags.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in HTML and CSS Forum Timeline: How to create a web page with scrolling menus to specific entries
Next Thread in HTML and CSS Forum Timeline: JS Ticker





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC