wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes Failed to set external entity into CDATA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Failed to set external entity into CDATA" Watch "Failed to set external entity into CDATA" New topic
Author

Failed to set external entity into CDATA

Leon Pu
Greenhorn

Joined: Nov 02, 2005
Posts: 22
Hi all,

I have a non-xml file which need to be included in a xml file.

I use external entity to include the non-xml file, but it doesn't affect to set external entity to CDATA.

Here are my files.

[xml-file]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script [
<!ENTITY datepicker SYSTEM "datepicker.js">
]>

<script>
<body>
<![CDATA[
&datepicker;
]]>
</body>
</script>
[xml-file]

[non-xml-file]
function getScroll () {
if (document.all && typeof document.body.scrollTop != "undefined") {// IE model
var ieBox = document.compatMode != "CSS1Compat";
var cont = ieBox ? document.body : document.documentElement;
return {
left:cont.scrollLeft,
top:cont.scrollTop,
width:cont.clientWidth,
height:cont.clientHeight
};
}
else {
return {
left:window.pageXOffset,
top:window.pageYOffset,
width:window.innerWidth,
height:window.innerHeight
};
}

}
[non-xml-file]


Best regards,
Leon
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18643
    
    8

What makes you think that entity references in CDATA should be expanded? I don't see where in the XML recommendation it says that, and in fact it says about CDATA sections that they are "used to escape blocks of text containing characters which would otherwise be recognized as markup". My Google search for "external entity cdata" turned up a couple of other newsgroup posts about that topic including this response:

http://mail.python.org/pipermail/xml-sig/2001-May/005525.html

from someone who wrote an XSLT processor so presumably knows what he's talking about.
Leon Pu
Greenhorn

Joined: Nov 02, 2005
Posts: 22
I also got the mail-list, but I cannot understand the meaning.

I also found an article which describes use CDATA inside <!ENTITY>. But it is impossibile to put CDATA inside <!ENTITY>, at least in my IE environment, only NDATA is allowed.

http://www.oasis-open.org/cover/english-cdata.html#SEC4

I just wonder why it cannot put non-xml entity into CDATA. Use NDATA you can handle jsp or postscript file. It should be easy to convert the non-xml entity to xml acceptable CDATA content.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18643
    
    8

The "why" part wasn't obvious to me either. But Uche's advice in that mail-list link was pretty clear: just don't put the entity reference inside CDATA. It looks like that would be extremely simple to do in your example.
Leon Pu
Greenhorn

Joined: Nov 02, 2005
Posts: 22
Originally posted by Paul Clapham:
The "why" part wasn't obvious to me either. But Uche's advice in that mail-list link was pretty clear: just don't put the entity reference inside CDATA. It looks like that would be extremely simple to do in your example.


Do you mean the following content from Uche's mail-list?

Unfortunately there is no easy solution. You'd have to wrap all the entities in ]]>&entity;<![CDATA[


But it doesn't work at all. "&entity;" is still non-xml content.

Is there any other way to import non-xml file to xml? Maybe external entity is a dead way, or import non-xml as xml content is impossible.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18643
    
    8

Oh, I see. After messing about with those files for a bit I suddenly realized what your original question was. (The one for which your answer was "Let's wrap this in a CDATA section.") It was "How do I put text into an XML document if it makes that document not well-formed XML?" I didn't notice that the point of the question was that there were ampersands in the javascript.

Well, I think the answer is to make it well-formed. Either escape the ampersands (per the XML rules) or put the <![CDATA[...]]> brackets around the text in the datepicker.js file. Either of those works for me in the sense that it results in well-formed XML that can be parsed and transformed.

Are you going to say you can't do that because then it won't be correct javascript? You may be right. But if you are right then it would be impossible to embed that javascript in an XHTML file, which I suspect is not the case. I don't generally embed scripts in HTML so I don't know the rule, but surely there is a rule for embedding javascript in XHTML. If there is, I would bet it involves escaping ampersands. But as I said, I don't know.
Leon Pu
Greenhorn

Joined: Nov 02, 2005
Posts: 22
Originally posted by Paul Clapham:
Oh, I see. After messing about with those files for a bit I suddenly realized what your original question was. (The one for which your answer was "Let's wrap this in a CDATA section.") It was "How do I put text into an XML document if it makes that document not well-formed XML?" I didn't notice that the point of the question was that there were ampersands in the javascript.

Well, I think the answer is to make it well-formed. Either escape the ampersands (per the XML rules) or put the <![CDATA[...]]> brackets around the text in the datepicker.js file. Either of those works for me in the sense that it results in well-formed XML that can be parsed and transformed.

Are you going to say you can't do that because then it won't be correct javascript? You may be right. But if you are right then it would be impossible to embed that javascript in an XHTML file, which I suspect is not the case. I don't generally embed scripts in HTML so I don't know the rule, but surely there is a rule for embedding javascript in XHTML. If there is, I would bet it involves escaping ampersands. But as I said, I don't know.


Thank you for your hint Paul.

I found the answer in following url.

http://www.codehouse.com/javascript/tips/xhtml/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Failed to set external entity into CDATA