473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Elementtree and CDATA handling

I am experimenting with ElementTree and i came accross some
(apparently) weird behaviour.
I would expect a piece of XML to be read, parsed and written back
without corruption (except for the comments and PI which have purposely
been left out). It isn't however the case when it comes to CDATA
handling.
I have the following code:
text="""<html><head>
<title>Document</title>
</head>
<body>
<script type="text/javascript">
//<![CDATA[
function matchwo(a,b)
{
if (a < b && a > 0) then
{
return 1
}
}
//]]>
</script>
</body>
</html>
"""

from elementtree import ElementTree
tree = ElementTree.fromstring(text)
ElementTree.dump(tree)

Running the above piece of code yields the following:

<html><head>
<title>Document</title>
</head>
<body>
<script type="text/javascript">
//
function matchwo(a,b)
{
if (a &lt; b &amp;&amp; a &gt; 0) then
{
return 1
}
}
//
</script>
</body>
</html>

There are two problems: the //<![CDATA[ has disappeared and the <, >
and && have been replaced by their equivalent entities (CDATA should
have prevented that).
I am no XML/HMTL expert, so i might be doing something wrong...
Thank you for helping

Alain

Jul 19 '05 #1
4 8572
al********@yahoo.fr wrote:
There are two problems: the //<![CDATA[ has disappeared and the <, >
and && have been replaced by their equivalent entities (CDATA should
have prevented that).
I am no XML/HMTL expert, so i might be doing something wrong...


you're confusing the external representation of something with the internal
data model.

consider this:
"hello"
'hello'
"hell\x6f"
"hell\157"
"hell" + "o"
'h' 'e' 'l' 'l' 'o'


the above are six ways to write the same string literal in Python. all these result
in a five-character string containing the letters "h", "e", "l", "l", and "o". if you type
the above at a python prompt, you'll find that Python echoes the strings back as
'hello' in all six cases.

in XML, entities, character references, and CDATA sections are three different
way to represent reserved characters. once you've loaded the file, they all "dis-
appear".

</F>

Jul 19 '05 #2

"Fredrik Lundh" <fr*****@pythonware.com> wrote in message
news:d7**********@sea.gmane.org...
you're confusing the external representation of something with the
internal
data model.

consider this:
>>> "hello"
>>> 'hello'
>>> "hell\x6f"
>>> "hell\157"
>>> "hell" + "o"
>>> 'h' 'e' 'l' 'l' 'o'

the above are six ways to write the same string literal in Python.


Minor nit: I believe 'hell' + 'o' is two string literals and a runtime
concatenation operation. Perhaps you meant 'hell' 'o', without the '+',
which I believe is joined to one literal at parsing or compilation time.
all these result
in a five-character string containing the letters "h", "e", "l", "l", and
"o".
if you type the above at a python prompt,
you'll find that Python echoes the strings back as
'hello' in all six cases.


Nit aside, this is a valuable point that bears repeating. Another example
of one internal versus multiple external that confuses many is the
following:
1.1 == 1.1000000000000001 # True

The mapping of external to internal is many-to-one for both strings and
floats and therefore *cannot* be exactly inverted! (Or round-tripped.) So
Python has to somehow choose one of the possible external forms that would
generate the internal form.

Terry J. Reedy


Jul 19 '05 #3
Alain <al********@yahoo.fr> wrote:
I would expect a piece of XML to be read, parsed and written back
without corruption [...]. It isn't however the case when it comes
to CDATA handling.
This is not corruption, exactly. For most intents and purposes, CDATA
sections should behave identically to normal character data. In a real
XML-based browser (such as Mozilla in application/xhtml+xml mode), this
line of script would actually work fine:
if (a &lt; b &amp;&amp; a &gt; 0) {


The problem is you're (presumably) producing output that you want to be
understood by things that are not XML parsers, namely legacy-HTML web
browsers, which have special exceptions-to-the-rule like "<script>
elements don't contain markup" that are not present in XML.

ElementTree is a data binding that strives to simplify the XML
processing experience, and as such it folds CDATA sections down to
plain characters - this is usually easier for programmers to deal with.
Such a feature is considered normal in XML processing, and is the
default for, eg. DOM Level 3 implementations.

If, instead, you want to keep track of where the CDATA sections are,
and output them again without change, you'll need to use an
XML-handling interface that supports this feature. Typically, DOM
implementations do - the default Python minidom does, as does pxdom.
DOM is a more comprehensive but less friendly/Python-like interface for
XML processing.

There are a few other obstacles you may meet if you are outputting XML
for use by a non-XML parser (legacy browsers):

- entity references - &eacute; etc. The HTML entities are not
built into XML so to read them at all you'll need a parser that
reads the external DTD subset (and a suitable !DOCTYPE). Even then
they'll be converted to text, if that matters. (pxdom, optionally,
can keep them as entity references regardless of whether their
content is known);

- empty elements - <img/> etc. An XML serialiser won't know how to
output this is a browser-compatible way. (The next release of pxdom
has an option to do so.)

If you're generating output for legacy browsers, you might want to just
use a 'real' HTML serialiser.

--
Andrew Clover
mailto:an*@doxdesk.com
http://www.doxdesk.com/

Jul 19 '05 #4
Terry Reedy wrote:
the above are six ways to write the same string literal in Python.


Minor nit: I believe 'hell' + 'o' is two string literals and a runtime concatenation operation.


I guess I should have written "constant".

on the other hand, while the difference might matter for current python
interpreter implementations, it doesn't really matter for the user. after
the operation, they end up with a string object that doesn't contain what
they wrote.

</F>

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Greg Wilson | last post by:
I'm trying to convert from minidom to ElementTree for handling XML, and am having trouble with entities in DTDs. My Python script looks like this: ...
0
by: Aidan | last post by:
The setup: a Sax parser in a servlet and a Java client (same machine) which uploads an XML document containing CDATA elements which hold base 64 encoded binary files. The servlet then SAX parses...
3
by: mirandacascade | last post by:
Verion of Python: 2.4 O/S: Windows XP ElementTree resides in the c:\python24\lib\site-packages\elementtree\ folder When a string that does not contain well-formed XML is passed as an argument...
1
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
30
by: Chas Emerick | last post by:
I looked around for an ElementTree-specific mailing list, but found none -- my apologies if this is too broad a forum for this question. I've been using the lxml variant of the ElementTree API,...
2
by: JohnAD | last post by:
Hello NG, I am getting some information from DB, and that data has mix html and XML tags in the content (e.g. detail on country). Basically CDATA types are mixed with regular string. Also,...
18
by: sim.sim | last post by:
Hi all. i'm faced to trouble using minidom: #i have a string (xml) within CDATA section, and the section includes "\r\n": iInStr = '<?xml version="1.0"?>\n<Data><!]></Data>\n' #After i...
2
by: Zvi | last post by:
Hi All, Can someone tell me why id the following not working? I have a soap response envelope, for test purpose it's just a string and I create ElementTree from it. Then I try to find Response...
9
by: shapper | last post by:
Hello, Why do some pages I have seen have //<![CDATA[ in the beginning of a script tag before the script itself? Do I need this? Thanks, Miguel
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.