2014年2月17日星期一

XML Schema and XMLspy notes

Introduction

An xml documents consists of elements, attributes and text.
There are two structures in an xml document:
simple ( contains only text ) can ba either Built-in, or Derived
complex ( has attributes or child elements, and possibly texts as well)

Restriction on values:
<xs:element name="age">
    <xs:simpleType>
    <xs:restriction base="xs:integer">
         <xs:minInclusive value="0"/>
         <xs:maxInclusive value="120"/>
    </xs:restriction>    </xs:simpleType>
</xs:element>







To limited the content of an XML element to a set of acceptable values, we can use the enumeration constraint:
<xs:element name="car" type="carType"/>
<xs:simpleType name="carType">
    <xs:restriction base="xs:string">
    <xs:enumeration value="Audi"/>
    <xs:enumeration value="Golf"/>
    <xs:enumeration value="BMW"/>
    </xs:restriction>
</xs:simpleType>
Restrictions on a Series of Values
<xs:element name="initials">
    <xs:simpleType>
    <xs:restriction base="xs:string">
        <xs:pattern value="[A-Z][A-Z][A-Z]"/>
        <xs:pattern value="[a-zA-Z0-9]{8}"/>
        <xs:length value="8"/>
    </xs:restriction>
    </xs:simpleType>
</xs:element>



xs:schema is the root element of the XSD document

<xs: schame 
xmlns:xs="http://www.w3.org/2001/XMLSchema //must have the namespace declaration (Schema for schemas i.e. "http://www.w3.org/2001/XMLSchema")
version="1.1">
...
...
</xs:schema>  under xs:schema (global:can be referenced)/further down the schema hierarchy (local)

xs:schema cam contain the following child elements:
  • xs:include
  • xs:import
  • xs:override     new to XML Schema 1.1
  • xs:element
  • xs:attribute
  • xs:group    enables you to specidy constraints on a specific set of sub-elements
  • xs:attributeGroup
  • xs:annotation     can contain two child elements: xs:appinfo/xs:documentation
  • xs:defaultOpenContent     new to XML Schema 1.1
  • xs:simpleType or
  • xs:complexType   re-use previous definitions (base="complexTypeName", type="complexTypeName")
i.e.
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2014(x64) (http://www.altova.com) by Christian Luntzer (Altova GmbH) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.1">

 <xs:element name="city" type="xs:string" default="MainStreet"/
      //global element name is mandatory, type is optional
<xs:element name="shoesize">  //Complex Element type1:  simple contents   
    <xs:complexType>  //Only contains simple contents: text, attributes
        <xs:simpleContent>
        <xs:extension base="xs:integer">
        <xs:attribute name="country" type="xs:string" />
        </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>
 <xs:element name="address">
  <xs:complexType>      //Complex element Type2: mixed contents
   <xs:annotation>
      <xs:documentation>type which contains driver....
      </xs:documentation>
   </xs:annotation>

   <xs:sequence mixed="ture">    //contains mixed contents
    <xs:element name="street" type="xs:string"/>    //local element declaration "street", mandatory 
    <xs:element ref="city"/>//local element reference "city", name and type attribute cannot be present   </xs:sequence>
  </xs:complexType>
 </xs:element>
<xs:element name="product">  //Complex element Type3: only attribute content 
    <xs:complexType>
        <xs:complexContent>
       <xs:restriction base="xs:integer“>
       <xs:attribute name="prodid" type="xs:positiveInteger"/>
       </xs:restriction>
       </xs:complexContent>
       </xs:complexType>
    </xs:element>

</xs:schema>
 Attributes only can be simple, not complex
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2014(x64) (http://www.altova.com) by Christian Luntzer (Altova GmbH) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.1">
 <xs:element name="gift_voucher">   
  <xs:complexType>
   <xs:attribute ref="voucher_id"/> //local attribute reference to a global attribute declaration
   <xs:attribute name="expiry_date" type="xs:date"/>  //(local attribute) name is mandatory, type is optional  </xs:complexType>
 </xs:element>
 <xs:attribute name="voucher_id" type="xs:positiveInteger"/> global attribute declaration
<xs:element name="person">  //MinOccurs and MaxOccurs can be used for both element and group
<xs:complexType>    
<xs:sequence>
<xs:element name="full_name" type="xs:string"/>
<xs:element name="child_name" type="xs:string" maxOccurs="10" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>



 default/fixed: mutually exclusive
If an element appeas but is empty, the schema processor provides the default value. If the element is missing, the schema processor does not provide the default value.
Cannot appear on local element references.

 Attribute's default value will provide a default value for an attribute if the attribute is missing from the xml document. It will not provide a default value for a missing element.

Group must be defined globally, though the local reference to a globally named group can exist.
unnamed groups (compositors): xs:sequence; xs:choice; xs:all also can be used foe complex element
  <xs:group name="vehiclegroup" minOccurs="1" maxOccurs="1">
  <xs:choice>  //only one sub-element can be used from a list of sub-elements
   <xs:element name="car"/>
   <xs:element name="motorbike"/>
  </xs:choice>
 </xs:group>
<xs:element name="car" type="carinfo"/>
<xs:complexType name="carinfo">
    <xs:sequence>
        <xs:group ref="vehiclegroup"/>    //re-using a group definition , the referenced element has to be a global one.
        <xs:element name="country" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

 attributeGroup situations arise where there are several different elements that will require the same set of attributes.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.1">
 <xs:element name="person">
  <xs:complexType>
   <xs:attributeGroup ref="dimensions"/>
  </xs:complexType>
 </xs:element>
 <xs:attributeGroup name="dimensions">
  <xs:attribute name="height" type="xs:positiveInteger"/>
  <xs:attribute name="weight" type="xs:positiveInteger"/>
 </xs:attributeGroup>
</xs:schema>








 

Built-in datatypes

only one complex datatype:
<xs: element name="para" type="xs:anyType"/> ==<xs:element name="pare"/>

 

User-defined Simple Types

simple types can only be derived via restriction. This mean that the derived simple type will only allow a base type it is being derived from. The base type is more general, the derived type is more specific.

Namespaces

Mixing Several Namespaces
<gs:dish xmlns: gs = “http://china.com“
               xmlns: uom = “http://units.com“>
<gs:dm uom:unit = “cm“>20</gs:dm>
<gs:vol uom:unit = “1“>5</gs:vol>
<gs:decor>Meissner</gs:decor>
<comment>This is an unqualified element name</comment>
</gs:dish>

reference:

2014年2月13日星期四

XML-->DTD&Schema Notes


  • Introduction


DTD is the abbreviation for "Document Type Definition" to define the legal building blocks of   an XML document with a list of legal elements and attributes, which can be defined inline an XML doc or as an external reference. With the DTD, your can verify the data that you receive from the outside world is valid. Elements/attributes names in XML are case-sensitive, so DTD must be case-sensitive also!
Example: inline example
<?xml version="1.0"?>  <? PI ?>
<!DOCTYPE note [                // defines that the root element of this document is note
<!ELEMENT note (to,from,heading,body)>  //defines that the note element contains four elements: "to,from,heading,body"
<!ELEMENT to (#PCDATA)>   // defines the to element to be of type "#PCDATA"
<!ELEMENT from (#PCDATA)>                                   Parsable character data 
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>

<note>           Root element
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>


external example: 
use <!DOCTYPE note SYSTEM "note.dtd"> to replace the inline DTD block
                   SYSTEM indicates its a private

  • Building Block

Elements, Attributes, Entities, CDATA, PCDATA
PCDATA should not contain any characters like &, > or < which should be represented by &amp, &lt and &gt entities, respectively.
CDATA will not be parsed by a parser.

elements:

  1. Declaring Elements:<!ELEMENT element-name category>
  2. Empty elements: <!ELEMENT oven EMPTY>  <oven/>
  3. Elements with only parsed character data: <!ELEMENT from (#PCDATA)>
  4. can contain any combination of parable data: <!ELEMENT note ANY> replace by specific content now.
  5. Elements with one or more children: <!ELEMENT element-name (child1,child2,…)>
  6. Only one occurrence: <!ELEMENT element-name (child-name)>
  7. one or more occurrence: <!ELEMENT element-name (child-name+)>
  8. zero or more occurrence: <!ELEMENT element-name (child-name*)>
  9. zero or one occurrence: <!ELEMENT element-name (child-name?)>
  10. either or occurrence: <!ELEMENT note (to,from,header,(message|body))>
  11. mixed content: <!ELEMENT note (#PCDATA|to|from|header|message)*>
5:consecutively; 11: no specific sequence

attributes:

<!ATTLIST element-name attribute-name attribute-type attribute-value>


Declaration:     <!ATTLIST payment type CDATA "check">  
XML example:     <payment type="check" /> 

TypesDescription
CDATA (strings)The value is character data except <, >, &, and
(en1|en2|..)  enumeratedThe value must be one from an enumerated list
ID tokenized most restrictiveThe value is a unique id--> Uniquely identifies an element
IDREF tokenizedThe value is the id of another element--> Point to element with ID
IDREFS tokenizedThe value is a list of other ids  consistency to ID
NMTOKENThe value is a valid XML name
NMTOKENSThe value is a list of valid XML names
ENTITY tokenizedThe value is an entity
ENTITIES tokenizedThe value is a list of entities
NOTATIONThe value is a name of a notation
xml:The value is a predefined xml value

value: the value of the attribute

Required:
<!ATTLIST person number CDATA #REQUIRED>

Valid XML:
<person number="5677" />

Invalid XML:
<person />

Implied:
<!ATTLIST contact fax CDATA #IMPLIED>

Valid XML:
<contact fax="555-667788" />

Valid XML:
<contact />

Fixed:
<!ATTLIST sender company CDATA #FIXED "Microsoft">

Valid XML:
<sender company="Microsoft" />

Invalid XML:
<sender company="W3Schools" />

Enumerated attribute values:
<!ATTLIST payment type (check|cash) "cash">

XML example:
<payment type="check" />
or
<payment type="cash" />

elements vs attributes:

  There is no rule for when to use elements or attributes
  Store data in element is better and use attribute to provide information not relevant to data.
   Metadata (data about data) should be stored as attributes, and that data itself should be stored   as elements.


Entities:

internal entities: <!ENTITY entity-name "entity-value">
DTD Example:

<!ENTITY writer "Donald Duck.">
<!ENTITY copyright "Copyright W3Schools.">

XML example:

<author>&writer;&copyright;</author>

external entities:<!ENTITY entity-name SYSTEM "URI/URL">
DTD Example:

<!ENTITY writer SYSTEM "http://www.w3schools.com/entities.dtd">
<!ENTITY copyright SYSTEM "http://www.w3schools.com/entities.dtd">

XML example:

<author>&writer;&copyright;</author>

  • A General XML Validator Errors in XML documents will stop your XML program. 

To help you check your xml files, you can syntax-check any XML file here.

Semantic Web