Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CDATA handling 1

Status
Not open for further replies.

anuj7700

Programmer
Jan 19, 2005
2
US
Hi everybody there,
Hope you guys can help me here. I am having an XML that contains CDATA sections and these CDATA sections contain text and the HTML Formating tags for formatting that text something like
<Tag><![CDATA[ <FONT size="10"> <I> Hello & There </I> </FONT>]]>

Now as you see here this CDATA section contains tags as well as may contain some more special characters like '&' here.

I have written an XSL to transform this XML. Now the thing is that i want to process the content of CDATA section as well lets say i want to replace <FONT size="10"> <I> Hello & There </I> </FONT> with <fo:inline font-size="10"> Hello & There </fo:inline>

I am not able to find a way to process the content of CDATA and apply transformation to it.

Please let me know if anybody knows a way to do this. I have ready templates for html formatting tags which may appear in CDATA section but i don't know a way to subject this content to these templates.

Thanks in advance for ur replies
 
All text in an XML document will be parsed by the parser.
Only text inside a CDATA section is ignored by the parser.
See for more info...

CDATA is not meant to be proccessed... It is strictly for display...

You may be able to set up a series of <xsl:if> tags to handle the CDATA...
But, you need to take the tags out of the CDATA block if you want to process them the correct way...

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
You might be able to read the contents of the CDATA section into a XSL variable, but then you'd be doing string parsing to find stuff inside. yuck.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
chiph said:
...but then you'd be doing string parsing to find stuff inside. yuck.

Yeah! That's what I why hinting at with the "...series of <xsl:if> tags to handle the CDATA..."

Personally, I wouldn't want to bother going to all that trouble ;-)

My question is...
Why are you using CDATA to store tags???

CDATA is mainly meant for Code DATA, such as javascript, C++, or any other language, that could have characters that conflict with XML/XSL characters...

If you are just using it for:
Code:
<Tag><![CDATA[ <FONT size="10"> <I> Hello & There </I> </FONT>]]>

Why can't you simply use:
Code:
<Tag>
  <FONT size="10">
    <I>Hello & There</I>
  </FONT>
</Tag>

And use XSL, the way it was meant to be used, to transform the tags...
Code:
<xsl:for-each select="Tag">
  <fo:inline font-size="{@size}"><xsl:value-of select="I" /></fo:inline>
</xsl:for-each>

Though, your <xsl:for-each> method might be a little more complex, when you actually apply it...
Depending on what you are actually trying to accomplish ;-)

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
An example of a useful way to use CDATA that comes to my mind would be...

Say you are writing a program for, say, JavaScript, and you want to store functions with descriptions in xml, via tags...

The code could contain one of these characters: < > " ' &

So you would use CDATA to store this code in XML, so it won't hang up the parser...
Code:
<JavaStuff>
  <Function>
    <Name>average</Name>
    <Description>Function to average 2 numbers</Description>
    <Code>
<![CDATA[
function average(a,b)
{
  var c = (a + b) / 2;
  return c;
}
]]>
    </Code>
  </Function>
  <Function>
    <Name>add</Name>
    <Description>Function to add 2 numbers</Description>
    <Code>
<![CDATA[
function add(a,b)
{
  var c = a + b;
  return c;
}
]]>
    </Code>
  </Function>
</JavaStuff>

But, If you are just using tags, such as for html, there is no reason to use CDATA

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Actually i am immplementing a web based form which also includes text formatting editors in some fields. So i need to store the HTML formating information that may be applied to the text content and then there can be special charaters that user might want to enter as a part of text.
So at moment i just enclose all the field content inside a CDATA section and thus preserve everything.
Isn't there any wayout using template functions or anything else ofcourse i do not want to do string parsing as that would be too painfull as you know well. Haven't you ever come across any such thing done previously.

 
Why do you need to put the HTML tags in CDATA sections? The XML will maintain special characters.

Jon
 
anuj7700 said:
So i need to store the HTML formating information that may be applied to the text content and then there can be special charaters that user might want to enter as a part of text.

Again... like I said above... The ONLY text that needs to be placed in CDATA is text with characters that conflict with XML...

Such as where < means greater than, instead of the start of a tag...

HTML does NOT conflict with XML, they are based on the same language style...

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
But even if you didn't want to use CDATA sections, you could still use the XML reserved characters, you'd just have to use their entity version:
[ignore]
< &lt;
> &gt;
& &amp;
' &apos;
" &quot;
[/ignore]

Messy, but works.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
chiph said:
Messy, but works.

Messy... heh... Yeah...
You can say that again ;-)

<rant>
I wish they would have decided to just use some form of the good 'ole escape set...
Like:
\<
\>
\&
\'
\"
\\


<sarcasm>
I know I just love to see stuff like:
Code:
&lt;test value=&quot;test&quot;&gt;x &amp;&amp; y &gt; z&lt;/test&gt;
</sarcasm>

Personally, I think this would be easier on the eyes ;-)
Code:
\<test value=\"test\"\>x \&\& y \> z\</test\>
Maybe not much... but definitely a little...

But I guess we're stuck with: &x;

(plus, I guess it "could" interfere with the URLs... but, COME ON...
</rant>

OK... 'nuff of that...:)

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top