Solved

How to keep text formatting in XSL

Posted on 2002-03-28
13
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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, …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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