Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Element-Mandatory Attribute declaration in XSD Schema:

Tags:

xsd

I want to declare an element to be included in a complex type declaration, and the element has a mandatory attribute: "option=MyOption", but the value of the "option" attribute could be anything, depending on the context.

That is: the attribute "option" with some unknown value should be mandatory in any document using the complex type containing this element.

Example:

    <xs:element name="SpecialOption" type="xs:string"/>

    <xs:complexType name="SpecialOptions">
        <xs:sequence>
            <xs:element ref="SpecialOption" minOccurs="1" maxOccurs="100"/>
            <xs:element ref="XXX"/>     
        </xs:sequence>
    </xs:complexType>   

In this case the "SpecialOption" element in the complex type "SpecialOptions" should have this mandatory attribute.

I don't know how to declare a mandatory attribute for an element in XSD, or how to specify that the attribute must have a value that is not yet known.

like image 288
Vector Avatar asked Oct 07 '11 17:10

Vector


People also ask

How do you make attributes mandatory in XSD?

Attributes are either optional or mandatory (by default they are optional). The "use" property in the XSD definition is used to specify if the attribute is optional or mandatory. To specify that an attribute must be present, use="required" (Note: use may also be set to "prohibited", but we'll come to that later).

Which attributes of schema is mandatory?

That is: the attribute "option" with some unknown value should be mandatory in any document using the complex type containing this element.

How do you define an element attribute in XSD?

An attribute provides extra information within an element. Attributes are defined within an XSD as follows, having name and type properties. An Attribute can appear 0 or 1 times within a given element in the XML document. Attributes are either optional or mandatory (by default the are optional).

What attribute is used to indicate that an attribute is required for an element in the XSD?

Attributes are by default optional. But to make an attribute mandatory, "use" attribute can be used.


2 Answers

You need to modify the definition of the "SpecialOption" element to include the required attribute. Update this code:

<xs:element name="SpecialOption" type="xs:string"/>

to this:

<xs:element name="SpecialOption">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="Option" type="xs:string" use="required"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

With this change your complex type will contain the required "Option" attribute on all instances of the "SpecialOption" element in the "SpecialOptions" complex type. Declaring the "Option" attribute to be of type xs:string will allow any value to be passed in this field.

like image 78
pmartin Avatar answered Sep 28 '22 10:09

pmartin


1) This is a simple required string attribute

<xs:element name="SpecialOption">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="Option" type="xs:string" use="required"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element> 

2) To require exactly one of a list of allowed values:

<xs:element name="SpecialOption">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="Option" use="required">
                    <xs:simpleType>  
                        <xs:restriction base="xs:string">  
                            <xs:enumeration value="DE"/>  
                            <xs:enumeration value="EN"/>  
                        </xs:restriction>  
                    </xs:simpleType>  
                </xs:attribute>  
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element> 

3) One can use a range as a restriction, like in the example below.

<xs:element name="SpecialOption">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="Option" use="required">
                    <xs:simpleType>  
                        <xs:restriction base="xs:integer">  
                            <xs:minInclusive value="95"/>  
                            <xs:maxInclusive value="137"/>  
                        </xs:restriction>  
                    </xs:simpleType>  
                </xs:attribute>  
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element> 

4) Below, the attribute is declared as a list containing decimal values. This allows an attribute to contain a subset of the specified values, e.g. Option="6 77 95".

<xs:simpleType name="Items">  
    <xs:restriction base="xs:decimal">  
        <xs:enumeration value="137"/>  
        <xs:enumeration value="95"/>  
        <xs:enumeration value="6"/>  
        <xs:enumeration value="77"/>  
    </xs:restriction>  
</xs:simpleType>  
<xs:element name="SpecialOption">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="Option" use="required">
                    <xs:simpleType>  
                        <xs:list itemType="Items"/>  
                    </xs:simpleType>  
                </xs:attribute>  
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element> 

5) Here the attribute is declared optional, but provided with a default value ("test"), which is sometimes sufficient:

<xs:element name="SpecialOption">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="Option" type="xs:string" use="optional" default="test"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element> 
like image 27
mousio Avatar answered Sep 28 '22 09:09

mousio