[lxml-dev] Entity References resolution
Hi everybody, today I tried this: parser = etree.XMLParser(resolve_entities=False) tree = etree.parse(StringIO("<aRoot>&aReference;</aRoot>"), parser) I was expecting it to simply treat the entity reference as text but it raises an error. What is the parser's parameter resolve_entities for then? Manu
Hi, I don't know why but resolve_entities=False works for me only when DOCTYPE declaration is present. For your case will work: tree = etree.parse(StringIO('<!DOCTYPE aRoot SYSTEM "some_dtd_link"><aRoot>&aReference;</aRoot>'), parser) Just be careful because unparsed entities are not just parts of element's text, they are this element's child objects. This causes problems with attributes (I don't know if this is a bug or feature) because attributes are strings and can't contain objects inside. By this I mean the following. If you have xml like: <aRoot>bla &aReference; foo</aRoot> You'll get:
print tree.getroot().text bla print tree.getroot()[0] &aReference; print tree.getroot()[0].tail foo
But when you have xml like: <aRoot><aChild attr="bla &aReference; foo" /></aRoot> You'll get two child elements inside aRoot:
print tree.getroot()[0] &aReference; print tree.getroot()[1] <Element aChild at c35e10> print tree.getroot()[1].get('attr') bla foo
So, parser prepends entity object before child node. That doesn't look nice... -- Marat On Tue, Nov 10, 2009 at 2:08 AM, Emanuele D'Arrigo <manu3d@gmail.com> wrote:
Hi everybody,
today I tried this:
parser = etree.XMLParser(resolve_entities=False) tree = etree.parse(StringIO("<aRoot>&aReference;</aRoot>"), parser)
I was expecting it to simply treat the entity reference as text but it raises an error. What is the parser's parameter resolve_entities for then?
Manu
_______________________________________________ lxml-dev mailing list lxml-dev@codespeak.net http://codespeak.net/mailman/listinfo/lxml-dev
Marat Dakota, 10.11.2009 07:29:
I don't know why but resolve_entities=False works for me only when DOCTYPE declaration is present. For your case will work:
tree = etree.parse(StringIO('<!DOCTYPE aRoot SYSTEM "some_dtd_link"><aRoot>&aReference;</aRoot>'), parser)
Undeclared entities are an error for XML parsers, regardless if you want to keep them in the tree or resolve them. BTW, using etree.fromstring() instead of etree.parse(StringIO()) is both more readable and more efficient.
Just be careful because unparsed entities are not just parts of element's text, they are this element's child objects.
This is a feature.
This causes problems with attributes (I don't know if this is a bug or feature)
Certainly not a feature.
when you have xml like:
<aRoot><aChild attr="bla &aReference; foo" /></aRoot>
You'll get two child elements inside aRoot:
print tree.getroot()[0] &aReference; print tree.getroot()[1] <Element aChild at c35e10> print tree.getroot()[1].get('attr') bla foo
So, parser prepends entity object before child node.
I doubt that it does that. However, this looks like something that needs better handling. Could you file a bug report? Thanks, Stefan
participants (3)
-
Emanuele D'Arrigo -
Marat Dakota -
Stefan Behnel