Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add attributes to a simpletype or restriction to a complextype in Xml Schema

The problem is as follows:

I have the following XML snippet:

<time format="minutes">11:60</time> 

The problem is that I can't add both the attribute and the restriction at the same time. The attribute format can only have the values minutes, hours and seconds. The time has the restriction pattern \d{2}:\d{2}

<xs:element name="time" type="timeType"/> ... <xs:simpleType name="formatType">     <xs:restriction base="xs:string">         <xs:enumeration value="minutes"/>         <xs:enumeration value="hours"/>         <xs:enumeration value="seconds"/>     </xs:restriction> </xs:simpleType> <xs:complexType name="timeType">     <xs:attribute name="format">         <xs:simpleType>             <xs:restriction base="formatType"/>         </xs:simpleType>     </xs:attribute> </xs:complexType> 

If I make a complex type of timeType, I can add an attribute, but not the restriction, and if I make a simple type, I can add the restriction but not the attribute. Is there any way to get around this problem. This is not a very strange restriction, or is it?

like image 257
Ikke Avatar asked Mar 09 '09 13:03

Ikke


People also ask

What is simpleType and complexType 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.

Which attribute is used to reference an XML schema?

The 'schemaLocation' attribute is used to reference XML Schema(s) that are defined in a target-namespace. The schemaLocation attribute can contain a list of namespaces and schema-locations, separated by white-space.


1 Answers

To add attributes you have to derive by extension, to add facets you have to derive by restriction. Therefore this has to be done in two steps for the element's child content. The attribute can be defined inline:

<xsd:simpleType name="timeValueType">   <xsd:restriction base="xsd:token">     <xsd:pattern value="\d{2}:\d{2}"/>   </xsd:restriction> </xsd:simpleType>  <xsd:complexType name="timeType">   <xsd:simpleContent>     <xsd:extension base="timeValueType">       <xsd:attribute name="format">         <xsd:simpleType>           <xsd:restriction base="xsd:token">             <xsd:enumeration value="seconds"/>             <xsd:enumeration value="minutes"/>             <xsd:enumeration value="hours"/>           </xsd:restriction>         </xsd:simpleType>       </xsd:attribute>     </xsd:extension>   </xsd:simpleContent> </xsd:complexType> 
like image 103
Richard Avatar answered Sep 21 '22 17:09

Richard