2

Hope someone can help.

I am using lxml objectify to parse xml, which is returned from a third party integration using objectify.fromstring(). I have one element in my xml which sometimes consists of ints 0-9 only, when it is ints only the leading zero(s) are removed. As there is no set number of how many digits will be returned and the fact sometimes it may have 2 letters before the number, padding the value with zeros wouldn't suffice.

Is there a way I could specify lxml objectify to force the type to a string before doing objectify.fromstring() so it retains the value as received in the xml?

I have had a look at the lxml website but can't seem to find what I am looking for.

Many Thanks

Billy
  • 69
  • 1
  • 9
  • Have you tried making a schema? It would force data types (and, additionally, protect you from potentially harmful, unanticipated inputs) http://lxml.de/objectify.html#asserting-a-schema – Tomalak Mar 11 '16 at 15:14
  • I have not tried that actually. Thanks I will give it a go. Thanks for the link too, I was reading docs for a older version of lxml objectify and that didn't have the schema section. – Billy Mar 11 '16 at 15:52

1 Answers1

1

You might also be experiencing this kind of behavior:

>>> from lxml import objectify
>>> 
>>> xml = "<a><b>01</b></a>"
>>> a = objectify.fromstring(xml)
>>> print(a.b)
1
>>> print(a.b.text)
01

As you can see, if you get .text property you would get the text of the node as is.

FYI, created a follow-up topic: lxml.objectify and leading zeros.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195