[lxml-dev] extracting namespace prefix map dict
Hi, Is there a way to extract a namespace prefix map from an etree _Element, i.e.: xmlString= <?xml version="1.0"?> <sciflo xmlns="http://genesis.jpl.nasa.gov/sciflo" xmlns:sfl="http://genesis.jpl.nasa.gov/sciflo" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> ... </sciflo> and get a dict: nsmapDict={'_default': "http://genesis.jpl.nasa.gov/sciflo", 'sfl': "http://genesis.jpl.nasa.gov/sciflo", 'xsd': "http://www.w3.org/2001/XMLSchema", 'xsi': "http://www.w3.org/2001/XMLSchema-instance"} Currently I'm parsing the xml into a minidom and extracting this info. Any help is greatly appreciated. Thanks, Gerald
Gerald John M. Manipon wrote:
Is there a way to extract a namespace prefix map from an etree _Element, i.e.:
xmlString= <?xml version="1.0"?> <sciflo xmlns="http://genesis.jpl.nasa.gov/sciflo" xmlns:sfl="http://genesis.jpl.nasa.gov/sciflo" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
... </sciflo>
and get a dict: nsmapDict={'_default': "http://genesis.jpl.nasa.gov/sciflo", 'sfl': "http://genesis.jpl.nasa.gov/sciflo", 'xsd': "http://www.w3.org/2001/XMLSchema", 'xsi': "http://www.w3.org/2001/XMLSchema-instance"}
Currently I'm parsing the xml into a minidom and extracting this info. Any help is greatly appreciated.
Hi, there isn't an API for that currently. It could be made available, but it actually doesn't fit very well with the intentions of the ElementTree API. ElementTree is not very concerned with prefixes at all, since it deploys James Clark's tag notation ('{namespace}elementname'). Maybe you could tell us what you are actually trying to do with this information? Stefan
I'm just using it to pass into the xpath() method. Stefan Behnel wrote:
Gerald John M. Manipon wrote:
Is there a way to extract a namespace prefix map from an etree _Element, i.e.:
xmlString= <?xml version="1.0"?> <sciflo xmlns="http://genesis.jpl.nasa.gov/sciflo" xmlns:sfl="http://genesis.jpl.nasa.gov/sciflo" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
... </sciflo>
and get a dict: nsmapDict={'_default': "http://genesis.jpl.nasa.gov/sciflo", 'sfl': "http://genesis.jpl.nasa.gov/sciflo", 'xsd': "http://www.w3.org/2001/XMLSchema", 'xsi': "http://www.w3.org/2001/XMLSchema-instance"}
Currently I'm parsing the xml into a minidom and extracting this info. Any help is greatly appreciated.
Hi,
there isn't an API for that currently. It could be made available, but it actually doesn't fit very well with the intentions of the ElementTree API. ElementTree is not very concerned with prefixes at all, since it deploys James Clark's tag notation ('{namespace}elementname').
Maybe you could tell us what you are actually trying to do with this information?
Stefan
Stefan Behnel wrote:
Gerald John M. Manipon wrote:
Is there a way to extract a namespace prefix map from an etree _Element, i.e.:
xmlString= <?xml version="1.0"?> <sciflo xmlns="http://genesis.jpl.nasa.gov/sciflo" xmlns:sfl="http://genesis.jpl.nasa.gov/sciflo" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
... </sciflo>
and get a dict: nsmapDict={'_default': "http://genesis.jpl.nasa.gov/sciflo", 'sfl': "http://genesis.jpl.nasa.gov/sciflo", 'xsd': "http://www.w3.org/2001/XMLSchema", 'xsi': "http://www.w3.org/2001/XMLSchema-instance"}
Currently I'm parsing the xml into a minidom and extracting this info. Any help is greatly appreciated.
there isn't an API for that currently. It could be made available, but it actually doesn't fit very well with the intentions of the ElementTree API. ElementTree is not very concerned with prefixes at all, since it deploys James Clark's tag notation ('{namespace}elementname').
Maybe you could tell us what you are actually trying to do with this information?
Gerald John M. Manipon wrote:
I'm just using it to pass into the xpath() method.
Then don't do it. You only need to define prefixes that appear in your XPath expressions, where you can use whatever prefix you choose as long as you define it in the provided dictionary. That will get better in lxml 0.9 (and already is in the current SVN of the scoder2 branch). In any case, there is no reason for extracting the prefixes used in the XML. Just use new ones. The namespace URI will make the match between them. Stefan
participants (2)
-
Gerald John M. Manipon
-
Stefan Behnel