XSDs, Attributes, and Enumeration. I've built XML schemas before, and created elements that had enumerations or attributes, but never both.

Roseanne Zhang

This is a good question! When you try to enumerate base type string, you need restriction. However, you want to add attributes to a simpleContent, you need extension. This took me sometime to figure the answer out. It worthed the effort anyway, since I learned a lot from the process. Here is the full xml schema solution for the given XML, validated. The key is to separate restriction and extension.

XML Source

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  <state code="12345">TX</state>
  <state code="34651">CO</state>
  <state code="41562">FL</state>
  <state code="28309">NJ</state>
XML Schema Source
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  <xs:element name="root">
        <xs:element ref="state" maxOccurs="unbounded" />

  <xs:element name="state" >
        <xs:extension base="enum" >
          <xs:attribute name="code" type="xs:integer" />


  <xs:simpleType name="enum">
    <xs:restriction base="xs:string">
      <xs:enumeration value="NJ" />
      <xs:enumeration value="TX" />
      <xs:enumeration value="FL" />
      <xs:enumeration value="CO" />
About | Sitemap | Contact