Session 1

41
Create by ChungLD faculty XML by Example / Bachkhoa – Aptech Computer Education 1/34 Session 1 Introduction to XML and Namespace

description

XML by Example Session 1

Transcript of Session 1

Page 1: Session 1

Create by ChungLD faculty XML by Example / Bachkhoa – Aptech Computer Education 1/34

Session 1Introduction to XML

andNamespace

Page 2: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 2/34

Objectives Introduction to XML Exploring XML Working with XML XML Syntax XML Namespaces Working with Namespaces Syntax

Page 3: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 3/34

Overview Markup languages GML (Generalized Markup Language-

1970) describes the document in terms of its format, structure and other properties.

SGML (Standard Generalized Markup Language-1980) ensures that the system can represent the data in its own way.

HTML (Hyper text Markup Language - 1989) used ASCII text, which allows the user to use any text editor

Page 4: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 4/34

Evolution of XML XML is a W3C recommendation The first version (1.0) introduced in 1998 XML is a set of rules for defining semantic

tags that break a document into parts and identify the different parts of the document.

XML was developed over HTML because of the basic differences between them given in the table below.

Page 5: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 5/34

Comparison HTML and XML

Page 6: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 6/34

Features of XML XML stands for Extensible Markup

Language XML is a markup language much like HTML XML was designed to describe data XML tags are not predefined. You must

define your own tags XML uses a Document Type Definition

(DTD) or an XML Schema to describe the data

XML with a DTD or XML Schema is designed to be self - descriptive

Page 7: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 7/34

Benefits of XML Data Independence Easier to parse Reducing Server Load Easier to create Web site Content Remote Procedure Calls e-Commerce

Page 8: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 8/34

Example XML Document<?xml version=“1.0” encoding=“iso-8859-1” ?><FlowerPlanet>

<Name>Rose</Name><Price>$1</Price><Description>Red in color</Description><Number>700</Number>

</FlowerPlanet>

Page 9: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 9/34

XML Document Structure

Page 10: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 10/34

XML document life cycle XML parser creates, manipulates and

updates the XML document. It will read the list of records in the XML document and the handler stores it in the form of a data structure. The handler then processes it and displays it in HTML

Page 11: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 11/34

XML Editors The main functions that editors provide:

Add opening and closing tags to the code Check for validity of XML Verify XML against a DTD/Schema Perform series of transforms over a document Color the XML syntax Display the line numbers Present the content and hide the code Complete the word

Page 12: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 12/34

XML Parsers An XML parser/XML processor reads the

document and verifies it for its well-formedness. After the document is verified, the processor converts the document into a tree of elements or a data structure.

Commonly used parser are: MSXML Oracle XML Parser Java API for XML

Page 13: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 13/34

XML Parsers… The two types of parsers are:

Non validating parser: It checks the well formedness of the document. Read the document and checks for its conformity with XML standards.

Validating parser: It checks the validity of the document using DTD

Page 14: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 14/34

Browsers After the XML document is read, the parser

passes the data structure to the client application. The application can be a web browser. Other programs like database, MIDI program or a spreadsheet program.

Commonly used web browsers are: Netscape Mozilla Internet Explorer Firefox Opera

Page 15: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 15/34

Building an XML document The steps to build an XML document are:

Create an XML document in an editor. Save the XML document Load XML document in a browser

Click and see Click and see

Page 16: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 16/34

Exploring the XML document XML version declaration Document Type Definition Document instance in which the content is

defined by the markup.

Click and see Click and see

Page 17: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 17/34

Well-formed XML document Well-formness refers to the standards that

are to be followed by the XML document. A document is well formed, if it fullfills the following rules:

Page 18: Session 1

XML by Example / Bachkhoa – Aptech Computer Education

XML Documents Must Have a Root Element

Create by Chungld faculty 18/34

<root>  <child>    <subchild>.....</subchild>  </child></root>

Page 19: Session 1

XML by Example / Bachkhoa – Aptech Computer Education

XML Tags are Case Sensitive

Create by Chungld faculty 19/34

<Message>This is incorrect</message><message>This is correct</message>

Page 20: Session 1

XML by Example / Bachkhoa – Aptech Computer Education

All XML Elements Must Have a Closing Tag

In HTML, you will often see elements that don't have a closing tag:

In XML, it is illegal to omit the closing tag. All elements must have a closing tag:

Create by Chungld faculty 20/34

<p>This is a paragraph<p>This is another paragraph

<p>This is a paragraph</p><p>This is another paragraph</p>

Page 21: Session 1

XML by Example / Bachkhoa – Aptech Computer Education

XML Elements Must be Properly Nested

In HTML, you might see improperly nested elements:

In XML, all elements must be properly nested within each other:

Create by Chungld faculty 21/34

<b><i>This text is bold and italic</b></i>

<b><i>This text is bold and italic</i></b>

Page 22: Session 1

XML by Example / Bachkhoa – Aptech Computer Education

XML Attribute Values Must be Quoted In XML the attribute value must always be

quoted. Study the two XML documents below. The first one is incorrect, the second is correct:

Create by Chungld faculty 22/34

<note date=12/11/2007>  <to>Tove</to>  <from>Jani</from></note>

<note date=“12/11/2007”>  <to>Tove</to>  <from>Jani</from></note>

Page 23: Session 1

XML by Example / Bachkhoa – Aptech Computer Education

Entity References Some characters have a special meaning in XML. If you place a character like "<" inside an XML element, it

will generate an error because the parser interprets it as the start of a new element.

This will generate an XML error:<message>if salary < 1000 then</message>

To avoid this error, replace the "<" character with an entity reference:<message>if salary &lt; 1000 then</message>

Create by Chungld faculty 23/34

Page 24: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 24/34

Comments in XML Comments is same HMTL

<!-- Comments --> Click and see Click and see

Page 25: Session 1

XML by Example / Bachkhoa – Aptech Computer Education

Processing Instructions Processing instructions are information

which is application specific. The instructions do not follow XML rules or internal syntax. With the help of a parser these instructions are passed to the application

<?PITarget <instructions>?>

Create by Chungld faculty 25/34

Page 26: Session 1

XML by Example / Bachkhoa – Aptech Computer Education

White-space is Preserved in XML HTML truncates multiple white-space

characters to one single white-space:HTML: Hello           my name is Tove Output: Hello my name is Tove.

With XML, the white-space in a document is not truncated.

Create by Chungld faculty 26/34

Page 27: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 27/34

Classification to character data An XML document is divided into markup

and character data. Character data describes the document’s actual content with the white space. The text in character data is not processed by the parser and thus not treated as a regular text.

Character dataCharacter data

PCDATAPCDATA

CDATACDATA

Page 28: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 28/34

PCDATA The data that is parsed by the parser is

called as parsed character data (PCDATA). The PCDATA specifies that the element has parsed character data. It is used in the element declaration.

Example<Semster>X<5&10</Semster>A result it will generate an error as shown:

Page 29: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 29/34

CDATA The text inside a CDATA section is not

parsed by the XML parser. A text is considered in a CDATA section if it contains ‘<‘ or ‘<&>’ character. The CDATA section cannot be nested. It also does not accept line breaks or spaces inside the <![CDATA[ 0 <X< =10   ]]> string. Comments are also not recognized.

Click and see Click and see

Page 30: Session 1

XML by Example / Bachkhoa – Aptech Computer Education

What is an XML Element? An XML element is everything from (including)

the element's start tag to (including) the element's end tag.

An element can contain other elements, simple text or a mixture of both. Elements can also have attributes.

XML elements must follow these naming rules: Names can contain letters, numbers, and other characters Names cannot start with a number or punctuation character Names cannot start with the letters xml (or XML, or Xml, etc) Names cannot contain spaces

Create by Chungld faculty 30/34

Page 31: Session 1

XML by Example / Bachkhoa – Aptech Computer Education

Example elements<bookstore>  <book category="CHILDREN">    <title>Harry Potter</title>    <author>J K. Rowling</author>    <year>2005</year>    <price>29.99</price>  </book>  <book category="WEB">    <title>Learning XML</title>    <author>Erik T. Ray</author>    <year>2003</year>    <price>39.95</price>  </book></bookstore>

Create by Chungld faculty 31/34

Page 32: Session 1

XML by Example / Bachkhoa – Aptech Computer Education

XML Attributes XML elements can have attributes in the

start tag, just like HTML. Attributes provide additional information

about elements.<elementName attName1=“value” attName2=“value”..>

Create by Chungld faculty 32/34

<person sex="female">  <firstname>Anna</firstname>  <lastname>Smith</lastname></person>

Page 33: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 33/34

XML Validation XML with correct syntax is "Well Formed" XML. XML validated against a DTD is "Valid" XML or

XML Schema

Page 34: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 34/34

Namespaces Duplicate element names Consequences of duplicate element names

<Cd>

<Name>…</Name>

</Cd>

<Singer>

<Name>…</Name>

</Singer>

• How do I search for all CD title/names?

• How do I differentiate between cd name and singer name?

Page 35: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 35/34

Define Namespaces In XML, elements are distinguished by

using namespaces. XML Namspaces provide a globally unique name for an element or attribute so that they do not conflict on another.

Page 36: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 36/34

Prefixing element names Using prefixes in the element names provide a

means for the document authors to prevent name collisions

<CD:Title>Feel</CD:Title>

and<Book:Title>Returning to Earth</Book:Title>

But if the prefixes are not unique, the original problem of duplication would still exist. To solve this problem, each namespace prefix is added to a Uniform Resource Identifier or URI that uniquely identifies the namspace.

Page 37: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 37/34

Syntax to add namespace URI

<elementName xmlns:namespacePrefix=“URI”>You can place attributes in a namespaceprefix:localname=“value”

Click and see Click and see

Page 38: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 38/34

Default namspace A default namespace using the xmlns

attribute with a URI as its value. Once this default namespace is declared, child elements that are part of this namespace do not need a namespace prefix.

Click and see Click and see

Page 39: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 39/34

Override default namespaces The default namespace applies to the

element on which it was defined and all descendants of that element. If one of the descendants has another default namespace defined on it, this new namespace definition overrides the previous one and becomes the default for that element and all its descendants.

Click and see Click and see

Page 40: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 40/34

Refer Website http://www.xmluk.org http://www.zvon.org/xxl/

XMLSchemaTutorial/Output/series.html http://www.xmlmaster.org http://xmlwriter.net http://www.w3schools.com/XML/

Page 41: Session 1

XML by Example / Bachkhoa – Aptech Computer EducationCreate by Chungld faculty 41/34

Summary and workshop