Transforming XML with XSLT

Reply

Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Transforming XML with XSLT

 
0
  #1
Mar 12th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 355
Reputation: DanceInstructor is an unknown quantity at this point 
Solved Threads: 14
DanceInstructor's Avatar
DanceInstructor DanceInstructor is offline Offline
Posting Whiz

Re: Transforming XML with XSLT

 
0
  #2
Mar 12th, 2005
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..
Clear Mind Hosting and Web Design

If I've helped you please consider adding to my reputation.
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 764
Reputation: DaveSW is on a distinguished road 
Solved Threads: 17
DaveSW's Avatar
DaveSW DaveSW is offline Offline
Master Poster
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Transforming XML with XSLT

 
0
  #4
Mar 14th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: Transforming XML with XSLT

 
0
  #5
Mar 14th, 2005
You need to replace the line carriage characters with <br/> tags.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the HTML and CSS Forum


Views: 6830 | Replies: 4
Thread Tools Search this Thread



Tag cloud for HTML and CSS
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC