Question

XSL Transform CDATA into XHTML with Block Mark Up <p>

Asked by: Sivakatirswami

I searched for "line break" in the XML topic area, and found a number of threads on this subject of passing vertical white space out from XML to XHTML tags... but they are quite old (some 5 years old) and I've got points to spend and wanted to open this discussion anew, as I'm amazed this is so difficult and hoping there has got to be a simple solution as of June, 2005....
========

Given XML that looks like this:

<audio_transcript>
   <header>
         <subject>Some Title</subject>
         <date_given>2005-01-01</given>
                 ## more header child nodes with other info...##
     </header>
<transcript_text>
<![CDATA[    

# note: source may have extra blank lines
#  *or* may have single CR's only -- char(10)
     
Conditions in the world today are certainly troubling,

New 2nd paragraph...  The shocking attacks in New York on  
September 11, 2001,

3rd paragraph... One of the immediate consequences of 9/11 was  
the television coverage etc.

]]>
     </transcript_text>
</audio_transcript>

How do we get an XSLT  transformation to tag these paragraphs with standard block mark up, where the XSLT has:

<div id="mainContentSection">
  <xsl:value-of select="audio_transcript/transcript_text" />
</div>

And the final XHTML output for web for that div wants to be a simple standard markup of:

 <p class="transcript_text">Conditions in the world today are certainly troubling, </p>

<p class="transcript_text">New 2nd paragraph...  The shocking attacks in New York on  
September 11, 2001, </p>

 <p class="transcript_text">3rd paragraph... One of the immediate consequences of 9/11 was  the television coverage etc.</p>

If we *can preserve the vertical white space in addition the <p> tags, so much the better, as it is so much easier to read and assess XHTML that "breathes" openly.

Sivakatirswami

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-06-10 at 11:53:21ID21454279
Tags

cdata

,

transform

Topics

Extensible Markup Language (XML)

,

Macromedia Homesite

Participating Experts
2
Points
500
Comments
13

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Transforming XSL to XSL
    My ultimate goal is to pull a string from the URL and use it in <xsl:apply-templates select="use here" /> in order to limit the displayed results. I have messed around with variables and params and gotten nowhere. It seems like the thing for me to do is to t...
  2. XSL transformation
    Hi, I'm trying to run this xsl it gives me the follwoing error: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft...
  3. XSL Transformation
    Hi, I have an Input XML File - "Input XML File.xml". This file contains a) Product/Product Group Hierarchy, and b) Individual Products within each Product Group. Each Product Group XML tag defines the list of products within that group. (E.g. "Nokia Handset...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: metalmickeyPosted on 2005-06-10 at 13:28:37ID: 14192351

I think that your CDATA section is what is causing the problem, since cdata sections are a specific instruction the processor NOT to process anything between the open and close <![CDATA[  tags ]]>.

you could use this facet of cdata to your advantage by passing in html markup...

<transcript_text>
<![CDATA[    
<p class="transcript_text">Conditions in the world today are certainly troubling,</p>

<p class="transcript_text">New 2nd paragraph...  The shocking attacks in New York on  September 11, 2001,</p>

<p class="transcript_text">3rd paragraph... One of the immediate consequences of 9/11 was  the television coverage etc.</p>

]]>
    </transcript_text>

since your parser isnt reading this part of the page, your tags wont be escaped once parsed.

&gt;p class="transcript_text"&lt;Conditions in the world today are certainly troubling,&gt;/p&lt;


Your browser is then free to read the html markup within.


This does assume that you have some control over the xml content...but having said that you'd have probably marked the xml up accordingly.

This may not have answered your Q but may have opened up an avenue to explore.

MM



 

by: SivakatirswamiPosted on 2005-06-10 at 14:04:53ID: 14192646

MM, thanks for tackling this old tedious topic.

Yes, we do have control over the XML content as it is generated from within applications with a UI we have built ourselves with Revolution. And I can do anything with the data as it is written out to XML... i.e. we  can easily "poke" the CDATA section by doing an SnR to replace  "cr"  --  char(10)  --  with "</p>&cr&<p>" in my code...

But, while a simple solution, it's not working, I did a manual entry to the CDATA section and, at least in Fire Fox, we still seeing those strings passed as literals to the screen:

as you can see at:

http://himalayanacademy.com/resources/bodhinatha/editorials/test-2.xml

But, yes, let's not be stuck in a box....... I like "other avenues" ... [disclaimer: I'm and XML XSLT infant who doesn't know anything more than what he learned at w3Schools.org, so feel free to tell me anything you want. ]

So how about this... for simple textual data (articles in magazines, transcripts of audio discourses, lectures etc.) Do we really need to use CDATA? I just did that to be "safe" but if the only chars that are a problem are > and & then may I should

a) not use CDATA  but just let the content be standard PCDATA as in

<transcript_text>

Conditions in the world today are certainly troubling
New 2nd paragraph...  The shocking attacks in New York on  September 11, 2001,
3rd paragraph... One of the immediate consequences of 9/11 was  the television coverage etc.

</transcript_text>

b) have our input app UI simply escape illegal chars  ">" and "&" (I keep telling data entry people don't use Ampersands, but invariably some one will enter "apples & oranges")

c) then we tackle the mark up issue with the XSL some other way... ? --0 especially since your proposed solution didn't work

This has advantages because the data in the main content section (s) which has paragraph breaks, may be repurposed into a other environments and I would rather not have to enter html mark up into the XML unless absolutely necessary.  Which is after all "forbotten!" in the XML --> XHMTL paradigm anyway ... not that I'm stuck on such so called principles, but it's relevant here...

TIA

Sivakatirswami

 

by: GertonePosted on 2005-06-10 at 15:07:28ID: 14193005

Just my little 2cts.
metalmickeys solution would work if you set the output to text <xsl:output method="text"/>
Your XSLT processor isn't tempted then to escape the content of the CDATA section with &lt; etc
and you will get the tagged results as you please.

If you really need the output being XML or XHTML for other reasons,
you can always think of a second pass copying the whole bit but setting the output method to text in the second pass.
That will replace all the &lt; with '<'

Please note that CDATA doesn't give you much control in XSLT.
The XPath data model is unaware of CDATA sections and considers them as text nodes (transforming them to PCDATA if output is XML or HTML, if need be)

If you can solve the character escaping in the UI, then using an entry scheme other than CDATA would be a good idea as well

Anyway, try the method='text' first

Gertone

 

by: SivakatirswamiPosted on 2005-06-10 at 19:59:59ID: 14193998

Gertone: thanks for joinging in...

Well, that doesn't work either...

<?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="text"/>
      <xsl:template match="/">
            <html>
                  <head> [etc.]

results in: Yikes! :-) a raw text web page!

http://www.himalayanacademy.com/resources/bodhinatha/editorials/test-3.xml

What if we just don't use CDATA for this node in the first place? I can have my UI escape any illegal chars that would break in parsers that are processing that node as PCDATA ( I think there is only two right?) would that give us better options then to tag the paragraphs with HTML block mark up?

I could live with "<br />" or double "<br /><br /> if necessary, in which case perhaps devPlayer's answer in

http://www.experts-exchange.com/Web/Web_Languages/XML/Q_20919151.html?query=line+breaks&topics=105

is the way to go... (not the accepted solution there... which was to use <pre>... which doesn't work in this scenario...but the one after that...)

but that XSL code is a bit over my head... where do you go on the next for learning this stuff? w3School is rudimentary..


Sivakatirswami




 

by: GertonePosted on 2005-06-11 at 01:47:09ID: 14194703

You don' want to hear this...
there is a way out of this text-serialisation
look at this, I got it working with your Stylesheet
<xsl:output method="text"/>
      <xsl:template match="/">
            &lt;html&gt;
            &lt;head&gt;

If you really want to do some hacking... that could be a way to go.
Of course you don't maintain your stylesheet this way.
You create a decent stylesheet, and before publishing you run some regex over the stylesheet to replace the "<" etc.
The regex takes out every "tag" that is not in the XSL namespace... can be done in a couple of lines of Python, Perl or OmniMark code
but damn ugly :-)

Gertone

 

by: GertonePosted on 2005-06-11 at 02:06:17ID: 14194721

The reference you gave to an earlier question, is a workable solution too.

To answer your "where-to-learn-XSLT" question in that case...

good FAQ information lists
http://www.dpawson.co.uk/xsl/index.html
http://www.mulberrytech.com/xsl/xsl-list/index.html

webinar training and ebook
http://www.cranesoftwrights.com/

a book I like
http://www.amazon.co.uk/exec/obidos/ASIN/0596000537/qid=1118480484/sr=1-2/ref=sr_1_3_2/202-6603183-6943039

lots of sample programs
http://www.amazon.co.uk/exec/obidos/ASIN/0596003722/ref=pd_bxgy_img_2_cp/202-6603183-6943039

and of course this reference book
http://www.amazon.co.uk/exec/obidos/ASIN/0764569090/qid=1118480484/sr=2-1/ref=sr_2_3_1/202-6603183-6943039

but there are others... de gustibus et coloribus ....
gertone

 

by: metalmickeyPosted on 2005-06-11 at 12:44:03ID: 14196106

I may have missed something, whislt skimming your posts but if you can ditch the cdata section, you can control inline html markup by using the DOE function in your template call.

so for this xml

<transcript_text>

<p class="transcript_text">Conditions in the world today are certainly troubling,</p>
<p class="transcript_text">New 2nd paragraph...  The shocking attacks in New York on  September 11, 2001,</p>
<p class="transcript_text">3rd paragraph... One of the immediate consequences of 9/11 was  the television coverage etc.</p>
</transcript_text>

use a template like this

<xsl:template match="transcript_text">
     <xsl:value-of select="." disable-output-escaping="yes">
</xsl:template>

This will override the &lt;s escaping and render your html within the browser..

but your problem with users inputting &'s will still remain.

Your preference to stick with the xml paradigm is a resolute one, but markup is markup in xml is just a description of content so whether you use <p> or <p clas="myclass"> or even <para> its still of some value...

maybe theres a halfway house with

<transcript_text>
<![CDATA[  
<p>Conditions in the world today are certainly troubling,</p>
<p>New 2nd paragraph...  The shocking attacks in New York on  September 11, 2001,</p>
<p>3rd paragraph... One of the immediate consequences of 9/11 was  the television coverage etc.</p>
]]>
</transcript_text>


<xsl:template match="transcript_text">
     <xsl:value-of select="." disable-output-escaping="yes">
</xsl:template>

(All examples untested)

failing that, if you are controlling user input via some sort of UI, why not have more that one entry box within trascript text node to collect para information, you could then keep the <![CDATA ]]> sections for the apples & pears within the paras.
 
Ive tried this method and although its tedious trying to guess how many paras users are going to collect, it may be the closest to nirvana your gonna get...Well for today at least


MM

 

by: SivakatirswamiPosted on 2005-06-11 at 13:17:43ID: 14196191

OK thanks.. I need to defer research on this for a few days... another project is looming here... I would *really, really* prefer to find a solution that simply takes vertical white space in any node (CDATA or just any PCDATA node) and wraps those para's with tags.

So, I'll play with DevPlayer's offerings in the line break thread from before... if I can get my head around it... or bail out of XSL altogether (sigh... I was hoping this would work...) or find a middle ground... rather than using a style sheet for delivery, I can just use it to transform on the back end and pipe that into another program which will parse that node (now a <div>) in the XHTML and tag the paras...and then send it out port 80...(back to the browser) or.. in cases where content is static, save the static XHMTL file to disk and have the web UI use URLs to those instead of to the XML files...

 i.e. XSL gets me 98% there then I only need a few lines of code in another program to tag paragraphs inside <transcript> node or <article> node... where the users are entering data with carriage returns after each paragraph.

But thanks for taking the time to share your ideas... I'll try them all out.

Sivakatirswami

 

by: GertonePosted on 2005-06-11 at 13:21:21ID: 14196199

Trying everything out,
save yourself some time and
forget about my &lt; &gt; suggestion
being sleepy, I missed out on "disable-output-escaping"
metalmickey had a better night than I had :-)

 

by: SivakatirswamiPosted on 2005-06-30 at 15:03:11ID: 14343037

I can't believe I didn't see this solution from the beginning. Assuming, as it is in our case, all paragraphs are in one node. the solution is exceeding simple:

The following should *not* be in a CDATA section

<transcript_text>
<p> some paragraph 1</p>
<p> some paragraph 2</p>
<p> some paragraph 3</p>


then in the XSLT you just use:

 <xsl:for-each select="audio_transcript/transcript_text/p">
       <p class="transcript"><xsl:value-of select="."/></p>
</xsl:for-each>

viola! it works ... easy! or am I missing something? Some Caveat that will bite me later?

Sivakatirswami

 

by: SivakatirswamiPosted on 2005-06-30 at 15:47:59ID: 14343346

If I use this solution:

 <xsl:for-each select="audio_transcript/transcript_text/p">
       <p class="transcript"><xsl:value-of select="."/></p>
</xsl:for-each>

Is it now possible to force the XSL to pass to the browser any html mark up *inside* the paragraphs

<transcript_text>
<p> some <b>paragraph</b> 1</p>
<p> some paragraph 2</p>
<p> <i>someforeignword</i> paragraph 3</p>
</transcript>

where the desired output is in the final HTML:
<html>
<body>

<p class="transcript"><p> some <b>paragraph</b> 1</p>
<p  class="transcript"> some paragraph 2</p>
<p  class="transcript"> <i>someforeignword</i> paragraph 3</p>

</body></html>

right now if I try this.. the text is included but not the <b> tags are not passed to the html output and using:


 <xsl:for-each select="audio_transcript/transcript_text/p">
       <p class="transcript"><xsl:value-of select="." disable-output-escaping="yes" /></p>
</xsl:for-each>

Doesn't do anything.

Sivakatirswami

 

by: GertonePosted on 2005-07-01 at 01:42:43ID: 14345618

what if you use
<p class="transcript"><xsl:copy-of select="."/></p>

 

by: SivakatirswamiPosted on 2005-08-12 at 19:05:23ID: 14666261

I decided to accept Mickey's answers, which, though not an actual solution, led me in the right direction. I had never used "apply templates" before... but the solution turned out to be much more straight forward... as MM said "mark up is mark up" OK. so then I realized, having control over the input from the UI app for data entry (written in Revolution transcript) I simply wrapped the paragraphs with <p>para</p> tags on output.

Then duh..."ditch the CDATA" and  just treat these as XML child nodes instead of thinking of them as HTML paragraph block mark, up... i.e. think different... then this works just dandy:

  <xsl:template match="p">
        <p class="transcript">
            <xsl:apply-templates />
        </p>
    </xsl:template>
   
    <xsl:template match="b">
        <b>
            <xsl:apply-templates />
        </b>
    </xsl:template>

Very easy.

 So much too learn and too little time!  :-)

Sivakatirswami

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...