ipt chapter 3 data mapping and exchange - dr. j. vijipriya

22
Chapter 3 Data Mapping and Exchange Integrative Programming and Technology 08/12/2015 1 Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia

Upload: vijipriya-jeyamani

Post on 09-Jan-2017

188 views

Category:

Engineering


22 download

TRANSCRIPT

Page 1: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 1

Chapter 3Data Mapping and Exchange

Integrative Programming and Technology

08/12/2015

Page 2: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 2

XML DTD and XML Schema

How does an XML processor check your xml document?There are two main checks that XML processors make: 1. Checking that your document is well-formed ( Syntax rule)2. Checking that it's valid (syntax-check your XML either in

XML DTD or XSD) DTD- Document Type DefinitionXSD-XML Schema Definition

Why need XML Validator Use our XML validator to syntax-check your XML. Errors in XML documents will stop your XML applications

unlike HTML browser

08/12/2015

Page 3: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 3

XML DTD

An XML document with correct syntax is called "Well Formed".

An XML document validated against a DTD is "Well Formed" and "Valid".

The purpose of a DTD is to define the structure of an XML document and a list of legal elements.

How you add a DTD to our XML document1. DTDs can be separate documents (or )2. They can be built into an XML document using a special element named <!DOCTYPE>.

08/12/2015

Page 4: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 4

An XML Document with a DTD (example4.xml)<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/css" href="css1.css"?><!DOCTYPE document [ <!ELEMENT document (heading, message)> <!ELEMENT heading (#PCDATA)> <!ELEMENT message (#PCDATA)>]><document>

<heading> Hello From XML </heading> <message> This is an XML document! </message></document>

08/12/2015

XML DTD Con…

Page 5: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 5

Valid XML Document with DTD (example.5.xml) The DOCTYPE declaration is a reference to an external DTD file "Note.dtd“

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE note SYSTEM "Note.dtd"><note>

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

</note> Note.dtd

<!DOCTYPE note[

<!ELEMENT note (to,from,heading,body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>

]>

08/12/2015

XML DTD Con…

Page 6: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 6

The DTD above is interpreted like this:

!DOCTYPE note defines that the root element of the document is note !ELEMENT note defines that the note element contains four elements:

"to, from, heading, body" !ELEMENT to defines the to element to be of type "#PCDATA" !ELEMENT from defines the from element to be of type "#PCDATA" !ELEMENT heading defines the heading element to be of type

"#PCDATA" !ELEMENT body defines the body element to be of type "#PCDATA“

Note#PCDATA means parse-able text data.

When NOT to Use a Document Definition? When you are working with small XML files, creating document

definitions may be a waste of time.

08/12/2015

XML DTD Con…

Page 7: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 7

XML Schema

Another way of validating XML documents: using XML schemas.

The XML Schema language is also referred to as XML Schema Definition (XSD), describes the structure of an XML document.

defines the legal building blocks (elements and attributes) of an XML document like DTD.

defines which elements are child elements defines the number and order of child elements defines whether an element is empty or can include text defines data types for elements and attributes defines default and fixed values for elements and

attributes

08/12/2015

Page 8: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 8

XML Schemas will be used in most Web applications as a replacement for DTDs.

Here are some reasons: XML Schemas are extensible to future additions XML Schemas are richer and more powerful than DTDs XML Schemas are written in XML XML Schemas support data types and namespaces

Creating XML Schemas by Using XML Schema-Creation Tools

HiT Software xmlArchitect XMLspy XML Ray Microsoft Visual Studio .NET

08/12/2015

XML Schema Con…

Page 9: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 9

XSD Simple Element The syntax for defining a simple element Default and Fixed Values for Simple Elements

XSD Attributes The syntax for defining an attribute Default and Fixed Values for Attributes Optional and Required Attributes

XSD Complex Elements How to Define a Complex Element using XML Scheme XSD Empty Elements

XSD IndicatorsOrder indicators are: All Choice Sequence

08/12/2015

XML Schema Con…

Page 10: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 10

XSD Simple Element A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes. XML Schema has a lot of built-in data types. The most common types are:

1. xs:string2. xs:decimal3. xs:integer4. xs:boolean5. xs:date6. xs:time

The syntax for defining a simple element is: <xs:element name="xxx" type="yyy"/> Example

<lastname>Refsnes</lastname><age>36</age><dateborn>1970-03-27</dateborn> <xs:element name="lastname" type="xs:string"/><xs:element name="age" type="xs:integer"/><xs:element name="dateborn" type="xs:date"/>

08/12/2015

XML Schema Con…

Page 11: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 11

Default and Fixed Values for Simple Elements

Simple elements may have a default value or a fixed value specified.

1. A default value is automatically assigned to the element when no other value is specified.

<xs:element name="color" type="xs:string" default="red"/>

2. A fixed value is also automatically assigned to the element, and you cannot specify another value.

<xs:element name="color" type="xs:string" fixed="red"/>

08/12/2015

XML Schema Con…

Page 12: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 12

XSD Attributes Simple elements cannot have attributes. If an element has attributes, it is considered to be of a complex type. But

the attribute itself is always declared as a simple type.The syntax for defining an attribute is:

<xs : attribute name="xxx" type="yyy"/> Example

<lastname lang="EN">Smith</lastname> <xs:attribute name="lang" type="xs:string"/>

Default and Fixed Values for AttributesAttributes may have a default value or a fixed value specified.

<xs:attribute name="lang" type="xs:string" default="EN"/> <xs:attribute name="lang" type="xs:string" fixed="EN"/>

Optional and Required AttributesAttributes are optional by default. To specify that the attribute is required,

use the "use" attribute:<xs:attribute name="lang" type="xs:string" use="required"/>

08/12/2015

XML Schema Con…

Page 13: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 13

XSD Complex Elements A complex element is an XML element that contains other elements and/or

attributes. There are four kinds of complex elements:

Example:1. A complex XML element, "product", which is empty:

<product pid="1345"/>

2. A complex XML element, "employee", which contains only other elements:<employee>  <firstname>John</firstname>  <lastname>Smith</lastname></employee>

3. A complex XML element, "food", which contains only text:<food type="dessert">Ice cream</food>

4. A complex XML element, "description", which contains both elements and text:<description>It happened on <date lang=“EN">03.03.99</date> </description>

08/12/2015

XML Schema Con…

Page 14: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 14

How to Define a Complex Element using XML Scheme Complex XML element, "employee", which contains only other elements:

<employee>  <firstname>John</firstname>  <lastname>Smith</lastname></employee>

The "employee" element can be declared directly by naming the element:<xs:element name="employee">  <xs:complexType>    <xs:sequence>      <xs:element name="firstname" type="xs:string"/>      <xs:element name="lastname" type="xs:string"/>    </xs:sequence>  </xs:complexType></xs:element>

An empty complex element cannot have contents, only attributes.<product prodid="1345" />

It is possible to declare the "product" element more compactly:<xs:element name="product">  <xs:complexType>    <xs:attribute name="prodid" type="xs:positiveInteger"/>  </xs:complexType></xs:element>

08/12/2015

XML Schema Con…

Page 15: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 15

XSD Indicators How elements are to be used in documents with indicators. Order indicators are used to define the order of the elements.They are:

1. All2. Choice3. Sequence

All Indicator: The <all> indicator specifies that the child elements can appear in any

order, and that each child element must occur only once: <xs:element name="person">  <xs:complexType>    <xs:all>      <xs:element name="firstname" type="xs:string"/>      <xs:element name="lastname" type="xs:string"/>    </xs:all>  </xs:complexType></xs:element>

08/12/2015

XML Schema Con…

Page 16: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 16

Choice Indicator: The <choice> indicator specifies that either one child element or another can occur:

<xs:element name="person">  <xs:complexType>    <xs:choice>      <xs:element name="employee" type="employee"/>      <xs:element name="member" type="member"/>    </xs:choice>  </xs:complexType></xs:element>

Sequence Indicator: The <sequence> indicator specifies that the child elements must appear in a specific

order:<xs:element name="person">   <xs:complexType>    <xs:sequence>      <xs:element name="firstname" type="xs:string"/>      <xs:element name="lastname" type="xs:string"/>    </xs:sequence>  </xs:complexType></xs:element>

08/12/2015

XML Schema Con…

Page 17: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 17

XML Example (“note.xml”)<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

XSD Example (“note.xsd”)<xs:element name="note"><xs:complexType>

  <xs:sequence>    <xs:element name="to" type="xs:string"/>    <xs:element name="from" type="xs:string"/>    <xs:element name="heading" type="xs:string"/>    <xs:element name="body" type="xs:string"/>  </xs:sequence>

</xs:complexType></xs:element>

08/12/2015

XML Schema Con…

Page 18: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 18

Example (shiporder.xml):<?xml version="1.0" encoding="ISO-8859-1"?><shiporder orderid="889923">  <orderperson>John Smith</orderperson>  <shipto>    <name>Ola Nordmann</name>    <address>Langgt 23</address>    <city>4000 Stavanger</city>    <country>Norway</country>  </shipto> </shiporder>

08/12/2015

XML Schema Con…

Page 19: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 19

Example "shiporder.xsd":<?xml version="1.0" encoding="ISO-8859-1" ?>

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

<xs:element name="shiporder">  <xs:complexType>    <xs:sequence>

      <xs:element name="orderperson" type="xs:string"/>     

<xs:element name="shipto">        <xs:complexType>

          <xs:sequence>            <xs:element name="name" type="xs:string"/>            <xs:element name="address" type="xs:string"/>            <xs:element name="city" type="xs:string"/>            <xs:element name="country" type="xs:string"/>          </xs:sequence>      

  </xs:complexType>      </xs:element>   

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

</xs:schema>

08/12/2015

XML Schema Con…

Page 20: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 20

XML Parser (Parsing XML documents)

All modern browsers have a built-in XML parser. An XML parser converts an XML document into an XML DOM object -

which can then be manipulated with JavaScript.

Parse an XML Document The following code fragment parses an XML document into an XML DOM

object:if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp.open("GET","books.xml",false);xmlhttp.send();xmlDoc=xmlhttp.responseXML;

08/12/2015

Page 21: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 21

XML DOM The XML DOM defines a standard way for accessing and

manipulating XML documents. The XML DOM views an XML document as a tree-structure. All elements can be accessed through the DOM tree.

Their content (text and attributes) can be modified or deleted, and new elements can be created.

The elements, their text, and their attributes are all known as nodes.

The HTML DOM The HTML DOM defines a standard way for accessing and

manipulating HTML documents. All HTML elements can be accessed through the HTML DOM.

08/12/2015

XML Parser (Parsing XML documents) Con…

Page 22: IPT Chapter 3 Data Mapping and Exchange - Dr. J. VijiPriya

Dr. J. VijiPriya, Assistant Professor, Hawassa University, Ethiopia 22

Load an XML File - Cross-browser parses an XML document ("note.xml") into an XML DOM object and then extracts some information from it with a JavaScript:Example<html><body>

<span id="to"></span><span id="from"></span><span id="message"></span>

<script>if (window.XMLHttpRequest)   { // code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest(); }else  { // code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }

xmlhttp.open("GET","note.xml",false);xmlhttp.send();xmlDoc=xmlhttp.responseXML;

document.getElementById("to").innerHTML= xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;

document.getElementById("from").innerHTML= xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;

document.getElementById("message").innerHTML= xmlDoc.getElementsByTagName("message")[0].childNodes[0].nodeValue;

</script></body></html>

08/12/2015

XML Parser (Parsing XML documents) Con…