] > XML (Extensible Markup Language)

XML (Extensible Markup Language)

17.1 Background

17.2 Syntax

17.3 Relations and XML

17.4 Well Formed Files

17.5 Rendering Semantics

17.6 Recommended Standards

http://www.w3.org:

17.7 Name Spaces

<html xmlns  ="...xhtml..." 
      xmlns:m="...mathml..." 
      xmlns:s="...svg.."> 
  .... 
  <p> 
     <m:math>...</m:math> 
     <s:svg>...</s:svg> 
  </p> 
  ... 
</html> 

17.8 Document Type Definitions (DTD’s)

<!DOCTYPE  COURSES SYSTEM "file.dtd"> 
<COURSES> 
  <course ref="sp"> 
     <number>670</number> 
     <days>TR</days> 
  </course> 
  <course ref="au"> 
     <number>680</number> 
     <days>MWF</days> 
  </course> 
  <quarter id="sp">Spring</quarter> 
  <quarter id="au">Autumn</quarter> 
</COURSES> 
file.dtd
<!ELEMENT COURSES (course | quarter)* > 
<!ELEMENT course  (number,days) > 
<!ELEMENT quarter (#PCDATA)> 
<!ELEMENT number  (#PCDATA)> 
<!ELEMENT days    (#PCDATA)> 
<!ATTLIST course  ref  IDREF #REQUIRED 
                  name CDATA #IMPLIED > 
<!ATTLIST quarter id   ID    #REQUIRED> 

17.9 A Java DTD-Based Validator

<..Dtd.java..>
 import java.io.File;
 import javax.xml.parsers.*;
 import org.xml.sax.*;
 import org.xml.sax.ext.*;
 
 class Dtd {
   static public void main(String[] args) throws Exception {
     SAXParserFactory factory = SAXParserFactory.newInstance();
     factory.setValidating( args.length>1 );
     SAXParser saxParser = factory.newSAXParser();
     XMLReader xmlReader = saxParser.getXMLReader();
     xmlReader.parse ( new File( args[args.length-1] ).toURL().toString() );
 } }
 -_-_-

javac Dtd.java

This command compiles the program

java Dtd xml-filename

This command checks whether the given file is well formed.

java Dtd validate xml-filename

This command checks whether the given file validates against the specified DTD.

<..course.xml..>
 <!DOCTYPE  COURSES SYSTEM "file.dtd">
 <COURSES>
   <course ref="sp">
      <number>670</number>
      <days>TR</days>
   </course>
   <course ref="au">
      <number>680</number>
      <days>MWF</days>
   </course>
   <quarter id="sp">Spring</quarter>
   <quarter id="au">Autumn</quarter>
 </COURSES>
 -_-_-

<..Dtd.dtd..>
 <!ELEMENT COURSES (course | quarter)* >
 <!ELEMENT course  (number,days) >
 <!ELEMENT quarter (#PCDATA)>
 <!ELEMENT number  (#PCDATA)>
 <!ELEMENT days    (#PCDATA)>
 <!ATTLIST course  ref  IDREF #REQUIRED
                   name CDATA #IMPLIED >
 <!ATTLIST quarter id   ID    #REQUIRED>
 -_-_-

17.10 XML Schema (XML Schema Definition, XSD)

<?xml version="1.0"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" > 
<xs:element name="student"> 
    <xs:complexType> 
      <xs:sequence> 
         <xs:element name="fname"  type="xs:string"/> 
         <xs:element name="ssn"    type="xs:integer"/> 
         <xs:element name="bdate"  type="xs:date"/> 
      </xs:sequence> 
      <xs:attribute name="status" type="xs:string"/> 
    </xs:complexType> 
</xs:element> 
</xs:schema> 
<?xml version="1.0"?> 
<student 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      status="junior" > 
   <fname>Joe</fname> 
   <ssn>123456789</ssn> 
   <bdate>1982-11-24</bdate> 
</student> 
<!ELEMENT student (fname, ssn, bdate) > 
<!ELEMENT fname  (#PCDATA) > 
<!ELEMENT ssn    (#PCDATA) > 
<!ELEMENT bdate  (#PCDATA) > 
<!ATTLIST student status #IMPLIED > 

Elements and Attributes

Simple Types

pattern
<restriction base="string"> 
  <pattern value="me | you"/> 
</restriction> 
<pattern value="[A-Za-z][02468]"/> 
<pattern value="[0-9]{4}"/> 
<pattern value="(0|1)+"/> 
<pattern value="(0|1)*"/> 
enumerationDomain of values
<restriction base="string"> 
  <enumeration value="square"/> 
  <enumeration value="round"/> 
</restriction> 
length
minLength
maxLength
number of characters or list items
<restriction base="string"> 
  <minLength value="2"/> 
  <maxLength value="4"/> 
</restriction> 
whiteSpaceValues: preserve, replace (into space ch), collapse.
<restriction base="string"> 
  <whiteSpace value="preserve"/> 
</restriction> 
minInclusive
maxInclusive
minExclusive
maxExclusive
Range of values
<restriction base="integer"> 
  <minInclusive value="0"/> 
  <maxInclusive value="17"/> 
</restriction> 
totalDigits
fractionDigits
number of all/decimal digits

Complex Types

Example

XML Code from a Database

students
namessn
Ron123-45-6789
Nora987-65-4321
courses
titlecode
data bases670
NULL680
classes
codessn
670123-45-6789
680987-65-4321
<?xml version="1.0"?> 
<dataBase 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
> 
  <students> 
    <student> <name>Ron</name> 
              <ssn>123-45-6789</ssn> </student> 
    <student> <name>Nora</name> 
              <ssn>987-65-4321</ssn> </student> 
  </students> 
 
  <courses> 
    <course> <title>data bases</title> 
             <code>670</code>         </course> 
    <course> <title xsi:nil="true" /> 
             <code>680</code>         </course> 
  </courses> 
 
  <classes> 
    <class> <code>670</code> 
            <ssn>123-45-6789</ssn>  </class> 
    <class> <code>680</code> 
            <ssn>987-65-4321</ssn> </class> 
  </classes> 
</dataBase> 

From Relations to XML Sub-Schemas

<students> 
  <student> <name>Ron</name> 
            <ssn>123-45-6789</ssn> </student> 
  <student> <name>Nora</name> 
            <ssn>987-65-4321</ssn> </student> 
</students> 
<xs:element name="students"> 
  <xs:complexType> 
    <xs:choice> 
      <xs:element name="student" minOccurs="0" maxOccurs="unbounded"> 
        <xs:complexType> 
          <xs:all> 
            <xs:element name="name" type="nameType"/> 
            <xs:element name="ssn" type="ssnType"/> 
          </xs:all> 
        </xs:complexType> 
      </xs:element> 
    </xs:choice> 
  </xs:complexType> 
</xs:element> 
  <classes> 
    <class> <code>670</code> 
            <ssn>123-45-6789</ssn>  </class> 
    <class> <code>680</code> 
            <ssn>987-65-4321</ssn> </class> 
  </classes> 
<xs:element name="classes"> 
  <xs:complexType> 
    <xs:choice> 
      <xs:element name="class" minOccurs="0" maxOccurs="unbounded"> 
        <xs:complexType> 
          <xs:all> 
            <xs:element name="code" type="codeType"/> 
            <xs:element name="ssn" type="ssnType"/> 
          </xs:all> 
        </xs:complexType> 
      </xs:element> 
    </xs:choice> 
  </xs:complexType> 
</xs:element> 

XML Schemas for the Database

<?xml version="1.0"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
 
  <xs:element name="dataBase"> 
    <xs:complexType> 
      <xs:all> 
        <xs:element name="students"> ... </xs:element> 
        <xs:element name="courses"> ... </xs:element> 
        <xs:element name="classes"> ... </xs:element> 
      </xs:all> 
    </xs:complexType> 
  </xs:element> 
 
  <xs:simpleType name="codeType"> 
    <xs:restriction base="xs:string"> 
      <xs:pattern value="[1-9][0-9][0-9]"/> 
    </xs:restriction> 
  </xs:simpleType> 
 
  <xs:simpleType name="ssnType"> 
    <xs:restriction base="xs:string"> 
      <xs:pattern value="[0-9]{3}-[0-9]{2}-[0-9]{4}"/> 
    </xs:restriction> 
  </xs:simpleType> 
 
  <xs:simpleType name="nameType"> 
    <xs:restriction base="xs:string"/> 
  </xs:simpleType> 
 
</xs:schema> 

Primary Key and Unique Declarations

  <xs:key name="studentsKey"> 
    <xs:selector xpath="./students/student"/> 
    <xs:field xpath="ssn"/> 
  </xs:key> 

Foreign Key Declaration

  <xs:keyref name="toStudent" refer="studentsKey"> 
    <xs:selector xpath="./classes/class"/> 
    <xs:field xpath="ssn"/> 
  </xs:keyref> 

[full code]

17.11 A Java XMLSchema-Based Validator

<..Xsd.java..>
 import javax.xml.validation.SchemaFactory;
 import javax.xml.XMLConstants;
 import javax.xml.transform.stream.StreamSource;
 import javax.xml.validation.Schema;
 import javax.xml.validation.Validator;
 
 class Xsd {
   public static void main(String[] args) {
     try {
        SchemaFactory factory = SchemaFactory.newInstance(
                                XMLConstants.W3C_XML_SCHEMA_NS_URI );
        Schema schema = factory.newSchema(new StreamSource( args[0] ));
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource( args[1] ));
     } catch (Exception e) {
        System.err.println("--- Error --- " + e);
 } } }
 -_-_-

javac Xsd.java

This command compiles the program

java Xsd schema-filename xml-filename

This command checks whether the given file validates against the specified Schema.

<..student.xml..>
 <?xml version="1.0"?>
 <student xmlns:xsi=
  "http://www.w3.org/2001/XMLSchema-instance"
  status="junior"
 >
    <fname>Joe</fname>
    <ssn>123456789</ssn>
    <bdate>1982-11-24</bdate>
 </student>
 -_-_-

<..student.xsd..>
 <?xml version="1.0"?>
 <xs:schema xmlns:xs=
  "http://www.w3.org/2001/XMLSchema">
 <xs:element name="student">
    <xs:complexType>
       <xs:sequence>
          <xs:element name="fname"
                   type="xs:string"/>
          <xs:element name="ssn"
                  type="xs:integer"/>
          <xs:element name="bdate"
                     type="xs:date"/>
       </xs:sequence>
       <xs:attribute name="status"
                   type="xs:string"/>
    </xs:complexType>
 </xs:element>
 </xs:schema>
 -_-_-

17.12 XML Path (XPath)

A simple query language

Navigation Space (The Data Model)

<COURSES> 
  <course attr="database"> 
     <number >670</number> 
     <quarter>Spring</quarter> 
     <days>TR</days> 
     <!--a comment--> 
  </course> 
  <course> ... </course> 
  <course> ... </course> 
</COURSES> 
                                     ----                                    --root---                                     --|--                                       |                                 -|----------|-                                 ||COURSES----                         |---|--------------------|---|                        -|course|| ||course-|| ||course-|                             |         |          |                             |         ...        ...      |-----------|---------|----------------------| ---------|| ---------|- -|----|| ||----------|| ----| -|number-|- -|quarter--- ||days|- ||comment-()-|| -attr      |           |         |            |         |      |           |         |           a          |     670       Spring      TR        comment   databases

Navigation Directions

Directions of tree traversal:

Path and Query Directives

The respond of a query is a set of nodes.

Sample Program

<..Path.java..>
 import org.w3c.dom.NodeList;
 import org.xml.sax.InputSource;
 import javax.xml.xpath.XPath;
 import javax.xml.xpath.XPathConstants;
 import javax.xml.xpath.XPathFactory;
 
 
 class Path {
   public static void main(String[] args) {
     String expression;
     try {
       XPath xpath = XPathFactory.newInstance().newXPath();
       InputSource document = new InputSource("course.xml");
 
       expression = "COURSES/course";
       NodeList nameNodes = (NodeList) xpath.evaluate(expression,
               document, XPathConstants.NODESET);
       System.out.println("Number of courses: " + nameNodes.getLength());
 
       expression = "COURSES/course[1]/days";
       String days = (String) xpath.evaluate(expression, document,
               XPathConstants.STRING);
       System.out.println("Days: " + days);
 
     } catch (Exception e) {
         System.err.println("--- Error --- " + e);
 } } }
 -_-_-

17.13 Query Languages

17.14 Sample Problems

  1. Give a XML document representing the relation ...
  2. Give a XML document obeying the DTD ...
  3. Give a XML document obeying the XML schema ...
  4. Give a DTD appropriate for the XML document ...
  5. Give a DTD appropriate for the XML schema ...
  6. Give a XML schema appropriate for the XML document ...
  7. Give a XML schema appropriate for the DTD ...

Reference: Ch. 27 (5th ed) and 26 (4th ed) in textbook.