+ All Categories
Transcript
Page 1: Introduction to XML

1

Introduction to XML

Babak Esfandiari

Page 2: Introduction to XML

2

What is XML?

introduced by W3C in 98 Stands for eXtensible Markup Language it is more general than HTML, but simpler than

SGML it is used to to describe metadata you can define your own set of tags!

– an XML document does not “do” anything on its own

Page 3: Introduction to XML

3

XML example

<?xml version="1.0" ?>

<!-- a simple tagset for museums>

<Museum name="Louvre">

<city> Paris

</city>

</Museum>

Page 4: Introduction to XML

4

XML - what for?

content is independent from rendering meta-data makes search easier standard tags enable data interchange across

tools format for data and object persistence, human

readable and editable no need for a custom parser anymore

Page 5: Introduction to XML

5

XML concepts and syntax

Elements– can be nested– must have a closing tag

Attributes XML declaration Comments

Page 6: Introduction to XML

6

XML concepts (2)

An XML document that follows the syntax rules is considered well-formed

But there is no restriction on the nature, order and number of tags in a well-formed XML document!– in order to impose some restrictions, you need to

define validity criteria in a separate document…

Page 7: Introduction to XML

7

DTD

Document Type Definition Describes the XML tagset

<!DOCTYPE Museum [ <!ELEMENT Museum (city?)> <!ATTLIST Museum name CDATA #REQUIRED> <!ELEMENT city (#PCDATA)>]>

An XML document that is compliant to its DTD is valid

Page 8: Introduction to XML

8

DTD Syntax

Defining elements:

<!ELEMENT Museum (city?, genre+) Character data types:

– #PCDATA (is parsed)– #CDATA (is not parsed)

Page 9: Introduction to XML

9

DTD

DTDs are hard to read DTD has its own syntax DTD has very limited support for data types

Page 10: Introduction to XML

10

XML Schema

a 2001 W3C recommendation allows the definition of elements and attributes

using the XML syntax supports many primitive types allows the creation of complex types uses namespaces to:

– allow reuse of types and schemas – avoid naming clashes

Page 11: Introduction to XML

11

XML Schema Example

http://chat.carleton.ca/~narthorn/project/community.xsd

Page 12: Introduction to XML

12

Some XML-based standards

MathML CML MusicXML XMI

Page 13: Introduction to XML

13

XMI Example

<Class>

<name>Museum</name>

<feature>

<Attribute>

<name>name</name>

</Attribute>

</feature>

</Class>

Page 14: Introduction to XML

14

XML Parsing

Many XML parsers are available: JAXP, XERCES… Two “standardized” parsing methods:

– SAX event-driven serial-access element-by-element processing

– DOM creates a tree structure of objects stores it in memory easier to navigate, but more memory needed

Page 15: Introduction to XML

15

SAX

good to use if you are “consuming” XML data from a stream

see Echo.java example (from JAXP)

Page 16: Introduction to XML

16

DOM

use it if you need “random access” to various elements of the document

see EchoDom.java example

Page 17: Introduction to XML

17

XSLT

eXtensible Stylesheet Language Templates allows the transformation of one XML

document into another by specifying transformation rules

Page 18: Introduction to XML

18

XSLT example

<xsl:stylesheet><xsl:template match="Class">

blah <xsl:value-of select="name"/> blah<xsl:apply-templates select="feature/Attribute"/>

</xsl:template><xsl:template match="feature/Attribute">

<xsl:value-of select="name"/></xsl:template></xsl:stylesheet>

Page 19: Introduction to XML

19

Semantic Web

Tim Berners-Lee’s idea of the future of the Web

The goal is to make information accessible to non-humans(ie agents)

Therefore information should be structured and use metadata– RDF is proposed as such structure

Page 20: Introduction to XML

20

RDF Example

See Software Agents course example

Page 21: Introduction to XML

21

Refs

W3C specs: http://www.w3.org/TR/REC-xml


Top Related