3

I've got an xml schema which specifies a default value for an optional field...

<xs:element name="expiry" type="xs:positiveInteger" default="86400"/>

I'm using lxml to parse the incoming xml...

root = lxml.etree.XML(xml)

When I go looking for the optional element, and it's not there, I get a 'None' result...

expiry = root.findtext('expiry')

How would I get the expiry value to default to the schema's default (86400)?

John Mee
  • 50,179
  • 34
  • 152
  • 186

2 Answers2

1

Can't be done by 'magic'. The long way is the only way (today).

That is: read the xmlschema like an xml document, find the element tag with the attribute "expiry", look for the value of the attribute "default" in that tag.

John Mee
  • 50,179
  • 34
  • 152
  • 186
0

First, you need to include you schema, using lxml.etree.XMLSchema

I think it only does validation, but you may want to try to parse your XML together with the schema and see if the "default" value appears.

njzk2
  • 38,969
  • 7
  • 69
  • 107