xmljson icon indicating copy to clipboard operation
xmljson copied to clipboard

Add namespace support

Open Charnelx opened this issue 9 years ago • 6 comments

Hi.

I tried to add namespace support for BadgerFish converter. The main problem i have faced - support for standard xml library and lxml. Their namespace implementation slightly differs so there is no simple methods to support them both in one way.

Because of a lack of experience my code is messy, sorry.

My implementation stands for one idea - give on the output exact thing that were received on the input.

Now examples of use:

  • simple one; default namespace without prefix.
element = fromstring('<root xmlns="http://dot.com"><joe>string</joe></root>')
dict_obj = xmljson.badgerfish.data(element)

print(dict_obj)
>>> OrderedDict([('root', OrderedDict([('@xmlns', {None: 'http://dot.com'}), ('joe', OrderedDict([('@xmlns', {None: 'http://dot.com'}), ('$', 'string')]))]))])

element = xmljson.badgerfish.etree(dict_obj)
print(tostring(element[0]))
>>> b'<root xmlns="http://dot.com"><joe>string</joe></root>'

print(element[0].nsmap) # lxml only!
>>> {None: 'http://dot.com'}
  • both root and child node have namespace prefix; root node have multiple namespaces
element = fromstring('<abc:root xmlns:abc="http://dot.com" xmlns:xyz="http://com.doc"><xyz:joe>string</xyz:joe></abc:root>')
dict_obj = xmljson.badgerfish.data(element)

print(dict_obj)
>>> OrderedDict([('root', OrderedDict([('@xmlns', {'xyz': 'http://com.doc', 'abc': 'http://dot.com'}), ('joe', OrderedDict([('@xmlns', {'xyz': 'http://com.doc'}), ('$', 'string')]))]))])

element = xmljson.badgerfish.etree(dict_obj)
print(tostring(element[0]))
>>> b'<root xmlns:xyz="http://com.doc" xmlns:abc="http://dot.com"><xyz:joe>string</xyz:joe></root>'

print(element[0].nsmap) # lxml only!
>>> {'xyz': 'http://com.doc', 'abc': 'http://dot.com'}

If you have some questions - ask, will be glad to answer.

Charnelx avatar Oct 03 '16 13:10 Charnelx

@Charnelx -- thanks a lot for this!

I like the approach you're taking for BadgerFish. Do you have any suggestions on how we could do this for other conventions? We don't necessarily have to implement them. But deciding on their specification can help iron-out any inconsistencies in the BadgerFish namespace convention as well.

Also, you may want to re-work the test cases ensure the Travis checks pass.

sanand0 avatar Oct 04 '16 03:10 sanand0

@sanand0 thanks for having time to look at my code!

Do you have any suggestions on how we could do this for other conventions?

Yes i have an idea of how to add support for other convention types but this needs total code refactory. The main problem is still support for both xml and lxml.

Also, you may want to re-work the test cases ensure the Travis checks pass.

Yes, i'll do that. It's little strange because locally tests pass without errors

Charnelx avatar Oct 04 '16 09:10 Charnelx

@Charnelx

So it's just the GData convention that needs namespaces then.

sanand0 avatar Oct 04 '16 11:10 sanand0

@sanand0

Good, i'll look what we can do with GData then.

Charnelx avatar Oct 04 '16 12:10 Charnelx

Has there been any more work done to support namespaces in GData?

fireundubh avatar Dec 29 '17 17:12 fireundubh

@fireundubh -- unfortunately, I haven't received a pull request subsequently. No work is happening on namespaces that I know of

sanand0 avatar Mar 11 '18 06:03 sanand0