473,809 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.fro mstring(text)
ElementTree.dum p(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 8600
al********@yaho o.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*****@python ware.com> wrote in message
news:d7******** **@sea.gmane.or g...
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.1000000000000 001 # 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********@yah oo.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*@doxd esk.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
3092
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: ---------------------------------------------------------------------- #!/usr/bin/env python import sys, os from elementtree import ElementTree
0
2425
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 the client's data. If I comment out the parser call I can see that the transmition time of large data files is very low. My understanding is that a SAX parser should transmit the content of a CDATA string downstream without signiificant...
3
3975
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 to the XML() method in ElementTree.py, an ExpatError exception is raised. I can trap the exception with a try/except where the except does not specify a specific exception, but I cannot figure out how to construct the except clause in a...
1
3073
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
4635
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, which I understand works in much the same way (with some significant additions). In particular, it shares the use of a .tail attribute. I ran headlong into this aspect of the API while doing some DOM manipulations, and it's got me pretty...
2
5924
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, html tags are in escape form (e.g. is &gt;). When I display that string I see those tags. Basically I am getting all this data as xml form and I want to find out how can I change those html tags into regular tags, and also how to remove CDATA
18
758
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 create DOM-object, i get the value of "Data" without "\r\n"
2
9799
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 tag, but I get None. data = """<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/ soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9
3267
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
9601
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10637
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7660
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.