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

2
  • Have you tried making a schema? It would force data types (and, additionally, protect you from potentially harmful, unanticipated inputs) lxml.de/objectify.html#asserting-a-schema Commented Mar 11, 2016 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. Commented Mar 11, 2016 at 15:52

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I will try that, Thank you very much
@Billy there is also an explanation and the possible workaround in the linked thread, check it out.
Thanks will check that out also

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.