473,386 Members | 1,819 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,386 software developers and data experts.

Problem with minidom and special chars in HTML

Hi :-)
I currently have quite a big problem with minidom and special chars (for
example ü) in HTML.

Let's say I have following input file:
--------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<body>
&uuml;
</body>
</html>
--------------------------------------------------

And following python script:
--------------------------------------------------
from xml.dom import minidom
if __name__ == '__main__':
doc = minidom.parse('test2.html')
f = open('test3.html','w+')
f.write(doc.toxml())
f.close()
--------------------------------------------------

test3.html only has a blank line where should be the &uuml; It is simply
removed.

Any idea how I could solve this problem?

MfG, Horst
Jul 18 '05 #1
6 3931
Horst Gutmann wrote:
I currently have quite a big problem with minidom and special chars (for example &uuml;) in HTML.

Let's say I have following input file:
--------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<body>
&uuml;
</body>
</html>
-------------------------------------------------- test3.html only has a blank line where should be the &uuml; It is simply
removed.

Any idea how I could solve this problem?


umm. doesn't that doctype point to an SGML DTD? even if minidom did fetch
external DTD's (I don't think it does), it would probably choke on that DTD.

running your documents through "tidy -asxml -numeric" before parsing them as
XML might be a good idea...

http://tidy.sourceforge.net/ (command-line binaries, library)
http://utidylib.berlios.de/ (python bindings)

</F>

Jul 18 '05 #2
Fredrik Lundh wrote:
umm. doesn't that doctype point to an SGML DTD? even if minidom did fetch
external DTD's (I don't think it does), it would probably choke on that DTD.

running your documents through "tidy -asxml -numeric" before parsing them as
XML might be a good idea...

http://tidy.sourceforge.net/ (command-line binaries, library)
http://utidylib.berlios.de/ (python bindings)

</F>


Thanks, but the problem is, that I can't use the numeric representations
of these special chars. I will probably simply play find&replace before
feeding the document into minidom and change the output back afterwards :-)

MfG, Horst
Jul 18 '05 #3
Horst Gutmann napisa³(a):
I currently have quite a big problem with minidom and special chars (for
example &uuml;) in HTML.

Let's say I have following input file:
--------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
HTML4 is not an XML application. Even if minidom will fetch this DTD and
be able to parse character entities, it may not be able to parse the
document.
Any idea how I could solve this problem?


Don't use minidom or convert HTML4 to XHTML and change declaration of
doctype.

--
Jarek Zgoda
http://jpa.berlios.de/ | http://www.zgodowie.org/
Jul 18 '05 #4
Jarek Zgoda wrote:
Horst Gutmann napisa³(a):
I currently have quite a big problem with minidom and special chars
(for example &uuml;) in HTML.

Let's say I have following input file:
--------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

HTML4 is not an XML application. Even if minidom will fetch this DTD and
be able to parse character entities, it may not be able to parse the
document.
Any idea how I could solve this problem?

Don't use minidom or convert HTML4 to XHTML and change declaration of
doctype.

This was just a bad example :-) I get the same problem with XHTML in the
doctype. The funny thing here IMO is, that the special chars are simply
removed. No warning, no nothing :-(

MfG, Horst
Jul 18 '05 #5
Horst Gutmann napisa³(a):
Don't use minidom or convert HTML4 to XHTML and change declaration of
doctype.

This was just a bad example :-) I get the same problem with XHTML in the
doctype. The funny thing here IMO is, that the special chars are simply
removed. No warning, no nothing :-(


As Fredrik pointed out, it's minidom that cann't fetch DTD from remote
location. Download this DTD file to your local machine (it lies at
exactly this URI), try changing PUBLIC identifier to SYSTEM and give
local path to this file.

--
Jarek Zgoda
http://jpa.berlios.de/ | http://www.zgodowie.org/
Jul 18 '05 #6
Horst Gutmann wrote:
I currently have quite a big problem with minidom and special chars
(for example &uuml;) in HTML.


Yes. Ignoring the issue of the wrong doctype, minidom is a pure XML
parser and knows nothing of XHTML and its doctype's entities 'uuml' and
the like. Only the built-in entities (&amp; etc.) will work.

Unfortunately the parser minidom uses won't read external entities -
including the external subset of the DTD (which is where all the stuff
about what "&uuml;" means is stored). And because minidom does not
support EntityReference nodes, the information that there was an entity
reference there at all gets thrown away as it is replaced with the
empty string. Which is kind of bad.

Possible workarounds:

1. pass minidom a different parser to use, one which supports external
entities and which will parse all the DTD stuff. I don't know if there
is anything suitable available, though...

2. use a DOM implementation with the option to support external
entities. For example, with pxdom, one can use DOM Level 3 LS methods,
or pxdom.parse(f, {'pxdom-external-entities': True}).

However note that reading and parsing an external entity will introduce
significant slowdown, especially in the case of the rather complex
multi-file XHTML DTD. Other possibilities:

3. hack the content on the way into the parser to replace the DOCTYPE
declaration with one including entity definitions in the internal
subset:

<!DOCTYPE html PUBLIC "..." "..." [
<!ENTITY uuml "ü">
...
]>
<html>...

4. hack the content on the way into the parser to replace entity
references with character references, eg. &uuml; -> ü. This is
'safe' for simple documents without an internal subset; charrefs and
entrefs can be used in the same places with the same meaning, except
for some issues in the internal subset.

5. use a DOM implementation that supports EntityReference nodes, such
as pxdom. Entity references with no replacement text (or all entity
references if the DOM Level 3 LS parameter 'entities' is set) will
exist as EntityReference DOM objects instead of being flattened to
text. They can safely be reserialized as &uuml; without the
implementation having to know what text they represent.

Entities are a big source of complication and confusion, which I wish
had not made it into XML!

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

Jul 18 '05 #7

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

Similar topics

4
by: Skip Montanaro | last post by:
I'm getting somewhat painfully acquainted with xml.dom.minidom. What is the relationship between its documentElement attribute and its childNodes list? I thought XML documents consisted of a...
5
by: Skip Montanaro | last post by:
I'd like to compare two xml.dom.minidom objects, but the naive attempt fails: >>> import xml.dom.minidom >>> d1 = xml.dom.minidom.parse("ES.xml") >>> d2 = xml.dom.minidom.parse("ES.xml") >>> d1...
5
by: Mike McGavin | last post by:
Hi everyone. I've been trying for several hours now to get minidom to parse namespaces properly from my stream of XML, so that I can use DOM methods such as getElementsByTagNameNS(). For some...
6
by: Jonas Meurer | last post by:
hello, my script selects a comment saved as VARCHAR in MySQL and displays it inside an html page. the problem is, that the comment contains several special characters, as mysterious utf-8...
4
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3...
0
by: Frank M | last post by:
I am writing an application with a dataload class. The development is done in Windows XP, and on the development Pc I can do a dataload and export without any problems if I use...
2
by: Lupus | last post by:
Hi everyone, I've got a problem with a piece of javascript code which parses a string. The problem occurs when the string contains some special characters like é, è, ü... There are 2 symptoms...
2
by: Paulo da Silva | last post by:
Hi! Please consider the following fragment std::cin.exceptions(std::ios::failbit); .... try { is.getline(p,ILEN); ... } catch (std::ios::failure const &problem)
3
by: John Carlyle-Clarke | last post by:
Hi. I'm new to Python and trying to use it to solve a specific problem. I have an XML file in which I need to locate a specific text node and replace the contents with some other text. The...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.