Introduction to XML

42
Introduction to XML Jussi Pohjolainen TAMK University of Applied Sciences

description

Introduction to XML.

Transcript of Introduction to XML

Page 1: Introduction to XML

Introduction to XML

Jussi PohjolainenTAMK University of Applied Sciences

Page 2: Introduction to XML

What is XML?

• eXtensible Markup Language, is a specification for creating custom markup languages

• W3C Recommendation• Primary purpose is to help computers to share

data• XML is meta-language. This means that you

use it for creating languages.• XML is an extensive concept.

Page 3: Introduction to XML

XML Document

• Every XML-document is text-based• => sharing data between different computers!• => sharing data in Internet!• => platform independence!

Page 4: Introduction to XML

Binary vs. Text

• Problems with Binary format– Platform depence– Firewalls– Hard to debug– Inspecting the file can be hard

• Since XML is text-based, it does not have the problems mentioned above.

• What are the disadvantages in text format?

Page 5: Introduction to XML

XML Doc Advantages

• Easy data sharing, text documents are readable between any device.

• Documents can be modified with any text editor.• Possible to understand the contents of the xml-

document just by looking at it with text editor.• Easy to manipulate via programming languages• Two levels of correctness: Well formed and Valid.

Page 6: Introduction to XML

.doc – file format

WindowsMS Word 2000

Mac OS XSince .doc is closed binary-format,there are very few alternatives forword processors that fully support

the doc – file format

0101011010101010001010101010111010101000101110101011101010101101011110101010101010101010

Page 7: Introduction to XML

.docx – file format (Office Open XML)

WindowsMS Word 2007

Mac OS XHopefully in the future there

will be loads of free programs that support this new open and easy access file format

<xml><heading1>title</heading1>..</xml>

Now the format isopen and it's much

easier to access

Page 8: Introduction to XML

SGML vs. XML

SGML: Standard Generalized Markup Language SGML: Standard Generalized Markup Language

XMLXML

HTML(.html)

XHTML(.xhtml)

MathML(.mml)

OOXML(.docx)

Page 9: Introduction to XML

XML – Meta Language

• XML is meta language, which you can use to create your own markup languages.

• There are several XML Markup Languages made for different purposes

• All the languages have common xml-rules• Languages: XHTML, OOXML, Open Document, RSS,

SVG, SOAP, SMIL, MathML...• List:

– http://en.wikipedia.org/wiki/List_of_XML_markup_languages

Page 10: Introduction to XML

XHTML - Example<?xml version="1.0"?>

<!DOCTYPE html

PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Minimal XHTML 1.0 Document</title>

</head>

<body>

<p>This is a minimal <a href="http://www.w3.org/TR/xhtml1/">XHTML 1.0</a>

document.</p>

</body>

</html>

Page 11: Introduction to XML

SVG - Example<?xml version="1.0"?>

<!DOCTYPE svg

PUBLIC "-//W3C//DTD SVG 1.1//EN"

"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<svg width="100%" height="100%" version="1.1"

xmlns="http://www.w3.org/2000/svg">

<circle cx="100" cy="50" r="40" stroke="black"

stroke-width="2" fill="red"/>

</svg>

Page 12: Introduction to XML

MathML (Open Office)<?xml version="1.0"?>

<!DOCTYPE math:math PUBLIC "-//OpenOffice.org//DTD Modified W3C MathML 1.01//EN" "math.dtd">

<math:math xmlns:math="http://www.w3.org/1998/Math/MathML">

<math:semantics>

<math:mrow>

<math:mi>x</math:mi>

<math:mo math:stretchy="false">=</math:mo>

<math:mfrac>

<math:mrow>

...

</math:mrow>

<math:annotation math:encoding="StarMath 5.0">x = {-b +-sqrt{b^{2}-4{ac}} } over {2 {a}} </math:annotation>

</math:semantics>

</math:math>

Page 13: Introduction to XML

RSS 2.0 - Example<?xml version="1.0"?>

<rss version="2.0">

<channel>

<title>W3Schools Home Page</title>

<link>http://www.w3schools.com</link>

<description>Free web building tutorials</description>

<item>

<title>RSS Tutorial</title>

<link>http://www.w3schools.com/rss</link>

<description>New RSS tutorial on W3Schools</description>

</item>

<item>

<title>XML Tutorial</title>

<link>http://www.w3schools.com/xml</link>

<description>New XML tutorial on W3Schools</description>

</item>

</channel>

</rss>

Page 14: Introduction to XML

XML Editors

• XML Spy • EditiX • Microsoft XML Notepad• Visual XML• XML Viewer• Xeena• XML Styler, Morphon, XML Writer…

Page 15: Introduction to XML

WELL FORMED XML - DOCUMENTRules that Apply to Every XML-Document

Page 16: Introduction to XML

Correctness

• There are two levels of correctness of an XML document:1. Well-formed. A well-formed document

conforms to all of XML's syntax rules. 2. Valid. A valid document additionally conforms to

some semantic rules.

• Let's first look at the XML's syntax rules (1).

Page 17: Introduction to XML

Simple Generic XML Example<?xml version="1.0" encoding="utf-8" standalone="yes"?>

<presentation>

<slide number="1">

<name>Introduction to XML</name>

<contents>XML is ...</contents>

</slide>

</presentation>

Page 18: Introduction to XML

XML-Declaration

• XML-declaration is optional in XML 1.0, mandatory in 1.1.– Recommendation: use it.

• Version: 1.0 or 1.1• Encoding: character encoding, default utf-8• Standalone: – is the xml-document linked to external markup declaration– yes: no external markup declarations– no: can have external markup declaration (open issue..)– default: "no"

Page 19: Introduction to XML

Comparing Declarations<?xml version="1.0" encoding="utf-8" standalone="no"?>

<presentation>

<slide>

<name>Introduction to XML</name>

<contents>XML is ...</contents>

</slide>

</presentation>

<?xml version="1.0"?>

<presentation>

<slide>

<name>Introduction to XML</name>

<contents>XML is ...</contents>

</slide>

</presentation>

Same DeclarationSame Declaration

Page 20: Introduction to XML

Element vs. Tag vs. Attribute• Element consists of start tag, optional content and an

end tag:– <name>Introduction to XML</name>

• Start tag– <name>

• Content– Introduction to XML

• End tag– </name>

• Start tag may have attribute– <slide number="1">

Page 21: Introduction to XML

Rules about Elements

• Only one root - element• Every element contains starting tag and an ending tag• Content is optional: Empty element– <x></x> <!-- same as -->– <x/>

• Tag – names are case-sensitive:– <X></x> <!-- Error -->

• Elements must be ended with the end tag in correct order:– <p><i>problem here</p></i> <!– Error

Page 22: Introduction to XML

Rules about Attributes

• XML elements can have attributes in the start tag.

• Attributes must be quoted:– <person sex="female">– <person sex='female'>– <gangster name='George "Shotgun" Ziegler'>– <gangster name="George &quot;Shotgun&quot; Ziegler">

Page 23: Introduction to XML

Naming Tags

• Names can contain letters, numbers, and other characters

• Names must not start with a number or punctuation character

• Names must not start with the letters xml (or XML, or Xml, etc)

• Names cannot contain spaces

Page 24: Introduction to XML

Well-Formed XML

• XML document is well-formed if it follows the syntax rules.

• XML document must be well-formed! – it's not an xml-document, if it does not follow the

rules..

Page 25: Introduction to XML

Is this Well-Formed XML Document?

<?xml version="1.0"?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Minimal XHTML 1.0 Document</title>

</head>

<body>

<p>This is a minimal <a href="http://www.w3.org/TR/xhtml1/">XHTML 1.0</a>

document.</p>

</body>

</html>

Page 26: Introduction to XML

Is this Well-Formed XML Document?

<?xml version="1.0"?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Minimal XHTML 1.0 Document</title>

</head>

<body>

<jorma>This is a minimal <a href="http://www.w3.org/TR/xhtml1/">XHTML 1.0</a>

document.</jorma>

</body>

</html>

Page 27: Introduction to XML

VALID XML DOCUMENTDefining the Structure for XML documents

Page 28: Introduction to XML

Valid XML

• XML document is valid if– 1) It is well formed AND– 2) It follows some semantic rules

• XML document is usually linked to an external file, that has semantic rules for the document.– The file can be dtd (.dtd) or schema (.xsd)

• Semantic rules?– Name of tags, order of elements

Page 29: Introduction to XML

DTD Linking<?xml version="1.0"?>

<!DOCTYPE html

PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Minimal XHTML 1.0 Document</title>

</head>

<body>

<p>This is a minimal <a href="http://www.w3.org/TR/xhtml1/">XHTML 1.0</a>

document.</p>

</body>

</html>

Rules for XHTML elements (order,

names, etc)

Rules for XHTML elements (order,

names, etc)

Page 30: Introduction to XML

DTD Linking

Defines the structure, tag names andorder for all xhtml - documents

W3C has created XML-language "XHTML"by defining it's rules in DTD.

W3C has created XML-language "XHTML"by defining it's rules in DTD.

Page 31: Introduction to XML

Is this valid XML Document?<?xml version="1.0"?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Minimal XHTML 1.0 Document</title>

</head>

<body>

<jorma>This is a minimal <a href="http://www.w3.org/TR/xhtml1/">XHTML 1.0</a>

document.</jorma>

</body>

</html>

1. There is no DTD! What language is this? MathML? SVG? XHTML?2. Assuming this is XHTML, what version of XHTML? Transitional? Strict?3. Assuming this is XHTML strict, does "jorma" – tag belong to XHTML Language?

1. There is no DTD! What language is this? MathML? SVG? XHTML?2. Assuming this is XHTML, what version of XHTML? Transitional? Strict?3. Assuming this is XHTML strict, does "jorma" – tag belong to XHTML Language?

Page 32: Introduction to XML

Invalid XHTML-document<?xml version="1.0"?>

<!DOCTYPE html

PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Minimal XHTML 1.0 Document</title>

</head>

<body>

<jorma>This is a minimal <a href="http://www.w3.org/TR/xhtml1/">XHTML 1.0</a>

document.</jorma>

</body>

</html>

Page 33: Introduction to XML

Validating with W3C Service

Page 34: Introduction to XML

Invalid XHTML in Browser?

May work... or not. Browser tries to detect the errors and tries to understandthem. If it works with one browser, are you certain that it works with all otherbrowsers? And with all the versions with the browsers? What about browsersin handheld devices?

And it might work now, but what about future? How will Firefox 5.0 handleincorrect web pages?

May work... or not. Browser tries to detect the errors and tries to understandthem. If it works with one browser, are you certain that it works with all otherbrowsers? And with all the versions with the browsers? What about browsersin handheld devices?

And it might work now, but what about future? How will Firefox 5.0 handleincorrect web pages?

Page 35: Introduction to XML

Invalid XML in General

• Because of HTML heritage, browsers try to understand invalid XHTML-pages

• This is not the case in other XML-languages. • In general, if XML-document is invalid, the

processing of the document is cancelled.

Page 36: Introduction to XML

Example: MathML and Open Office

Page 37: Introduction to XML

Open the Document in External Editor

Page 38: Introduction to XML

Modify and Save the Document

Break the XML file

Page 39: Introduction to XML

Open the Document

Page 40: Introduction to XML

Result

Nope.. It does not try to understand the errors inthe document. It does not handle the document at all.Nope.. It does not try to understand the errors inthe document. It does not handle the document at all.

Page 41: Introduction to XML

Benefits of WF and Valid

• XML has strict rules for WF and Valid• If application tries to manipulate xml-

document it does not have to try to understand the possible errors in the document

• This means that handling xml-files via programming language is much easier– If the document is correctly formed, manipulate it– If it isn't display error

Page 42: Introduction to XML

Case: TAMKOtuki• The menu of TAMKOtuki is saved into XML-

document:– http://php.tpu.fi/~pohjus/menu/index.php

• Since you can be sure, that the xml-document is well formed, reading the contents of the xml-document is fairly easy– Showing the contents in Web-page:

• http://tamkotuki.tamk.fi/en/ravintolat.php– Showing the contents in TAMK-intra:

• https://intra.tamk.fi– Showing the contents in Mobile Devices..