Element-Mandatory Attribute declaration in XSD Schema:

半腔热情 提交于 2020-01-01 08:20:09

问题


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.


回答1:


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.




回答2:


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> 



回答3:


To mark an attribute as mandatory you use <xs:attribute use="required" />.

As for type, you have a choice of the built-in XSD types (xs:string etc), or you can define your own <xs:simpleType /> and use that.

UPDATE

I am not certain what you mean by the attribute must have a value that is not yet known. Does this mean that the value is a string, but can be any string? Or a decimal?

Because it's an attribute value we are talking about you are restricted to using the built-in XSD types, or defining your own xs:simpleType type based on one of the built-in types. This is where you can apply more stringent rules to the allowed value, for example by extending xs:string and adding a regular expression constraint to allowed values.

<xsd:simpleType name="UKDate">
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)\d\d"/>
    </xsd:restriction>
</xsd:simpleType>

However, if there is absolutely no way of knowing what value will be used then you have the well known temporal paradox whereby you cannot restrict something at design-time to a value you only know at run-time. In this instance, surely it is only necessary to specify that the attribute must at least be present?

<xs:attribute use="required" />

Hope this answers your question a little more clearly.




回答4:


Simply you can do it as the following

<xs:element name="SpecialOption">
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:whiteSpace value="replace"/>
          <xs:minLength value="1"></xs:minLength>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>

by this code you enforce to insert a value on the xml tag and also the white space restriction will handle to remove the white space from the xml tag.



来源:https://stackoverflow.com/questions/7690949/element-mandatory-attribute-declaration-in-xsd-schema

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!