user3248822
user3248822

Reputation: 259

XSD attributes specifying

I need to create .xsd schema, and specify attribute. How I can do that?

<?xml version="1.0" encoding="UTF-8"?>
<ConfigData>
   <GlobalSettings>    
      <Logging param="Off"/> <-- param can be only in 'On' or 'Off' statement -->
   </GlobalSettings>
</ConfigData>

I created something like this, but it doesn`t work

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="ConfigData">
    <xs:complexType>
        <xs:element name="GlobalSettings">
          <xs:complexType>            
            <xs:simpleType name="statement">
                <xs:restriction base="xs:string">
                    <xs:enumeration value="On"/>
                    <xs:enumeration value="Off"/>
                </xs:restriction>
            </xs:simpleType>
            <xs:element name="Logging">
                <xs:complexType>
                    <xs:extension base="xs:string">
                        <xs:attribute name="param" type="statement" use="required"/>
                    </xs:extension>
                </xs:complexType>
            </xs:element>
          </xs:complexType>
        </xs:element>
    </xs:complexType>
  </xs:element>
</xs:schema>

Upvotes: 0

Views: 110

Answers (2)

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31269

If you define a new simpleType and you want to refer to it from another location, you need to define it at the top level, directly within an <xs:schema> element.

Your schema has other problems beyond the simpleType: you cannot define another <xs:element> directly under <xs:complexType>, rather you first need to say whether you want an <xs:sequence>, <xs:choice> or <xs:all>. In your case, the result is the same since you only have one <GlobalSettings> element under <ConfigData>, and one <Logging> element under GlobalSettings. So let's pick <xs:sequence>.

Your schema then becomes:

<xs:simpleType name="onOffType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="On" />
        <xs:enumeration value="Off" />
    </xs:restriction>
</xs:simpleType>

<xs:element name="ConfigData">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="GlobalSettings">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="Logging">
                            <xs:complexType>
                                <xs:attribute name="param" type="onOffType" use="required" />
                            </xs:complexType>
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

This validates your input file and raises a validation error if the value of the param attribute on your <Logging> element is anything other than On or Off.

Upvotes: 0

Naren
Naren

Reputation: 1477

<price currency="euros">20000.00</price>

restrict the currency attribute to one the following:

euros
pounds
dollars


<xs:simpleType name="curr">
  <xs:restriction base="xs:string">
    <xs:enumeration value="pounds" />
    <xs:enumeration value="euros" />
    <xs:enumeration value="dollars" />
  </xs:restriction>
</xs:simpleType>



<xs:element name="price">
        <xs:complexType>
            <xs:extension base="xs:decimal">
              <xs:attribute name="currency" type="curr"/>
            </xs:extension>
        </xs:complexType>
</xs:element>

Upvotes: 1

Related Questions