Hervé Cauwelier | 24 Jun 19:26
Picon
Favicon

parsing and serializing XML fragments

Hi, I'm trying to load fragments of XML to inject them in an existing
document tree.

They look like this:

  <table:table table:name="%s" table:style-name="%s"/>

(It's OpenDocument  format.)

Converting the fragment to the "{uri}name" syntax is not an option since
I must remain agnostic to the XML parser.

I would expect the XML() function to take an "nsmap" argument, like the
xpath() method on elements, or parts of the API for subclassing elements.

For now I have another template, a complete document with namespace
declaration, and I inject my fragment using string formatting. Lxml will
parse it and I extract the first child element. (I was using this
technique with the libxml2 Python wrapper and I have seen it in the
lxml.html source code for loading HTML fragments.)

I have looked at custom elements and other resolving methods but lxml
was raising a namespace error before my "print"'s show up.

Another issue is to save the element back to its snippet form, for unit
test validation. Lxml will produce a valid document with namespace
declaration. Either how to serialize without namespace declaration or
how to remove it while keeping prefixes?

Thanks in advance

Gmane