Skip to content
Advertisement

Extracting enumeration values from an XML Schema with Python

From an XML Schema using xmlschema package, I extracted an XsdEnumerationFacets like the one below

JavaScript

How can I extract the possible values from it? (‘OP1’, ‘OP2’, ‘OP3’, ‘OP3’, ‘OP4’ and so on in this case).

I had one idea to convert it into string (str(enum)), and parse it, but if it’s long, the last elements are not included.

(I have xmlschema==1.9.2 and Python 3.9.)

Example:

schema.xsd is

JavaScript

My code:

JavaScript

That creates me the dictionary:

JavaScript

Instead of "XsdEnumerationFacets(['Black', 'White', 'Green', 'Blue'])" as a value, I would like to have ['Black', 'White', 'Green', 'Blue']. And I don’t want to parse this string. As I mentioned for longer value list the last elements are substituted by ellipses (...), so parsing the string will give me a false or partial result.

Advertisement

Answer

JavaScript

returns: {'Color': ['Black', 'White', 'Green', 'Blue'], 'Size': ['S', 'M', 'L', 'XL']}

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement