Solved

How to keep text formatting in XSL

Posted on 2002-03-28
13
258 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
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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