Solved

How to keep text formatting in XSL

Posted on 2002-03-28
13
254 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
XSD Data to SQL Tables 2 21
SQL Remove Elements from XML 6 40
Merge 2 XML Files together 5 40
Word files to Wordpress Posts 2 26
The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now