xsl cdata issue

L

Lals

Hi,
I am stuck in problem involving transformation where I want to put my
source contents as itself in the target XML
My input xml looks like

<root>
<State name = "abc">
mystate<sub1>xx</sub1>
<p>This is description</p>
</State>
</root>

My output XML will look like ( I want to put all the contents inside
the 'state' tag to 'CDATA' section as itself)

<root>
<State name = "abc">
<!CDATA[
mystate<sub1>xx</sub1>
<p>This is description</p>]]>
</State>
</root>

How can I achieve this transformation using XSLT?
any information will be a great help for me.
thanks in advance

Regards,
Lals
 
J

Joseph Kesselman

Well, you can tell XSL that some elements should output their text
content as CDATA sections. The complication is, you also want to convert
some elements into text. For that, what I would suggest is that you use
a Mode to select a second set of templates, one which is based on the
identity transform but which outputs text equivalents rather than
copying the nodes.

Personal reaction: A better solution is to go to whoever asked you to do
this and hit them over the head with the XML specification until they
change their mind. Embedding XML markup in text is amost never a good
solution; it makes life far more difficult for everyone who ever has to
touch the file..
 
L

Lals

Hi,
Thanks for your reply Mr.Josseph Kesselman
As you said, I had try to specify the 'state' tag in xsl:eek:utput cdata
elements and its ok when there are simple contents(no other inner
tags), but when I tried to put the tags inside the 'state' tag it will
not working.
Could you please specify how I will select a mode to second set of
templates as you said.

Your personal reaction is just same as me, but the thing is in the
source XML there are contents, which are embeded in 'subscript' or
'superscript' or '<p>' tags etc. So we don't want to miss these type of
contents when they are displaying that is why we are trying to put that
type of data inside the 'CDATA ' elements. Anyway once again thanking
you for your kind advice. I am in stcuk with it, in the last three
days. I will try the way that you said.

regards
Lals
 
J

Joe Kesselman

Lals said:
tags), but when I tried to put the tags inside the 'state' tag it will
not working.

As I said: You need to write a template which matches on elements and
goes though the work of outputting the literal string equivalents (<,
element QName, space, attribute name, =, attribute value, namespace
name, =, namespace value, >, call apply-templates (still in this mode)
to process contents, </, qname, >). You'll need something similar for
comments and processing instructions, if you want to handle those properly.

"It's Just a Simple Matter Of Programming." Ugly, but doable.
Your personal reaction is just same as me, but the thing is in the
source XML there are contents, which are embeded in 'subscript' or
'superscript' or '<p>' tags etc. So we don't want to miss these type of
contents when they are displaying

The right answer is just to say that the contents can themselves be XML
structure, and insert the tags directly. If you're worries about
confusing them with the tags in the enclosing language, that's exactly
what namespaces are for.

Yeah, I know, sometimes you're stuck with having to try to work with
something designed by someone else. But I'd still recommend trying to
get the other tool fixed if at all possible.
 
P

Peter Flynn

Lals said:
Hi,
Thanks for your reply Mr.Josseph Kesselman
As you said, I had try to specify the 'state' tag in xsl:eek:utput cdata
elements and its ok when there are simple contents(no other inner
tags), but when I tried to put the tags inside the 'state' tag it will
not working.
Could you please specify how I will select a mode to second set of
templates as you said.

Your personal reaction is just same as me, but the thing is in the
source XML there are contents, which are embeded in 'subscript' or
'superscript' or '<p>' tags etc. So we don't want to miss these type of
contents when they are displaying that is why we are trying to put that
type of data inside the 'CDATA ' elements.

So just output them as-is(an identity transform for those element
types). There is no need for CDATA in this case.

And show this to whoever asked for CDATA:
http://xml.silmaril.ie/authors/cdata/

///Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top