Bug in Elementtree/Expat

A

alainpoint

Hello,

I use Elementtree to parse an elementary SVG file (in fact, it is one
of the examples in the "SVG essentials" book). More precisely, it is
the fig0201.svg file in the second chapter.
The contents of the file are as follows (i hope it will be rendered
correctly):
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="200" height="200">
<title>Default User Coordinates</title>
<desc>Shows a rectangle on a grid in default user coordinates</desc>
<image xlink:href="default_ruler.svg" x="0" y="0" width="200"
height="200"/>
<g transform="translate(40,40)">
<rect x="10" y="10" width="50" height="30"style="stroke: black; fill:
none;"/>
</g>
</svg>

The parsing fails in the following way:
Traceback (most recent call last):
File "C:\privatedata\myapp.py", line 60, in ?
root = ElementTree(file=infile).getroot()
File "c:\python23\lib\site-packages\elementtree\ElementTree.py", line
554, in __init__
self.parse(file)
File "c:\python23\lib\site-packages\elementtree\ElementTree.py", line
594, in parse
parser.feed(data)
File "c:\python23\lib\site-packages\elementtree\ElementTree.py", line
1171, in feed
self._parser.Parse(data, 0)
xml.parsers.expat.ExpatError: unbound prefix: line 6, column 1


The problem seems to lie with the xlink:href tag. If i remove the xlink
namespace, it then does not produce a traceback.

Thank you for helping
Alain
 
F

Fredrik Lundh

I use Elementtree to parse an elementary SVG file (in fact, it is one
of the examples in the "SVG essentials" book). More precisely, it is
the fig0201.svg file in the second chapter.
The contents of the file are as follows (i hope it will be rendered
correctly):
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="200" height="200">
<title>Default User Coordinates</title>
<desc>Shows a rectangle on a grid in default user coordinates</desc>
<image xlink:href="default_ruler.svg" x="0" y="0" width="200"
height="200"/>
<g transform="translate(40,40)">
<rect x="10" y="10" width="50" height="30"style="stroke: black; fill:
none;"/>
</g>
</svg>
xml.parsers.expat.ExpatError: unbound prefix: line 6, column 1
The problem seems to lie with the xlink:href tag. If i remove the xlink
namespace, it then does not produce a traceback.

so where in that file do you see an xlink declaration?

(elementtree doesn't fetch external DTD:s, so putting in the DTD file
doesn't really help).

adding

xmlns:xlink="http://www.w3.org/1999/xlink"

to the "svg" element should make the problem go away.

</F>
 
A

alainpoint

Fredrik said:
adding

xmlns:xlink="http://www.w3.org/1999/xlink"

to the "svg" element should make the problem go away.


Thanks for the tip. It indeed solves the problem.
Most examples in the book do not include such a declaration and yet are
properly rendered by Internet Explorer.
Is it mandatory and why is it that Expat crashes on it?

Alain
 
R

Richard Brodie

Most examples in the book do not include such a declaration and yet are
properly rendered by Internet Explorer.
Is it mandatory and why is it that Expat crashes on it?

It's not mandatory but it's probably good practice to make the document
self-contained. The xlink prefix is defined in the DTD but Expat, as a
nonvalidating parser, won't fetch it.
 
U

uche.ogbuji

"""
> Most examples in the book do not include such a declaration and yet are
properly rendered by Internet Explorer.
Is it mandatory and why is it that Expat crashes on it?

It's not mandatory but it's probably good practice to make the document
self-contained. The xlink prefix is defined in the DTD but Expat, as a
nonvalidating parser, won't fetch it.
"""

Important clarification:

The decision whether or not to read the external DTD subset is separate
from the decision whether or not to validate. Expat does not validate,
but it does read the external subset, if you tell it to. There are
other uses for reading the external subset, such as entity resolution.
And you can have validation constructs in the internal DTD subset (IOW
right in the XML source file itself), and expat will not do anything
with them because it does not validate.

This may seem a subtle distinction, but it lies behind a lot of user
confusion in practice. The XML WG really should have simplified such
matters (IIRC SGML compatability was a big obstruction to doing so).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top