By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

request for advice - possible ElementTree nexus

P: n/a
Situation is this:
1) I have inherited some python code that accepts a string object, the
contents of which is an XML document, and produces a data structure
that represents some of the content of the XML document
2) The inherited code is somewhat 'brittle' in that some well-formed
XML documents are not correctly processed by the code; the brittleness
is caused by how the parser portion of the code handles whitespace.
3) I would like to change the code to make it less brittle. Whatever
changes I make must continue to produce the same data structure that is
currently being produced.
4) Rather than attempt to fix the parser portion of the code, I would
prefer to use ElementTree. ElementTree handles parsing XML documents
flawlessly, so the brittle portion of the code goes away. In addition,
the ElementTree model is very sweet to work with, so it is a relatively
easy task using the information in ElementTree to produce the same data
structure that is currently being produced.
5) The existing data structure--the structure that must be
maintained--that gets produced does NOT include any {xmlns=<whatever>}
information that may appear in the source XML document.
6) Based on a review of several posts in this group, I understand why
ElementTree hanldes xmlns=<whateverinformation the way it does. This
is an oversimplification, but one of the things it does is to
incorporate the {whatever} within the tag property of the element and
of any descendent elements.
7) One of the pieces of information in the data structure that gets
produced by this code is the tag...the tag in the data structure should
not have any xmlns=<whateverinformation.

So, given that the goal is to produce the same data structure and given
that I really want to use ElementTree, I need to find a way to remove
the xmlns=<whateverinformation. It seems like there are 2 general
methods for accomplishing this:
1) before feeding the string object to the ElementTree.XML() method,
remove the xmlns=<whateverinformation from the string.
2) keep the xmlns=<whateverinformation in the string that feeds
ElementTree.XML(), but when building the data structure, ensure that
the {whatever} information in the tag property of the element should
NOT be included in the data structure.

My requests for advice are:
a) What are the pros/cons of each of the 2 general methods described
above?
b) If I want to remove the xmlns information before feeding it to the
ElementTree.XML() method, and I don't want to be aware of what is to
the right of the equal sign, what is the best way to remove all the
substrings that are of the form xmlns=<whatever>? Would this require
learning the nuances of regular expressions?
c) If I want to leave the xmlns information in the string that gets fed
to ElementTree.XML, and I want to remove the {whatever} from the tag
before building the data structure, what is the best way to find
{whatever} from the tag property...is this another case where one
should be using regular expressions?

Jul 4 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

mi************@yahoo.com wrote:
Situation is this:
1) I have inherited some python code that accepts a string object, the
contents of which is an XML document, and produces a data structure
that represents some of the content of the XML document
2) The inherited code is somewhat 'brittle' in that some well-formed
XML documents are not correctly processed by the code; the brittleness
is caused by how the parser portion of the code handles whitespace.
3) I would like to change the code to make it less brittle. Whatever
changes I make must continue to produce the same data structure that is
currently being produced.
4) Rather than attempt to fix the parser portion of the code, I would
prefer to use ElementTree. ElementTree handles parsing XML documents
flawlessly, so the brittle portion of the code goes away. In addition,
the ElementTree model is very sweet to work with, so it is a relatively
easy task using the information in ElementTree to produce the same data
structure that is currently being produced.
5) The existing data structure--the structure that must be
maintained--that gets produced does NOT include any {xmlns=<whatever>}
information that may appear in the source XML document.
6) Based on a review of several posts in this group, I understand why
ElementTree hanldes xmlns=<whateverinformation the way it does. This
is an oversimplification, but one of the things it does is to
incorporate the {whatever} within the tag property of the element and
of any descendent elements.
7) One of the pieces of information in the data structure that gets
produced by this code is the tag...the tag in the data structure should
not have any xmlns=<whateverinformation.

So, given that the goal is to produce the same data structure and given
that I really want to use ElementTree, I need to find a way to remove
the xmlns=<whateverinformation. It seems like there are 2 general
methods for accomplishing this:
1) before feeding the string object to the ElementTree.XML() method,
remove the xmlns=<whateverinformation from the string.
2) keep the xmlns=<whateverinformation in the string that feeds
ElementTree.XML(), but when building the data structure, ensure that
the {whatever} information in the tag property of the element should
NOT be included in the data structure.
[snip]

maybe transform the document with XSLT before processing?

google: xslt remove namespaces

eg. http://www.tei-c.org/wiki/index.php/...Namespaces.xsl

eg. http://www.thescripts.com/forum/thread86057.html

hth

Gerard

Jul 5 '06 #2

P: n/a
mi************@yahoo.com wrote:
c) If I want to leave the xmlns information in the string that gets fed
to ElementTree.XML, and I want to remove the {whatever} from the tag
before building the data structure, what is the best way to find
{whatever} from the tag property...is this another case where one
should be using regular expressions?
if the "whatever" in {whatever} is known in advance, you can use the
approach described here:

http://effbot.org/zone/element-tidyl...-xhtml-to-html

if the "whatever" is not known, you can do e.g.

if elem.tag.startswith("{"):
elem.tag = elem.tag.split("}")[1]

</F>

Jul 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.