Element-Mandatory Attribute declaration in XSD Schema:

后端 未结 4 1571
故里飘歌
故里飘歌 2021-02-19 15:46

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\

相关标签:
4条回答
  • 2021-02-19 16:33

    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.

    0 讨论(0)
  • 2021-02-19 16:45

    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> 
    
    0 讨论(0)
  • 2021-02-19 16:45

    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.

    0 讨论(0)
  • 2021-02-19 16:53

    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.

    0 讨论(0)
提交回复
热议问题