472,779 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 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 7024
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.