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" 
  xsi:noNamespaceSchemaLocation="n1.xsd">  
  <state code="12345">TX</state>
  <state code="34651">CO</state>
  <state code="41562">FL</state>
  <state code="28309">NJ</state>
</root>
XML Schema Source
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  elementFormDefault="qualified"
  attributeFormDefault="unqualified">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="state" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

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

    </xs:complexType>
  </xs:element>

  <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" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact