Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict values for attribute with xsd

Tags:

xml

xsd

I have a xsd file that looks like this:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Configurations">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Schema">
<xs:complexType>
<xs:sequence>
    <xs:element maxOccurs="unbounded" name="Table">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="Key">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Column" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:attribute name="Name" type="xs:string" use="required" />
                                    <xs:attribute name="Value" type="xs:string" use="required" />
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                        <xs:attribute name="Name" type="xs:string" use="required" />
                        <xs:attribute name="Value" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="Name" type="xs:string" use="required" />
        </xs:complexType>
    </xs:element>
</xs:sequence>
<xs:attribute name="Name" type="xs:string" use="required" />
<xs:attribute name="ConnectionString" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>

And I just can't figure out how to create an xs:enumeration for the Name attribute of the Schema element, so that just a few specified values can be used for that attribute. I'm not any good at xsd, a little help would be appreciated:)

like image 502
Pelle Avatar asked May 11 '12 09:05

Pelle


People also ask

Which element in XML Schema can be used to restrict values?

XSD Restrictions/Facets Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.

What is minOccurs and maxOccurs in XSD?

The minOccurs attribute specifies the minimum number of times that the element can occur. It can have a value of 0 or any positive integer. The maxOccurs attribute specifies the maximum number of times that the element can occur.

What is complexType and simpleType in XSD?

XSD elements can be of type simpleType , complexType , or anyType . An element of type simpleType contains only text. It cannot have attributes and elements. An element of type complexType can contain text, elements, and attributes.

What is fixed attribute in XSD?

Fixed means the value in the XML document can only have the value specified in the XSD.


1 Answers

If you want to reuse the restricted type for all your Name attributes, add a simpleType at the root level:

<xs:simpleType name="Name_type">
    <xs:restriction base="xs:string">
        <xs:enumeration value="Foo" />
        <xs:enumeration value="Bar" />
        <xs:enumeration value="Baz" />
    </xs:restriction>
</xs:simpleType>

then reference it as the type of your Name attributes:

<xs:attribute name="Name" type="Name_type" use="required" />
like image 145
Filburt Avatar answered Oct 13 '22 00:10

Filburt