Solved

How to keep text formatting in XSL

Posted on 2002-03-28
13
256 Views
Last Modified: 2008-02-26
I have a VB application where the user can enter some text. This text is then put in an XML to be used by an XSL. Is there any to keep as much text formatting as possible(paragraph, bold, italic, etc...)?

Also, if I format some text using various tags like list-blocks and blocks, and then I passed this text to a template with param, the XSL keeps only the text and forget the blocks and the rest! Very annoying!

Thx
0
Comment
Question by:BigGayHAL
  • 9
  • 4
13 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 6904209
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6904212
XML Document
============
<?xml version="1.0" encoding="iso-8859-1"?>
<document>
     <prologue><![CDATA[
     <h1>Welcome To Our WebSite</h1>
     <p>This a the old recommended paragraphing technique
     <p>Second <b>Para</b>
     ]]>
     </prologue>
     <block>
     This is my first line
     
     Second line after a break
     </block>
     <data>
          The following is a link of recommended vendors:
          <ul>
               <li><a href="http://www.microsoft.com">Microsoft</a></li>
               <li><a href="http://www.ibm.com">IBM</a></li>
          </ul>    
     </data>
     <transform>
          <p class="user">
          This is a new era of <b><i>Information <u>Overload</u></i></b> as well as a new way of doing things.
          </p>
     </transform>
</document>
0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 200 total points
ID: 6904213
XSLT Document
=============
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>Data</title>
</head>
<body>
<!--unescaping data within a CDATA Section-->
<xsl:value-of select="//prologue" disable-output-escaping="yes" />
<!--maintaining lines-->
<xsl:apply-templates select="//block" />
<!--copying entire html syntax-->
<xsl:copy-of select="//data/node()" />
<!--element transformation technique-->
<xsl:apply-templates select="//transform/node()" mode="html" />
</body>
</html>
</xsl:template>
<!--templates that provide persistence of lines-->
<xsl:template match="block">
<div>
     <xsl:call-template name="lines">
     <xsl:with-param name="raw" select="string(.)" />
     </xsl:call-template>
</div>
</xsl:template>
<xsl:template name="lines">
<xsl:param name="raw" />
<xsl:if test="$raw">
     <xsl:choose>
     <xsl:when test="contains($raw,'&#13;')">
          <xsl:value-of select="substring-before($raw,'&#13;')" /><br />
          <xsl:call-template name="lines">
          <xsl:with-param name="raw" select="substring-after($raw,'&#13;')" />
          </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
          <xsl:value-of select="$raw" />
     </xsl:otherwise>
     </xsl:choose>
</xsl:if>
</xsl:template>
<!--end of templates that provide persistence of lines-->
<!--templates that use the element transformation technique-->
<xsl:template match="@* | node()" mode="html">
<xsl:copy><xsl:apply-templates select="@* | node()" mode="html"/></xsl:copy>
</xsl:template>
<!--this switches the p element to a div element-->
<xsl:template match="p" mode="html">
<div><xsl:apply-templates select="@* | node()" mode="html" /></div>
</xsl:template>
<!--this switches the class of the p element accordingly-->
<xsl:template match="p/@class" mode="html">
<xsl:attribute name="style">font-size:10pt;font-family:Verdana;</xsl:attribute>
</xsl:template>
<!--this removes the bold-italic pair-->
<!--advanced XSLT technique-->
<xsl:template match="b[name(node()[1]) = 'i'] | i[name(..) = 'b']" mode="html">
<xsl:choose>
<xsl:when test="name() = 'b'"><xsl:apply-templates select="node()" mode="html"/></xsl:when>
<xsl:otherwise><span style="font-style:italic;font-weight:bold;"><xsl:apply-templates select="node()" mode="html"/></span></xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--end of templates that use the element transformation technique-->
</xsl:stylesheet>
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 23

Expert Comment

by:b1xml2
ID: 6904215
HTML Output
===========
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Data</title>
</head>
<body>
     <h1>Welcome To Our WebSite</h1>
     <p>This a the old recommended paragraphing technique
     <p>Second <b>Para</b>
      <div><br>     This is my first line<br>     <br>     Second line after a break<br>     </div>
          The following is a link of recommended vendors:
          <ul>
<li><a href="http://www.microsoft.com">Microsoft</a></li>
<li><a href="http://www.ibm.com">IBM</a></li>
</ul>
<div style="font-size:10pt;font-family:Verdana;">
          This is a new era of <span style="font-style:italic;font-weight:bold;">Information <u>Overload</u></span> as well as a new way of doing things.
          </div>
</body>
</html>
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6904220
Notes
=====
1. The XSLT Document show you a number of useful advance tricks <GRIN>

2. To persist lines, see the break templates in the XSLT Document.

3. To copy wholesale XHTML code
<!--copying entire html syntax-->
<xsl:copy-of select="//data/node()" />

4. To effect targetted element transformation, see the templates with the mode="html"

VB Notes
========
If you are transforming the data and putting it back into a VB Control, you will find that using a textbox control will not work even if multi-line is set to true. You will have to use the richtextbox control.

Regards,

Brandon Driesen

0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6915354
Okay, BigGayHal, Feedback pls and I hope you're not as bad as MrCrawFish =)
0
 

Author Comment

by:BigGayHAL
ID: 6918178
Sorry, I haven't had any time to check this yet, my boss asked me to do some urgent stuff for a client first. I'll try it ASAP

Thx!
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6918199
thanx for the feedback =)
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6942117
BigGayHal, it's been a while. please provide more feedback. lack of responsiveness will discourage experts from providing top class solutions to you now and in the future.
0
 

Author Comment

by:BigGayHAL
ID: 6948324
Hi! Yes I'm sorry but we kinda have an emergency so
this part of the project will be done only in a couple
of days. Don't worry! I haven't forgotten you!

Thx for your patience
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6948334
thanks yet again =)
0
 

Author Comment

by:BigGayHAL
ID: 6972185
humm.. I tried the  templates that provide persistence of lines but it gives me a compile error!
0
 

Author Comment

by:BigGayHAL
ID: 6972431
Hi! it seems the compiler didnt like <BR/>
so I replaced it with <fo:block>&#160;</fo:block>
it seems to work fine.
I now use a rich text box and I replace all vbCRLF by &#13;
and vice-versa.

thx for you patience
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question