Srini Srini - 7 days ago 4
C# Question

Using xs:extension and xs:restriction in a XSD

I wanted to use both

xs:extensions
and
xs:restriction
together in my XSD for the below mentioned XML

<?xml version="1.0" encoding="utf-8"?>
<records>
<row id="1">
<record>
<Value class="field">486.89</Value>
</record>
<record>
<Value class="field">811.49</Value>
</record>
</row>
<row id="2">
<record>
<Value class="field">123.45</Value>
</record>
<record>
<Value class="field">678.91</Value>
</record>
</row>
</records>


Below is the XSD im trying :

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="records">
<xs:complexType>
<xs:sequence>
<xs:element name="row">
<xs:complexType>
<xs:sequence>
<xs:element name="record" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="Value">
<xs:simpleType>
<xs:restriction base="xs:decimal">
<xs:totalDigits value="12"/>
<xs:fractionDigits value="2" />
</xs:restriction>
</xs:simpleType>
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute type="xs:string" name="class" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:byte" name="id" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


The above XSD is not working ,which is the solution I found here Using xs:extension & xs:restriction together?. It's throwing an error 'complexType' element already exists in the content model.

Answer

You must globally define and name the restricted xs:simpleType that you wish to extend, not define it locally under Value.

Note also that you need maxOccurs="unbounded" (or something greater than the default of 1).

Altogether, this XSD will validate your XML successfully:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" 
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:simpleType name="Decimal12.2">
    <xs:restriction base="xs:decimal">
      <xs:totalDigits value="12"/>
      <xs:fractionDigits value="2" />
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="records">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="row" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="record" maxOccurs="unbounded"
                          minOccurs="0">
                <xs:complexType>
                  <xs:sequence>                    
                    <xs:element name="Value">
                      <xs:complexType>
                        <xs:simpleContent>
                          <xs:extension base="Decimal12.2">
                            <xs:attribute type="xs:string"
                                          name="class" 
                                          use="optional"/>
                          </xs:extension>
                        </xs:simpleContent>
                      </xs:complexType>
                    </xs:element>                    
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute type="xs:byte" name="id" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>    
  </xs:element>
</xs:schema>