XSD what is the difference between attribute with type and attribute with restriction?

StackOverflow https://stackoverflow.com/questions/19564291

  •  01-07-2022
  •  | 
  •  

Question

I am doing some xsd cleanup activity with limited knowledge on XSD. The file I have contains a complex element with two attribute defined, but differently.

<xs:attribute name="DecisioningRequestType" 
              type="xs:string" 
              use="required"/>

<xs:attribute name="ProcessingRequestType" 
              use="required">
  <xs:simpleType>
    <xs:restriction base="xs:string"/>
  </xs:simpleType>
</xs:attribute>

Through when xml is created, both attribute contain a string value, but I am trying to understand what difference does it make when the attribute are defined with restriction? Isn't that I can define my second attribute similar to the first attribute shown above?

If it is same, I can bring a uniformity in defining the attributes in my XSD file through this cleanup.

Was it helpful?

Solution

Attribute with restriction means that the type of the attribute value is defined inline, directly within the definition of the attribute itself.

That is used when, on one hand, the attribute type is something special (not just a base type) but, on other hand, it is used only for that attribute. So, defining that type as a separate component would be redundant.

But in your case, the construct:

<xs:attribute name="ProcessingRequestType" use="required">
  <xs:simpleType>
    <xs:restriction base="xs:string"/>
  </xs:simpleType>
</xs:attribute>

although is valid, actually doesn't restrict anything (it is an empty restriction). So, it is equivalent to

<xs:attribute name="ProcessingRequestType" type="xs:string" use="required"/>

A true restriction would look something like this:

<xs:attribute name="ProcessingRequestType" use="required">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:enumeration value="typeA"/>
      <xs:enumeration value="typeB"/>
      <xs:enumeration value="typeC"/>
    </xs:restriction>
  </xs:simpleType>
</xs:attribute>

That means that the attribute value is a string, however restricted to be only one from the list: "typeA", "typeB", "typeC".

OTHER TIPS

The declaration of an attribute requires that the type of the attribute be specified; this can be done either with a type attribute giving the name of the type, or with an inline declaration of an anonymous type.

The use or non-use of the XSD restriction element is orthogonal to the difference between a type attribute and a simpleType child. In the case you give, the restriction is vacuous; the inline declaration could just as easily have taken the form

<xs:simpleType>
  <xs:union memberTypes="xs:string"/>
</xs:simpleType>

You write both attribute contain a string value -- this is true enough, as far as it goes, but the two attributes do not have the same type: one is associated with type xs:string and the other with an anonymous type whose value and lexical spaces are the same as those of xs:string (because it was created by a vacuous restriction of xs:string). In some cases, that difference can be important.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top