martinblech / xmltodict

Python module that makes working with XML feel like you are working with JSON
MIT License
5.46k stars 465 forks source link

Feature request: Namespace support #32

Closed thom-nic closed 11 years ago

thom-nic commented 11 years ago

This project is great! Does it support namespaces? I don't see anything in the examples or tests.

Unfortunately I have to deal with both building and parsing a lot of web-servicey XML where you have a half dozen or more namespaces in a given document. LXML is the only Python framework I've found that sufficiently deals with this issue, by way of the namespaces parameter so you don't have to qualify each element with the full namespace URI.

I'd like to request an additional optional parameter to xmltodict.parse():

namespaces = {
    'one' : 'http://one.com',
    'two' : 'http://two.com }

doc = xmltodict.parse("""
 <mydocument 
        xmlns='http://one.com'
        xmlns:two='http://two.com' 
        xmlns:three='http://three.com'
        has="an attribute">
   <two:and>
     <many>elements</many>
     <many>more elements</many>
   </two:and>
   <plus three:a="complex">
     element as well
   </plus>
 </mydocument>""", namespaces=namespaces)

doc['one:mydocument']['two:and']

doc['one:mydocument']['one:plus']['@three:a']
martinblech commented 11 years ago

I certainly didn't start the project with namespaces in mind, as you probably guessed from their complete absence in the examples and tests.

Your use case doesn't seem very complicated. I will have a look at it and see how much pyexpat can help me in this direction. I would love to add this feature if it doesn't bloat xmltodict for the simplest use cases.

Thanks for your input!

thom-nic commented 11 years ago

I figured as much, but thanks for considering it! Hopefully it will not be too complicated.

martinblech commented 11 years ago

@tomstrummer What's exactly the role of the namespaces dict argument in your example? I see that it has entries for one='http://one.com' and two='http://two.com', but three is missing, but as far as I can see, one, two and three are treated equally in your example's output.

What difference does it make for a namespace to be present or missing in namespaces?

thom-nic commented 11 years ago

Hah oops that was just me messing up the example I made up. three should have been in the namespace map as well :) On Aug 24, 2013 12:30 PM, "Martín Blech" notifications@github.com wrote:

@tomstrummer https://github.com/tomstrummer What's exactly the role of the namespaces dict argument in your example? I see that it has entries for one='http://one.com' and two='http://two.com ', but three is missing, but as far as I can see, one, two and three are treated equally in your example's output.

What difference does it make for a namespace to be present or missing in namespaces?

— Reply to this email directly or view it on GitHubhttps://github.com/martinblech/xmltodict/issues/32#issuecomment-23211791 .

martinblech commented 11 years ago

@tomstrummer Ok, it should have been there. But what if it wasn't? What would be a reasonable default behavior for xmltodict in case it bumps into a namespace that is not among the values of the namespaces dict? Should it raise an exception?

I'm just trying to figure out what's the purpose of the namespaces dict in your example.

martinblech commented 11 years ago

Fixed with release 0.7.0