laika222
11/11/2017 - 7:56 PM

XML Schema

<!-- XML Schema - set of rules describing an XML document. XML schema language is called XML Schema Definition (XSD). XML schemas provide validation contraints, data type information, and information about types of attributes and elements that are applied to an XML document.-->

<!-- example of an XML schema -->
<?xml version="1.0" encoding="utf-8"?>
<xs:schema	targetNamespace="http://aw/orderschema"
		xmlns="http://aw/orderschema"
		xmlns:xs="http://www.w3.org/2001/XMLSchema"
		elementFormDefault="qualified">

  <xs:complexType name="person">
    <xs:sequence>
      <xs:element name="name" type="xs"string"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:positiveInteger" use="required"/>
  </xs:complexType>

  <xs:complexType name="itemlist">
    <xs:sequence>
      <xs:element name="item" minOccurs="1" maxOccurs="unbounded">
        <xs:complexType>
          <xs:attribute name="id" type="xs:positiveInteger"/>
          <xs:attribute name=Qauntity" type="xs:positiveInteger"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="salesperson" type="person" minOccurs="1" maxOccurs="1"/>
        <xs:element name="customer" type="person" minOccurs="1" maxOccurs="1"/>
        <xs:element name="items" itemlist minOccurs="1" maxOccurs="1"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:positiveInteger" use="required"/>
      <xs:attribute name="date" type="xs:date" use="optional"/>
    </xs:complexType>
  </xs:element>

</xs:schema>

<!-------------------------------->
<!-- DETAIL OF THE SCHEMA ABOVE -->
<!-------------------------------->

<?xml version="1.0" encoding="utf-8"?>
<!-- creates schema root element, placed in the namespace xs, which refers to the standard w3.org namespace for schema. Allows any parser to know that this is an XML schema. Also gives it a targetNamespace="http://aw/orderschema" - the rules applied in this schema should be applied to any XML documents that use this targetNamespace.  -->
<xs:schema	targetNamespace="http://aw/orderschema"
		<!-- makes the targetNamespace the default nameSpace for the schema-->
		xmlns="http://aw/orderschema"
		xmlns:xs="http://www.w3.org/2001/XMLSchema"
		<!-- can let people validate an XML document with any namespace, or insist that they only validate documents with the same namespace as the targetNamespace="http://aw/orderschema" -->
		elementFormDefault="qualified">
  
  <!-- creates element person, which is a complexType -->
  <xs:complexType name="person">
    <!-- sequence shows the sequence of elements within person -->
    <xs:sequence>
      <xs:element name="name" type="xs"string"/>
    </xs:sequence>
    <!-- after sequence you can determine attributes for the element -->
    <xs:attribute name="id" type="xs:positiveInteger" use="required"/>
  </xs:complexType>

  <xs:complexType name="itemlist">
    <xs:sequence>
      <xs:element name="item" minOccurs="1" maxOccurs="unbounded">
        <!-- the item element created above is itself a complexType, as definied here -->
        <xs:complexType>
          <xs:attribute name="id" type="xs:positiveInteger"/>
          <xs:attribute name=Qauntity" type="xs:positiveInteger"/>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="order">
    <xs:complexType>
      <xs:sequence>
        <!-- order elemnet will have a child element salesperson of the type person, which is the first element we declared above. Therefore, each salesperson will also have a name element as definied in the person declaration above -->
        <xs:element name="salesperson" type="person" minOccurs="1" maxOccurs="1"/>
        <xs:element name="customer" type="person" minOccurs="1" maxOccurs="1"/>
        <!-- order will have a child element items, which is of the type itemlist, meaning as defined above it'll have one or more items and will have an id and quantity attribute -->
        <xs:element name="items" itemlist minOccurs="1" maxOccurs="1"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:positiveInteger" use="required"/>
      <xs:attribute name="date" type="xs:date" use="optional"/>
    </xs:complexType>
  </xs:element>

</xs:schema>

<!-- example of an XML document based on the schema created above. -->

<?xml version="1.0" encoding="iso-8859-1"?>
<order xmlns="http://aw/orderschema" id="123456" date="2015-01-01">
  <salesperson id="123">
    <name>Naomi Sharp</name>
  </salesperson>
  <customer id="921">
    <name>Dan Drayton</name>
  </customer>
  <items>
    <item id="561" quantity="1"/>
    <item id="127" quantity="2"/>
  </items>
</order>

<!-- more complicated version of a schema showing more of the things a schema can implement -->

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:annotation>
    <xsd:documentation>
      Schema regarding the borrowing of paintings.
    </xsd:documentation>
  </xsd:annotation>
  
  <xsd:element name="transaction type="transactionType"/>
  
  <xsd:complexType name="transactionType>
    <xsd:sequence>
      <xsd:element name="Lender" type="address"/>
      <xsd:element name="Borrower" type="address"/>
      <xsd:element ref="note" minOccurs="0"/>
      <xsd:element name="paintings" type="paintings"/>
    </xsd:sequence>
    
  <xsd:element name="note" type="xsd:string"/>
  
  <xsd:complexType name="address"/>
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string"/>
      <xsd:element name="street" type="xsd:string"/>
      <xsd:element name="city" type="xsd:string"/>
      <xsd:element name="state" type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="phone" type="xsd:string" use="optional"/>
  </xsd:complexType>
  
  <xsd:complexType name="paintings">
    <xsd:sequence>
      <xsd:element name="painting" minOccurs="0" maxOccurs="10">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="paintingTitle" type="xsd:string"/>
            <xsd:element name="creationDate" type="xsd:date" minOccurs="0"/>
            <xsd:element name="replacementValue" type="xsd:decimal"/>
            <xsd:element name="maxDaysOut">
              <xsd:simpleType>
                <xsd:restriction base="xsd:integer">
                  <xsd:maxInclusive value="14"/>
                </xsd:restriction>
              </xsd:simpleType>
            </xsd:element>
          </xsd:sequence>
          <xsd:attribute name="paintingID" type="catalogID"/>
        </xsd:complexType> 
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
  
  <xsd:simpleType name="catalogID">
    <xsd: restriction base="xsd:string">
      <xsd:pattern value="\d{3} - \d{4} - \d{3}"/>
    </xsd:restriction>
  </xsd:simpleType>
  
</xsd:schema>

<!-------------------------------->
<!-- DETAIL OF THE SCHEMA ABOVE -->
<!-------------------------------->

<?xml version="1.0"?>
<!-- sets the namespace to standard schema namespace xsd -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <!-- strts annotation, or overall note -->
  <xsd:annotation>
    <xsd:documentation>
      Schema regarding the borrowing of paintings.
    </xsd:documentation>
  </xsd:annotation>
  
  <!-- declares element transaction which will be a complexType (will contain child elements and/or attributes. You first have to decalre the element and give it a type, and then you have to create a complexType outlining the child elements/attributes and tie it back to the element you declared previously -->
  <xsd:element name="transaction type="transactionType"/>
  
  <!-- outlines the transactionType delclared earlier as a complexType -->
  <xsd:complexType name="transactionType>
    <!-- sequence allows you to declare the order of child elements -->
    <xsd:sequence>
      <!-- declares Lender, which is of the type address. The type address will be declared later, and will have its own child elements -->
      <xsd:element name="Lender" type="address"/>
      <xsd:element name="Borrower" type="address"/>
      <!-- notes uses a ref, or a reference, since the note element is not being declared within this sequence, rather it was declared outside on the top level and can therefore only be used with a ref -->
      <xsd:element ref="note" minOccurs="0"/>
      <!-- declares paintings, which is of the type paintings. The type paintings will be declared later, and will have its own child elements -->
      <xsd:element name="paintings" type="paintings"/>
    </xsd:sequence>
  
  <!-- declares note element, which is string (string is one of the simple data types built into XML schemas, and therefore you use the xsd namepace normally used for XML schema elements. Simple elements don't contain child elements and/or attributes. This is declared at the top level, outside of any other declarations. Therefore it can be used in a number of different places in the same document - in other words, a note child element can be added to any other element declared in the document. -->
  <xsd:element name="note" type="xsd:string"/>
  
  <!-- declares the address complexType referenced earlier -->
  <xsd:complexType name="address"/>
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string"/>
      <xsd:element name="street" type="xsd:string"/>
      <xsd:element name="city" type="xsd:string"/>
      <xsd:element name="state" type="xsd:string"/>
    </xsd:sequence>
    <!-- use optional declares that the attribute is optional, as opposed to required -->
    <xsd:attribute name="phone" type="xsd:string" use="optional"/>
  </xsd:complexType>
  
  <xsd:complexType name="paintings">
    <xsd:sequence>
      <!-- painting can have between 0 and 10 occurrences based on the minOccurs and maxOccures -->
      <xsd:element name="painting" minOccurs="0" maxOccurs="10">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="paintingTitle" type="xsd:string"/>
            <xsd:element name="creationDate" type="xsd:date" minOccurs="0"/>
            <xsd:element name="replacementValue" type="xsd:decimal"/>
            <xsd:element name="maxDaysOut">
              <xsd:simpleType>
                <xsd:restriction base="xsd:integer">
                  <xsd:maxInclusive value="14"/>
                </xsd:restriction>
              </xsd:simpleType>
            </xsd:element>
          </xsd:sequence>
          <xsd:attribute name="paintingID" type="catalogID"/>
        </xsd:complexType> 
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
  
  <xsd:simpleType name="catalogID">
    <xsd: restriction base="xsd:string">
      <!-- example of requiring a string to match a regular expression. In this case, catalogID must be in the format XXX-XXXX-XXX -->
      <xsd:pattern value="\d{3} - \d{4} - \d{3}"/>
    </xsd:restriction>
  </xsd:simpleType>
  
</xsd:schema>


<!-- in an ordered sequence of elements, you can also allow for a choice of elements. In other words, in the order, you can have a place occupied by one element or another. A choice fragment: -->
<xsd:complexType name="address">
  <xsd:sequence>
    <!-- declares the choice, the name or alias elements can be present in the sequence of elements -->
    <xsd:choice>
      <xsd:element name="name" type="xsd:string"/>
      <xsd:element name="alias" type="xsd:string"/>
    </xsd:choice>
  <xsd:element name="city" type="xsd:string"/>
</xsd:complexType>
    </xsd:choice>
  </xsd:sequence>
</xsd:complexType>