You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by ajay bhadauria <ab...@yahoo.com> on 2011/11/17 05:58:47 UTC

Need to copy all the cdata elements to output xml using xslt

Hi,

I need to copy all the elements including cdata element of a parent element. I do not know that which child element is going to have cdata element however I need to copy all the children to output xml file using xslt.

Is there any we can do that ? Any help will be great.

My XML file is
<SwInt:RequestPayload xmlns:SwInt="abc.txt">
    <SwInt:firstName>Robert</SwInt:firstName>
    <SwInt:lastName>Dowson</SwInt:lastName>
    <SwInt:address>Nokesville</SwInt:address>
    <SwInt:Data><![CDATA[aaaaa<>]]></SwInt:Data>
    <SwInt:Data1>aaaaa&lt;&gt;</SwInt:Data1>
</SwInt:RequestPayload>

Regards
Ajay 

Re: Need to copy all the cdata elements to output xml using xslt

Posted by Mukul Gandhi <ga...@gmail.com>.
Hi Ajay,
   It seems the attribute cdata-section-elements of xsl:output may serve
your need. The XSLT (1.0) spec says, this specifies a list of the names of
elements whose text node children should be output using CDATA sections.

Secondly I think, your legacy application expecting CDATA markers in XML
documents, and using text in them as normal text nodes may be a design
error (looks like an incorrect modeling of XML information -- this is a
common understanding among people who model XML data) in the legacy
application. If you see opportunity to fix your legacy application with
this respect, you may pursue along this path. Having said this, its
difficult for me to say really (since I have very little idea of your
technical environment) if the way you're using CDATA in your legacy
application is an incorrect design -- if you can't change (or can't get it
changed) the legacy application with this respect, then you simply have to
use some of the workarounds suggested in this thread or may have to use
other approaches.


On Thu, Nov 17, 2011 at 7:44 PM, ajay bhadauria <ab...@yahoo.com>wrote:

>
> Thanks a lot for reply,
>
> I agree with you. However, in our case, the other side of the legacy
> application expects all element with CDATA section.
>
> So, If I need to get all the CDATA section in the output xml, what should
> I do in the xslt where I do not know which elements have CDATA but I need
> all the elements in the output.
>
> Regards
> Ajay
>




-- 
Regards,
Mukul Gandhi

Re: Need to copy all the cdata elements to output xml using xslt

Posted by ke...@us.ibm.com.
<xsl:output> has an optional attribute called cdata-section-elements. See 
http://www.w3.org/TR/xslt#element-output

This would require that you explicitly list the element qnames whose text 
content you want treated this way, and it's applied to every instance of 
those elements. You must know in advance which kinds of elements would 
need to be treated this way. Sorry, but it doesn't accept match patterns 
or wildcards, only qnames, so an "identity transformation with CDATA 
sections" is not supported.

If the latter is really what you need, I'd suggest you have the XSLT 
processor produce SAX events as output, and hook in your own serializer 
which has been wrapped with a filter which causes all text nodes to be 
written out in this form. You'd still get the benefits of XSLT for other 
document processing tasks.


______________________________________
"You build world of steel and stone
I build worlds of words alone
Skilled tradespeople, long years taught:
You shape matter; I shape thought."
(http://www.songworm.com/lyrics/songworm-parody/ShapesofShadow.html)



From:
ajay bhadauria <ab...@yahoo.com>
To:
xalan-j-users@xml.apache.org, 
Date:
11/17/2011 09:26 AM
Subject:
Re: Need to copy all the cdata elements to output xml using xslt





Thanks a lot for reply,
 
I agree with you. However, in our case, the other side of the legacy 
application expects all element with CDATA section. 
 
So, If I need to get all the CDATA section in the output xml, what should 
I do in the xslt where I do not know which elements have CDATA but I need 
all the elements in the output.
 
Regards
Ajay 

--- On Thu, 11/17/11, keshlam@us.ibm.com <ke...@us.ibm.com> wrote:

From: keshlam@us.ibm.com <ke...@us.ibm.com>
Subject: Re: Need to copy all the cdata elements to output xml using xslt
To: "ajay bhadauria" <ab...@yahoo.com>
Cc: xalan-j-users@xml.apache.org
Date: Thursday, November 17, 2011, 10:37 AM

Semantically, there is no difference between text within a CData Section 
and other text (possibly with characters escaped as Numeric Character 
References). It's only a matter of how you happened to express that 
particular value in the file.

In particular, the XML Data Model doesn't distinguish between these, the 
XPath Data Model doesn't distinguish between these, and hence XSLT doesn't 
distinguish between these.

So: Don't worry about it. Let the parser and serializer deal with this, 
rather than trying to preserve the specific CData Section markup. The 
output may not look the same as the input, but will be semantically 
identical for any properly-written XML application.

If you know that specific elements do want their content expressed as 
CData Sections, XSLT does provide a way to express that hint. But if you 
don't know that in advance, you probably don't need to know it at all. 


______________________________________
"You build world of steel and stone 
I build worlds of words alone 
Skilled tradespeople, long years taught: 
You shape matter; I shape thought." 
(http://www.songworm.com/lyrics/songworm-parody/ShapesofShadow.html) 



From: 
ajay bhadauria <ab...@yahoo.com> 
To: 
xalan-j-users@xml.apache.org, 
Date: 
11/17/2011 12:00 AM 
Subject: 
Need to copy all the cdata elements to output xml using xslt





Hi,

I need to copy all the elements including cdata element of a parent 
element. I do not know that which child element is going to have cdata 
element however I need to copy all the children to output xml file using 
xslt.

Is there any we can do that ? Any help will be great.

My XML file is
<SwInt:RequestPayload xmlns:SwInt="abc.txt">
   <SwInt:firstName>Robert</SwInt:firstName>
   <SwInt:lastName>Dowson</SwInt:lastName>
   <SwInt:address>Nokesville</SwInt:address>
   <SwInt:Data><![CDATA[aaaaa<>]]></SwInt:Data>
   <SwInt:Data1>aaaaa&lt;&gt;</SwInt:Data1>
</SwInt:RequestPayload>

Regards
Ajay 




Re: Need to copy all the cdata elements to output xml using xslt

Posted by ajay bhadauria <ab...@yahoo.com>.
Thanks a lot for reply,
 
I agree with you. However, in our case, the other side of the legacy application expects all element with CDATA section. 
 
So, If I need to get all the CDATA section in the output xml, what should I do in the xslt where I do not know which elements have CDATA but I need all the elements in the output.
 
Regards
Ajay  

--- On Thu, 11/17/11, keshlam@us.ibm.com <ke...@us.ibm.com> wrote:


From: keshlam@us.ibm.com <ke...@us.ibm.com>
Subject: Re: Need to copy all the cdata elements to output xml using xslt
To: "ajay bhadauria" <ab...@yahoo.com>
Cc: xalan-j-users@xml.apache.org
Date: Thursday, November 17, 2011, 10:37 AM


Semantically, there is no difference between text within a CData Section and other text (possibly with characters escaped as Numeric Character References). It's only a matter of how you happened to express that particular value in the file.

In particular, the XML Data Model doesn't distinguish between these, the XPath Data Model doesn't distinguish between these, and hence XSLT doesn't distinguish between these.

So: Don't worry about it. Let the parser and serializer deal with this, rather than trying to preserve the specific CData Section markup. The output may not look the same as the input, but will be semantically identical for any properly-written XML application.

If you know that specific elements do want their content expressed as CData Sections, XSLT does provide a way to express that hint. But if you don't know that in advance, you probably don't need to know it at all. 


______________________________________
"You build world of steel and stone 
I build worlds of words alone 
Skilled tradespeople, long years taught: 
You shape matter; I shape thought." 
(http://www.songworm.com/lyrics/songworm-parody/ShapesofShadow.html) 






From: 
ajay bhadauria <ab...@yahoo.com> 

To: 
xalan-j-users@xml.apache.org, 

Date: 
11/17/2011 12:00 AM 

Subject: 
Need to copy all the cdata elements to output xml using xslt









Hi,

I need to copy all the elements including cdata element of a parent element. I do not know that which child element is going to have cdata element however I need to copy all the children to output xml file using xslt.

Is there any we can do that ? Any help will be great.

My XML file is
<SwInt:RequestPayload xmlns:SwInt="abc.txt">
   <SwInt:firstName>Robert</SwInt:firstName>
   <SwInt:lastName>Dowson</SwInt:lastName>
   <SwInt:address>Nokesville</SwInt:address>
   <SwInt:Data><![CDATA[aaaaa<>]]></SwInt:Data>
   <SwInt:Data1>aaaaa&lt;&gt;</SwInt:Data1>
</SwInt:RequestPayload>

Regards
Ajay 


Re: Need to copy all the cdata elements to output xml using xslt

Posted by ke...@us.ibm.com.
Semantically, there is no difference between text within a CData Section 
and other text (possibly with characters escaped as Numeric Character 
References). It's only a matter of how you happened to express that 
particular value in the file.

In particular, the XML Data Model doesn't distinguish between these, the 
XPath Data Model doesn't distinguish between these, and hence XSLT doesn't 
distinguish between these.

So: Don't worry about it. Let the parser and serializer deal with this, 
rather than trying to preserve the specific CData Section markup. The 
output may not look the same as the input, but will be semantically 
identical for any properly-written XML application.

If you know that specific elements do want their content expressed as 
CData Sections, XSLT does provide a way to express that hint. But if you 
don't know that in advance, you probably don't need to know it at all.


______________________________________
"You build world of steel and stone
I build worlds of words alone
Skilled tradespeople, long years taught:
You shape matter; I shape thought."
(http://www.songworm.com/lyrics/songworm-parody/ShapesofShadow.html)



From:
ajay bhadauria <ab...@yahoo.com>
To:
xalan-j-users@xml.apache.org, 
Date:
11/17/2011 12:00 AM
Subject:
Need to copy all the cdata elements to output xml using xslt




Hi,

I need to copy all the elements including cdata element of a parent 
element. I do not know that which child element is going to have cdata 
element however I need to copy all the children to output xml file using 
xslt.

Is there any we can do that ? Any help will be great.

My XML file is
<SwInt:RequestPayload xmlns:SwInt="abc.txt">
    <SwInt:firstName>Robert</SwInt:firstName>
    <SwInt:lastName>Dowson</SwInt:lastName>
    <SwInt:address>Nokesville</SwInt:address>
    <SwInt:Data><![CDATA[aaaaa<>]]></SwInt:Data>
    <SwInt:Data1>aaaaa&lt;&gt;</SwInt:Data1>
</SwInt:RequestPayload>

Regards
Ajay