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
Solved

How to keep text formatting in XSL

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

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

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…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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