473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Detecting CDATA sections with XSLT

Hi folks,

I'm writing a web-page editing tool for my company which will allow
staff (with no "technical" expertise) to maintain their own Intranet sites.
The content for each webpage is stored in the form of XHTML in an XML
document (which, in turn, is stored in an XML database). So far so good.
However the editing tool must allow users to paste in the contents of MS
Word documents. I soon discovered that Word does not generate
properly-formed HTML, the main problem being that tags that should be nested
are often "overlapped" (as my example below shows). My solution is to store
this "bad" data as CDATA sections, thereby preventing the finished XML
document from being invalidated. My finished XML document looks something
like this:
<page id="0001">
<content>
<p>
<i>
<font face="Arial">Properly-formed HTML</font>
</i>
</p>
<![CDATA[<p><i><font face="Arial">The 'i' and 'font' end-tags are
wrong and there is no end-tag for 'p'</i></font>]]>
<p>
<i>
<font face="Arial">This is OK.</font>
</i>
</p>
</content>
</page>
On retrieving a document for formatting and display within the client
browser, my XSL template for the <content> nodes needs to be able to detect
whether each of its children can be regarded as proper XML (and, therefore,
to transform the it into HTML) or a CDATA section whose contents will simply
be passed straight to the browser. So my template needs to look something
like this:
<xsl:template match="content">
<xsl:for-each select="*">
<xsl:choose>
<xsl:when test="nodetype(.)=cdata()">
<xsl:value-of select=".">
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
Of course it's the fourth line of code - <xsl:when
test="nodetype(.)=cdata()"> - that is giving me problems. Unfortunately I am
stuck with a fairly basic XSLT engine that has none of the fancy additional
functions MSXML, SAXON or Xalan offer. Try as I might, I can't find a way of
getting XSLT to tell when it's dealing with a CDATA section.

(I could simply hold everything as CDATA but in the future I am going to
have to interface with other systems that will demand as much content a
possible be presented as proper XML/XHTML.)

Any ideas would be very much appreciated!

--

Many thanks in advance!
Dave Matthews

'New Avengers' and 'Professionals' sites at:
http://www.mark-1.co.uk
Jul 20 '05 #1
2 7051
Dave Matthews wrote:
On retrieving a document for formatting and display within the
client
browser, my XSL template for the <content> nodes needs to be able to
detect whether each of its children can be regarded as proper XML
(and, therefore, to transform the it into HTML) or a CDATA section
whose contents will simply be passed straight to the browser.


xslt doesn't see CDATA secions. They're converted to text nodes before
xslt even sees them. But couldn't you just wrap an element around it?

<page id="0001">
****<content>
********<p>
************<i>
****************<font*face="Arial">Properly-formed*HTML</font>
************</i>
********</p>
<non-wellformed>
********<![CDATA[<p><i><font*face="Arial">The*'i'*and*'font'*end-tags*are
wrong and there is no end-tag for 'p'</i></font>]]>
</non-wellformed>
********<p>
************<i>
****************<font*face="Arial">This*is*OK.</font>
************</i>
********</p>
****</content>
</page>

Jul 20 '05 #2
Thanks for your help, guys. You've confirmed what I was rapidly coming to
suspect!

Rolf - your idea of using a "wrapping" element seems an ideal way around
the problem - many thanks!

--
Cheers,

Dave Matthews

'New Avengers' and 'Professionals' sites at:
http://www.mark-1.co.uk
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: John van Terheijden | last post by:
Hi. I'm trying to develop a program that uses XML files store data. I'm using Windows XP, Apache 1.3.29 and PHP 4.3.4. Right now the XML file is read using the xml_parser_create(),...
2
by: DZ | last post by:
How can i put all tag contents into <!]> (i mean using XSL) For example. I have .... <name> <firstname>John</firstname> <lastname>Malkovich</lastname> <name> .....
3
by: Edwin G. Castro | last post by:
Hi, I'm new to XSLT and I'm having a hard time figuring out whether XSLT will do what I need it to do. I have a XML file with a whole bunch of <message> elements. These <message> elements...
10
by: Jon Noring | last post by:
Out of curiosity, may a CDATA section appear within an attribute value with datatype CDATA? And if so, how about other attribute value datatypes which accept the XML markup characters? To me,...
10
by: Simon Brooke | last post by:
Here's my problem: <xsl:template match="/category"> .... <script type="text/javascript"> &lt;!]&gt; </script> .... </xsl:template>
11
by: ericms | last post by:
Can anybody show me how to insert a CDATA section using XPathNavigator ? I have tried the follwing with no luck: XmlDocument docNav = new XmlDocument(); docNav.LoadXml(xmlString);...
2
by: Steveino | last post by:
Hello, Just wondering if anyone could shed any light on this, it's probably me just being silly... I have a dataset that I've used to create an XmlDataDocument, in order to apply XSL. The XSL...
7
by: Vincent Lefevre | last post by:
I'd like to generate something like that: <style type="text/css">/*<!]></style> I thought the following would work, but it doesn't (I tried with xsltproc). <?xml version="1.0"?> ...
0
by: R | last post by:
Hi All, I've run into very disturbing problem, I must use html tags inside one of my XML node so I wrote: $cdataHelp = $DomDocument->createCDATASection($value); when I dump it as XML
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.