title: Extracting enumeration values from an XML Schema with Python
body: <p>From an XML Schema using <code>xmlschema</code> package, I extracted an XsdEnumerationFacets like the one below</p>
  <pre class="lang-py prettyprint-override"><code>XsdEnumerationFacets(['OP1', 'OP2', 'OP3', 'OP3', 'OP4', ...])
  </code></pre>
  <p>How can I extract the possible values from it? ('OP1', 'OP2', 'OP3', 'OP3', 'OP4' and so on in this case).</p>
  <p>I had one idea to convert it into string (<code>str(enum)</code>), and parse it, but if it's long, the last elements are not included.</p>
  <p>(I have xmlschema==1.9.2 and Python 3.9.)</p>
  <p>Example:</p>
  <p><code>schema.xsd</code> is</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot;?&gt;
  &lt;xs:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;
    targetNamespace=&quot;http://www.java2s.com&quot; xmlns=&quot;http://www.java2s.com&quot;
    elementFormDefault=&quot;qualified&quot;&gt;
  
      &lt;xs:element name = &quot;Tshirt&quot;&gt;
         &lt;xs:complexType&gt;
            &lt;xs:sequence&gt;
               &lt;xs:element name = &quot;Color&quot; type = &quot;clothesColorType&quot; /&gt;
               &lt;xs:element name = &quot;Size&quot; type = &quot;clothesSizeType&quot; /&gt;
            &lt;/xs:sequence&gt;
         &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
  
      &lt;xs:simpleType name=&quot;clothesSizeType&quot;&gt;
         &lt;xs:restriction base=&quot;xs:string&quot;&gt;
            &lt;xs:enumeration value=&quot;S&quot; /&gt;
            &lt;xs:enumeration value=&quot;M&quot; /&gt;
            &lt;xs:enumeration value=&quot;L&quot; /&gt;
            &lt;xs:enumeration value=&quot;XL&quot; /&gt;
         &lt;/xs:restriction&gt;
      &lt;/xs:simpleType&gt;
  
      &lt;xs:simpleType name=&quot;clothesColorType&quot;&gt;
         &lt;xs:restriction base=&quot;xs:string&quot;&gt;
            &lt;xs:enumeration value=&quot;Black&quot; /&gt;
            &lt;xs:enumeration value=&quot;White&quot; /&gt;
            &lt;xs:enumeration value=&quot;Green&quot; /&gt;
            &lt;xs:enumeration value=&quot;Blue&quot; /&gt;
         &lt;/xs:restriction&gt;
      &lt;/xs:simpleType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  <p>My code:</p>
  <pre class="lang-py prettyprint-override"><code>import xmlschema
  
  schema = xmlschema.XMLSchema(&quot;schema.xsd&quot;)
  tshirt = schema.elements[&quot;Tshirt&quot;]
  
  enumerate_values = {}
  for c in tshirt.type.content:
      for comp in c.type.iter_components():
          if isinstance(comp, xmlschema.validators.XsdEnumerationFacets):
              enumerate_values[c.name.split(&quot;}&quot;)[1]] = str(comp)
  
  print(enumerate_values)
  </code></pre>
  <p>That creates me the dictionary:</p>
  <pre class="lang-py prettyprint-override"><code>{'Color': &quot;XsdEnumerationFacets(['Black', 'White', 'Green', 'Blue'])&quot;, 'Size': &quot;XsdEnumerationFacets(['S', 'M', 'L', 'XL'])&quot;}
  </code></pre>
  <p>Instead of <code>&quot;XsdEnumerationFacets(['Black', 'White', 'Green', 'Blue'])&quot;</code> as a value, I would like to have  <code>['Black', 'White', 'Green', 'Blue']</code>. And I don't want to parse this string. As I mentioned for longer value list the last elements are substituted by ellipses (<code>...</code>), so parsing the string will give me a false or partial result.</p>