Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a user define data type in XML schema?

Tags:

xml

xsd

I have defined two complex element types - Developer and App.

Developer childs - ID ,Name, Email

App childs - ID, Name, Developer

Here the Developer in App complex element refers to Developer/ID.

How to define this relationship in xml schema. I am using XML spy2013 editor.

I have tried specifying name in the declaration of simple type Developer->ID. And using this name in App->Developer datatype. But it gives error..

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2013 (x64) (http://www.altova.com) by Piyush (USC) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"          attributeFormDefault="unqualified">
<xs:element name="appinfo">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="Developer">
                <xs:complexType>
                    <xs:all>
                        **<xs:element name="ID">**
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:length value="5"/>
                                    <xs:pattern value="[a-zA-Z][a-zA-Z][0-9][0-9][a-zA-Z]"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="Name">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:pattern value="([a-zA-Z])+"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="Email">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:pattern value="[^@]+@[^\.]+\..+"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="Company" type="xs:string"/>
                        <xs:element name="Website" type="xs:string"/>
                        <xs:element name="Phone">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:length value="13"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                    </xs:all>
                </xs:complexType>
            </xs:element>
            <xs:element name="App">
                <xs:complexType>
                    <xs:all>
                        <xs:element name="ID">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:length value="5"/>
                                    <xs:pattern value="[0-9][0-9][0-9][0-9][A-Z]"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="Name">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:pattern value="([a-zA-Z])+"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="Developer" ***type="xs:anySimpleType"/>***
                        <xs:element name="Price">
                            <xs:simpleType>
                                <xs:restriction base="xs:float">
                                    <xs:minInclusive value="0.00"/>
                                    <xs:maxInclusive value="6.99"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="TabletCompatible" type="xs:boolean" minOccurs="0"/>
                        <xs:element name="Category">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:enumeration value="Travel"/>
                                    <xs:enumeration value="Productivity"/>
                                    <xs:enumeration value="Game"/>
                                    <xs:enumeration value="Music"/>
                                    <xs:enumeration value="Education"/>
                                    <xs:enumeration value="Lifestyle"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="Platform">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:enumeration value="Android"/>
                                    <xs:enumeration value="iOS"/>
                                    <xs:enumeration value="Blackberry"/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                    </xs:all>
                </xs:complexType>
            </xs:element>
            <xs:element name="AppStat">
                <xs:complexType>
                    <xs:all>
                        <xs:element name="AppID" type="xs:anySimpleType"/>
                        <xs:element name="Statistics">
                            <xs:complexType>
                                <xs:sequence maxOccurs="unbounded">
                                    <xs:element name="Platform" type="xs:anySimpleType"/>
                                    <xs:element name="Downloads" type="xs:positiveInteger"/>
                                    <xs:element name="Rating">
                                        <xs:simpleType>
                                            <xs:restriction base="xs:float">
                                                <xs:minInclusive value="0.0"/>
                                                <xs:maxInclusive value="5.0"/>
                                            </xs:restriction>
                                        </xs:simpleType>
                                    </xs:element>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                        <xs:element name="LastChecked" type="xs:date"/>
                    </xs:all>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

like image 703
Leo03 Avatar asked Nov 16 '12 05:11

Leo03


People also ask

How do you define datatype in XML?

A data type within an XML document is a type that has been assigned to an element on the instance using the dt:dt attribute, or through an XML Schema, a formal definition of an XML document. In addition, data types can be declared as elements. The XML parser uses the data type information to validate the document.

How can we define within an XML schema?

XSD Example The purpose of an XML Schema is to define the legal building blocks of an XML document: the elements and attributes that can appear in a document. the number of (and order of) child elements. data types for elements and attributes. default and fixed values for elements and attributes.

Does an XML schema uses predefined types?

W3C XML Schema provides an extensive set of predefined datatypes. W3C XML Schema derives many of these predefined datatypes from a smaller set of “primitive” datatypes that have a specific meaning and semantic and cannot be derived from other types.


1 Answers

The way to use a common simple type for the developer id is to declare it as a named type at the beginning:

<xs:schema 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  elementFormDefault="qualified"          
  attributeFormDefault="unqualified">

  <xs:simpleType name="developerID">
    <xs:restriction base="xs:string">
      <xs:length value="5"/>
      <xs:pattern value="[a-zA-Z][a-zA-Z][0-9][0-9][a-zA-Z]"/>
    </xs:restriction>
  </xs:simpleType>

  . . . 

and then use it:

 . . . 
    <xs:element name="Developer" maxOccurs="unbounded">
      <xs:complexType>
        <xs:all>
          <xs:element name="ID" type="developerID">
 . . . 
    <xs:element name="App" maxOccurs="unbounded">
      <xs:complexType>
        <xs:all>
          . . .
          <xs:element name="Developer" type="developerID"/>

But this is not enough to create a contraint so that appinfo/App/Developer can contain only one of the developers' ids declared in appinfo/Developer/ID. To do that is necessary to create a unique key definition using xs:key and reference it using xs:keyref (see here).

Here is the complete XSD:

<xs:schema 
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  elementFormDefault="qualified"          
  attributeFormDefault="unqualified">

  <xs:simpleType name="developerID">
    <xs:restriction base="xs:string">
      <xs:length value="5"/>
      <xs:pattern value="[a-zA-Z][a-zA-Z][0-9][0-9][a-zA-Z]"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:element name="appinfo">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Developer" maxOccurs="unbounded">
          <xs:complexType>
            <xs:all>
              <xs:element name="ID" type="developerID">
              </xs:element>
              <xs:element name="Name">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:pattern value="([a-zA-Z])+"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="Email">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:pattern value="[^@]+@[^\.]+\..+"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="Company" type="xs:string"/>
              <xs:element name="Website" type="xs:string"/>
              <xs:element name="Phone">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:length value="13"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:all>
          </xs:complexType>
        </xs:element>
        <xs:element name="App" maxOccurs="unbounded">
          <xs:complexType>
            <xs:all>
              <xs:element name="ID">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:length value="5"/>
                    <xs:pattern value="[0-9][0-9][0-9][0-9][A-Z]"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="Name">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:pattern value="([a-zA-Z])+"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="Developer" type="developerID"/>
              <xs:element name="Price">
                <xs:simpleType>
                  <xs:restriction base="xs:float">
                    <xs:minInclusive value="0.00"/>
                    <xs:maxInclusive value="6.99"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="TabletCompatible" type="xs:boolean" minOccurs="0"/>
              <xs:element name="Category">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:enumeration value="Travel"/>
                    <xs:enumeration value="Productivity"/>
                    <xs:enumeration value="Game"/>
                    <xs:enumeration value="Music"/>
                    <xs:enumeration value="Education"/>
                    <xs:enumeration value="Lifestyle"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="Platform">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:enumeration value="Android"/>
                    <xs:enumeration value="iOS"/>
                    <xs:enumeration value="Blackberry"/>
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:all>
          </xs:complexType>
          <xs:keyref name="developerIDref" refer="developerID">
            <xs:selector xpath="."/>
            <xs:field xpath="Developer"/>
          </xs:keyref>
        </xs:element>
        <xs:element name="AppStat">
          <xs:complexType>
            <xs:all>
              <xs:element name="AppID" type="xs:anySimpleType"/>
              <xs:element name="Statistics">
                <xs:complexType>
                  <xs:sequence maxOccurs="unbounded">
                    <xs:element name="Platform" type="xs:anySimpleType"/>
                    <xs:element name="Downloads" type="xs:positiveInteger"/>
                    <xs:element name="Rating">
                      <xs:simpleType>
                        <xs:restriction base="xs:float">
                          <xs:minInclusive value="0.0"/>
                          <xs:maxInclusive value="5.0"/>
                        </xs:restriction>
                      </xs:simpleType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="LastChecked" type="xs:date"/>
            </xs:all>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    <xs:key name="developerID">
      <xs:selector xpath="Developer"/>
      <xs:field xpath="ID"/>
    </xs:key>
  </xs:element>

</xs:schema>
like image 97
MiMo Avatar answered Oct 02 '22 16:10

MiMo