extensible markup language (xml) objectives –introduction –xml overview through a practical...

72
eXtensible Markup Language (XML) Objectives – Introduction XML overview through a practical example XML syntax Elements and attributes to mark up data XML parser -- A well-formed XML document Markup text vs. character data CDATA sections XML namespace XML applications XML related technologies

Post on 19-Dec-2015

260 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

eXtensible Markup Language (XML)

• Objectives– Introduction

– XML overview through a practical example

– XML syntax

– Elements and attributes to mark up data

– XML parser -- A well-formed XML document

– Markup text vs. character data

– CDATA sections

– XML namespace

– XML applications

– XML related technologies

Page 2: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Introduction

• eXtensible Markup Language (XML)– A description of XML from W3C school:

XML is a cross-platform, software and hardware independent tool for transmitting information.

– Technology for creating markup languages

– Enables document authors to describe data of any type

– Allows creating new tags•HTML limits document authors to fixed tag set

•Example: <body>, <p>, <h1>……etc.

– A new tag, such as car type, compares to an HTML tag method

<carType>4-door</carType> vs. <h2>4-door</h2>

Page 3: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Introduction (cont.)How can XML be used?

• XML can Separate Data from HTML

– With XML, your data is stored outside your HTML.

– with HTML, data is stored inside your HTML. With XML, data can be stored in separate XML files.

– Concentrate on using HTML for data layout and display

– XML data can also be stored inside HTML pages as "Data Islands".

• XML is used to Exchange Data

– With XML, data can be exchanged between incompatible systems.

– Challenge: transmitting data in incompatible formats over the Internet

– Converting the data to XML can greatly reduce this complexity and create data that can be read by many different types of applications.

Page 4: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Introduction (cont.)How can XML be Used?

• XML and Business To Business (B2B)

– Financial information can be exchanged over the Internet.

– Expect to see a lot about XML and B2B in the near future.

– The main exchanging language for exchanging financial information between businesses over the Internet.

– A lot of interesting B2B applications are under development.

• XML can be used to Share Data

– Plain text files can be used to share data.

– XML in plain text format, a software- and hardware-independent.

– Easier to create data for different applications

– Easier to expand or upgrade a system to new operating systems, servers, applications, and new browsers. 

Page 5: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Introduction (cont.)How can XML be Used?

• XML can be used to Store Data

– Plain text files can be used to store data.

– XML can also be used to store data in files or in databases.

• XML can make your Data more Useful

– Your data is available to more users.

– Make your data available to other than only standard HTML browsers.

– Other clients and applications can access your XML files as data sources, like they are accessing databases.

– Data can be made available to all kinds of "reading machines" (agents), and it is easier to make your data available for blind people, or people with other disabilities.

Page 6: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Introduction (cont.)How can XML be Used?

• XML can be used to Create new Languages– XML is the mother of WAP and WML.

– The Wireless Markup Language (WML), used to markup Internet applications for handheld devices like mobile phones, is written in XML.

– Wireless Application Protocol (WAP)

• If Developers have Sense (Sky is the limit!)– If “we” DO have sense, all future applications will exchange

our data in XML

– Word processors, spreadsheet applications and databases that can read each other's data in a pure text format, without any conversion utilities in between.

– We can only pray that Microsoft and all the other software vendors will agree.

Page 7: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Introduction (cont.)

• An overview and structure diagram to present the idea of XML and its applications

XML document

XML Applications

Presentation

Of

XML Doc.

XSL, CSS, embedded in HTML… etc.

Page 8: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

An XML Example• XML does not DO anything• XML was not designed to DO anything.• XML was created to structure, store and to send information.• The following example is a note to Class from Wei, <note>

<to> TO: Class of Web Programming</to> <from> From : Wei</from> <heading> *** Reminder ***</heading> <body>Don't forget me this Friday!</body> </note>

• The note has a header and a message body. It also has sender and receiver information.

• But still, this XML document does not DO anything. It is just pure information wrapped in XML tags. Someone must write a piece of software to send, receive or display it.

Page 9: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

But, How to present the data in the XML file? Using eXtensible Stylesheet Language (XSL)

<?xml version = "1.0"?><xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"><xsl:template match = "note"> <head> <title><xsl:value-of select = "heading"/></title> </head>

<body bgcolor = "white"> <h1><xsl:value-of select = "from"/></h1><br></br>

<h1><xsl:value-of select = "to"/></h1> <h2><xsl:value-of select = "heading"/></h2>

<p style = "font-size: 20pt; color:#0000ff">xsl:value-of select = "body"/></p>

</body> </xsl:template></xsl:stylesheet>

XML

Presentation

Page 10: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Present data in XML file: with CSS

• With CSS (Cascading Style Sheets) you can define display information to an XML document

• In the XML file:

<?xml-stylesheet type="text/css" href=“example1_2.css"?>

• Same as normal CSS definitionnote{background-color: #ffffff;width: 100%;}

CSS file

XMLPresentation

Page 11: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Present data in XML file: XML in DataIsland • IE5.0 or higher, XML can be embedded within HTML

pages in Data Islands (http://www.w3.org/TR/2000/WD-DOM-Requirements-20001214/)

• Unofficial <xml> tag, declare in the tag <body>

<xml id="note" src="example1_3.xml"></xml>

• Data Islands can be bound to HTML elements <table> ,<span> , <div> through

attributes: “datasrc” & “datafld” , refers to the example

XMLPresentation

Page 12: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Introduction to XML Markup

• XML document (intro.xml)

– Marks up message as XML

– Commonly stored in text files

• Extension .xml

– Must contain exactly one root element

• Attempting to create more than one root element is erroneous

• All other elements must be within this root element

Page 13: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Outline

Fig. 5.1 Simple XML document containing a message.

Line numbers are not part of XML document. We include them for clarity.

Document begins with declaration that specifies XML version 1.0

Comments

Element message is child element of root element myMessage

1 <?xml version = "1.0"?>23 <!-- Fig. 5.1 : intro.xml -->4 <!-- Simple introduction to XML markup -->56 <myMessage>7 <message>Welcome to XML!</message>8 </myMessage>

Line numbers are not part of XML document. We include them for clarity.

Document begins with declaration that specifies XML version 1.0

Comments

Element message is child element of root element myMessage

<root>

<child>

<subchild>.....</subchild>

</child>

</root>

Page 14: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML syntax

• The syntax rules of XML are very simple and very strict.

• The rules are very easy to learn, and very easy to use.

• An example:<?xml version="1.0" encoding="ISO-8859-1"?>

<note>

<to>To: Class of Web Programming (II)</to>

<from>From: Wei</from>

<heading>*** Reminder ***</heading>

<body>Don't forget me this Friday!</body>

</note>

XML version &Char. Encoding (Latin-1/West

European) root

element

Child elements

Data content

Page 15: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML Syntax (cont.)

• All XML elements – Consists of

• Start tag: <message>

• Content(may or may not) : Welcome to XML!

• End tag : </message>

– All elements must have corresponding end tagExample: <img src = “img.gif”>

is correct in HTML, but not XML

– XML requires end tag or forward slash (/) for termination<img src = “img.gif”></img>

or<img src = “img.gif”/>

is correct XML syntax

Page 16: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML Syntax (cont.)

• XML tags are case sensitive – The tag <Letter> is different from the tag <letter>

– Opening and closing tags must therefore be written with the same case:

<Message>This is incorrect</message>

<message>This is correct</message>

• All XML elements must be properly nested – Improper nesting of tags makes no sense to XML

• Incorrect: <x><y>hello</x></y>• Correct: <x><y>hello</y></x>

Page 17: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML Syntax (cont.)• Comments in XML

– The syntax for writing comments in XML is similar to that of HTML.<!-- This is a comment -->

• There is nothing special about XML – It is just plain text with the addition of some XML tags enclosed

in angle brackets, like HTML• An software that can handle plain text can also handle XML. • In a simple text editor, the XML tags will be visible and will

not be handled specially. • In an XML-aware application, however, the XML tags can be

handled specially. i.e. Spy, .net • The tags may or may not be visible, or have a functional

meaning, depending on the nature of the application.

Page 18: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Markup Elements

• XML Elements are extensible – Authors are free to extend the elements without worrying the

crash of document– Example: add 2 or 3 tags to the example1_1.xml

• XML Elements have relationships– Elements are related as parents and children

Example: the content of a book:

How I Play GolfIntroduction to golf What is golf What are clubs Short Game What is short game Which club should be used

Page 19: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Markup Elements (cont.)

<book><<titletitle>How I Play Golf</>How I Play Golf</titletitle>><<prodprod id="33-657" media="paper"></ id="33-657" media="paper"></prodprod>><<chapterchapter>Introduction to golf>Introduction to golf

<<parapara>What is golf<>What is golf</para/para>><<parapara>What are clubs<>What are clubs</para/para>>

<</chapter/chapter>><<chapterchapter>Short Game>Short Game

<<parapara>What is short game<>What is short game</para/para>><<parapara>Which club should be used<>Which club should be used</para/para>>

<</chapter/chapter>>

</book>

Root element?

child elements?

Parent element?

Sibling elements?

Page 20: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Markup Elements (cont.)

• Element Content

– Element content, mixed content, simple content, or empty content

• Element Naming Rules

– 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 ..)

– Names cannot contain spaces

Page 21: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Markup Elements (cont.)

• Element Naming Rules (cont.)– Any name can be used, no words are reserved, but the idea

is to make names descriptive

– Element names can be as long as you like

– Non-English letters like éòá are perfectly legal in XML element names, but watch out for problems if your software vendor doesn't support them

– The ":" should not be used in element names because it is reserved to be used for something called namespaces

– Avoid to use “-” and “.”, these are normally used for specific purpose by software vendors

book-title, book.title

Page 22: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Markup Element’s Attributes

• XML elements can have attributes in the start tag

<book isbn=“0-13-028417-3”>

• Attributes are used to provide additional information about elements

• Values are enclosed in quotes: either single or double

– Element car contains attribute doors, which has value “4”

<car doors = “4”/>

Page 23: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Markup Element’s Attributes (cont.)

• Use of Elements vs. Attributes– Data can be stored in child elements or in attributes

<person sex = “female”>

<firstname>Anna</firstname>

<lastname>Smith</lastname>

</person>

<person>

<sex>female</sex>

<firstname>Anna</firstname>

<lastname>Smith</lastname>

</person>

Page 24: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Markup Element’s Attributes (cont.)

• Avoid using attributes?

– attributes cannot contain multiple values (child elements can)

– attributes are not easily expandable (for future changes)

– attributes cannot describe structures (child elements can)

– attributes are more difficult to manipulate by program code

– attribute values are not easy to test against a Document Type Definition (DTD) - which is used to define the legal elements of an XML document

Page 25: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Markup Element’s Attributes (cont.)

• Processing instruction (PI)– The <xsl:processing-instruction> element writes a

processing instruction to the output

PI: <xsl:processing-instruction name="xml-stylesheet"> href="style.css" type="text/css" </xsl:processing-instruction>

Result: <?xml-stylesheet href="style.css" type="text/css"?>

– Passed to application using XML document

– Provides application-specific document information

– Delimited by <? and ?>– Refers to Ch.12 eXtensible Stylsheet Language (XSL)

Page 26: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Outline

Fig. 5.5 XML document that marks up information about a fictitious book.

Processing instruction specifies stylesheet (discussed in Chapter 12)

Root element book contains child elements title, author, chapters and media

Element book contains attribute isbn, which has value of 999-9999-9-X

Element chapters contains four child elements, each which contain two attributes

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 5.5 : usage.xml -->

4 <!-- Usage of elements and attributes -->

5

6 <?xml:stylesheet type = "text/xsl" href = "usage.xsl"?>

7

8 <book isbn = "999-99999-9-X">

9 <title>Deitel&amp;s XML Primer</title>

10

11 <author>

12 <firstName>Paul</firstName>

13 <lastName>Deitel</lastName>

14 </author>

15

16 <chapters>

17 <preface num = "1" pages = "2">Welcome</preface>

18 <chapter num = "1" pages = "4">Easy XML</chapter>

19 <chapter num = "2" pages = "2">XML Elements?</chapter>

20 <appendix num = "1" pages = "9">Entities</appendix>

21 </chapters>

22

23 <media type = "CD"/>

24 </book>

Root element book contains child elements title, author, chapters and media

Processing instruction specifies stylesheet (discussed in Chapter 12)

Element book contains attribute isbn, which has

value of 999-99999-9-X

Element chapters contains four child elements, each

which contain two attributes

Page 27: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Fig. 5.5 XML document that marks up information about a fictitious book.

Page 28: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Outline

Fig. 5.6 XML document that marks up a letter.

1 <?xml version = "1.0"?>23 <!-- Fig. 5.6: letter.xml -->

4 <!-- Business letter formatted with XML -->

56 <letter>

78 <contact type = "from">

9 <name>Jane Doe</name>10 <address1>Box 12345</address1>

11 <address2>15 Any Ave.</address2>

12 <city>Othertown</city>13 <state>Otherstate</state>14 <zip>67890</zip>

15 <phone>555-4321</phone>

16 <flag gender = "F"/>17 </contact>

1819 <contact type = "to">

20 <name>Jane Doe</name>21 <address1>123 Main St.</address1>

22 <address2></address2>23 <city>Anytown</city>

24 <state>Anystate</state>

25 <zip>12345</zip>26 <phone>555-1234</phone>27 <flag gender = "M"/>

28 </contact>

29

Page 29: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Outline

Fig. 5.6 XML document that marks up a letter. (Part 2)

30 <salutation>Dear Sir:</salutation>

31

32 <paragraph>It is our privilege to inform you about our new

33 database managed with <bold>XML</bold>. This new system

34 allows you to reduce the load on your inventory list

35 server by having the client machine perform the work of

36 sorting and filtering the data.</paragraph>

37

38 <paragraph>The data in an XML element is normalized, so

39 plain-text diagrams such as

40 /---\

41 | |

42 \---/

43 will become gibberish.</paragraph>

44

45 <closing>Sincerely</closing>

46 <signature>Ms. Doe</signature>

47

48 </letter>

Page 30: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Fig. 5.6 XML document that marks up a letter.

Page 31: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML Validation

• XML with correct syntax is Well Formed XML.

• XML validated against a Document Type Definition (DTD is Valid XML)

• A DTD defines the legal elements of an XML document

• XML Schema is an XML based alternative to DTD

• Errors will Stop you XML Parsing Error

Page 32: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML Validation (cont.)

• XML Browser Support

– XML in Mozilla Firefox 1.0: supports the XML 1.0 standard

– XML in Netscape 6: Netscape 6 supports XML

– XML in Internet Explorer 5.0 : supports the XML 1.0 standard

Page 33: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML Validation (cont.)

• IE 5.0 has the following XML support:

– Viewing of XML documents

– Full support for W3C DTD standards

– XML embedded in HTML as Data Islands

– Binding XML data to HTML elements

– Transforming and displaying XML with XSL

– Displaying XML with CSS

– Access to the XML DOM

Page 34: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Parsers and Well-formed XML Documents

• XML parser– To read and update - create and manipulate - an

XML document, you need an XML parser

– Processes XML document• Reads XML document

• Checks syntax

• Reports errors (if any)

• Allows programmatic access to document’s contents

• Example (by Javascript)

var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")

Create an XML document object

XMLPresentation

Page 35: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Parsers and Well-formed XML Documents (cont.)

• XML document syntax

– Considered well formed if syntactically correct

• Single root element

• Each element has start tag and end tag

• Tags properly nested

• Attribute (discussed before) values in quotes

• Proper capitalization

– Case sensitive

Page 36: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Parsers and Well-formed XML Documents (cont.)

• XML parsers support

– Document Object Model (DOM, heavy-weight API)

• Builds tree structure containing document data in memory

– Simple API for XML (SAX, light-weight API)

• Generates events when tags, comments, etc. are encountered

– (Events are notifications to the application)

– More like event-driven

Page 37: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Parsing an XML Document with msxml

• Load XML document into Internet Explorer 5.0– Document is parsed by msxml.(from Microsoft)

– Places plus (+) or minus (-) signs next to container elements• Plus sign indicates that all child elements are hidden

• Clicking plus sign expands container element

– Displays children

• Minus sign indicates that all child elements are visible

• Clicking minus sign collapses container element

– Hides children

– Error generated, if document is not well formed

Examples

Page 38: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML document shown in IE5.

Page 39: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Fig. 5.3 Error message for a missing end tag.

Error Message

Page 40: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Characters

• Character set

– Characters that may be represented in XML document

• e.g., ASCII character set

– Letters of English alphabet

– Digits (0-9)

– Punctuation characters, such as !, - and ?

Page 41: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

White Space, Entity References and Built-in Entities

• Whitespace characters– Spaces, tabs, line feeds and carriage returns

• Significant (preserved by application)

• Insignificant (not preserved by application)

– Normalization

» Whitespace collapsed into single whitespace character

» Sometimes whitespace removed entirely

<markup>This is character data</markup>

after normalization, becomes

<markup>This is character data</markup>

Page 42: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

White Space, Entity References and Built-in Entities (cont.)

• XML-reserved characters– Ampersand (&)

– Left-angle bracket (<)

– Right-angle bracket (>)

– Apostrophe (’)

– Double quote (”)

• Entity references– Allow to use XML-reserved characters

• Begin with ampersand (&) and end with semicolon (;)

– Prevents from misinterpreting character data as markup

Page 43: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

White Space, Entity References and Built-in Entities (cont.)

• Build-in entities

–Ampersand (&amp;)

–Left-angle bracket (&lt;)

–Right-angle bracket (&gt;)

–Apostrophe (&apos;)

–Quotation mark (&quot;)

• Mark up characters “<>&” in element message

<message>&lt;&gt;&amp;</message>

&lt; <

&gt; >

&amp; &

&apos; '

&quot; "

Page 44: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Character Set

• XML documents may contain

– Carriage Return (CR + LF) returns Line feeds (LF)

– Unicode characters (Section 5.5.4)

• Enables computers to process characters for several languages

• Use editor that support the encoding

• More about unicode specification, please visit:

http://www.unicode.org

Page 45: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Using Unicode in an XML Document

• XML Unicode support– e.g., Fig. 5.4 displays Arabic words

• Arabic characters

– represented by entity references for Unicode characters

Page 46: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Outline

Fig. 5.4 XML document that contains Arabic words

Document type definition (DTD) defines document structure and entities

Root element welcome contains child elements from and subject

Sequence of entity references for Unicode characters in Arabic alphabet

lang.dtd defines entities assoc and text

1 <?xml version = "1.0"?>23 <!-- Fig. 5.4 : lang.xml -->4 <!-- Demonstrating Unicode -->56 <!DOCTYPE welcome SYSTEM "lang.dtd">78 <welcome>9 <from>1011 <!-- Deitel and Associates -->12 &#1583;&#1575;&#1610;&#1578;&#1614;&#1604;13 &#1571;&#1606;&#1583;1415 <!-- entity -->16 &assoc; 17 </from>1819 <subject>2021 <!-- Welcome to the world of Unicode -->22 &#1571;&#1607;&#1604;&#1575;&#1611; 23 &#1576;&#1603;&#1605;24 &#1601;&#1610;&#1616;25 &#1593;&#1575;&#1604;&#1605; 2627 <!-- entity -->28 &text; 29 </subject>30 </welcome>

Document type definition (DTD) defines document

structure and entities

Root element welcome contains child elements from and subject

Sequence of entity references for Unicode characters in

Arabic alphabet

lang.dtd defines entities assoc and text

Page 47: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Fig. 5.4 XML document that contains Arabic words.

Show Example

Page 48: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Characters vs. Markup

• XML must differentiate between

– Markup text

• Enclosed in angle brackets (< and >)

– e.g,. <message>

– Character data

• Text between start tag and end tag

– e.g., Fig. 5.1, line 7: Welcome to XML!

Page 49: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

CDATA Sections

• Parsing or not Parsing

(CDATA section)

• CDATA sections: <![CDATA[…………………………. ]]>

– Everything inside a CDATA section is ignored by the parser

– May contain text, reserved characters and whitespace

• Reserved characters need not be replaced by entity references

– A CDATA section cannot contain the string "]]>", nested CDATA sections are not allowed

– Commonly used for scripting code (e.g., JavaScript)

Page 50: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Outline

Fig. 5.7 Using a CDATA section.

Entity references required if not in CDATA section

XML does not process CDATA section

Note the simplicity offered by CDATA section

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 5.7 : cdata.xml -->

4 <!-- CDATA section containing C++ code -->

5

6 <book title = "C++ How to Program" edition = "3">

7

8 <sample>

9 // C++ comment

10 if ( this-&gt;getX() &lt; 5 &amp;&amp; value[ 0 ] != 3 )

11 cerr &lt;&lt; this-&gt;displayError();

12 </sample>

13

14 <sample>

15 <![CDATA[

16

17 // C++ comment

18 if ( this->getX() < 5 && value[ 0 ] != 3 )

19 cerr << this->displayError();

20 ]]>

21 </sample>

22

23 C++ How to Program by Deitel &amp; Deitel

24 </book>

XML does not process CDATA section

Entity references required if not in CDATA section

Note the simplicity offered by CDATA section

Page 51: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Fig. 5.7 Using a CDATA section

Page 52: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML Namespaces• XML Namespaces provide a method to avoid element name

conflicts • Naming collisions

– Two different elements have same name<subject>Math</subject><subject>Thrombosis</subject>

• Solving Name Conflicts using a Prefix • Namespaces

– Differentiate elements that have same name <school:subject>Math</school:subject><medical:subject>Thrombosis</medical:subject>

school and medical are namespace prefixes• Prepended to elements and attribute names• Tied to uniform resource identifier (URI)

– Series of characters for differentiating names

Page 53: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML Namespaces (cont.)

<h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr></h:table>

<f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length></f:table>

Page 54: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML Namespaces (cont.)

• Uniform Resource Identifier (URI)

– A string of characters which identifies an Internet Resource.

– The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address

• Note that the address used to identify the namespace, is not used by the parser to look up information.

• The only purpose is to give the namespace a unique name to be identified

Page 55: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML Namespaces (cont.)

• Creating namespaces:

– Use xmlns keyword

xmlns:text = “urn:deitel:textInfo”

xmlns:image = “urn:deitel:imageInfo”

• Creates two namespace prefixes text and image

• urn:deitel:textInfo is URI for prefix text

• urn:deitel:imageInfo is URI for prefix image

– Default namespaces

• Child elements of this namespace do not need prefix

xmlns = “urn:deitel:textInfo”

Page 56: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Outline

Fig. 5.8 Listing for namespace.xml.

Element directory contains two namespace prefixes

Use prefix text to describe elements file and description

Apply prefix image to describe elements file, description and size

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 5.8 : namespace.xml -->

4 <!-- Namespaces -->

5

6 <directory xmlns:text = "urn:deitel:textInfo"

7 xmlns:image = "urn:deitel:imageInfo">

8

9 <text:file filename = "book.xml">

10 <text:description>A book list</text:description>

11 </text:file>

12

13 <image:file filename = "funny.jpg">

14 <image:description>A funny picture</image:description>

15 <image:size width = "200" height = "100"/>

16 </image:file>

17

18 </directory>

Element directory contains two namespace prefixes

Use prefix text to describe elements file

and description

Apply prefix image to describe elements file, description and size

Page 57: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

Outline

Fig. 5.9 Using default namespaces.

urn:deitel:text-Info is default namespace

Element file is in default namespace

Specify namespace

1 <?xml version = "1.0"?>

2

3 <!-- Fig. 5.9 : defaultnamespace.xml -->

4 <!-- Using Default Namespaces -->

5

6 <directory xmlns = "urn:deitel:textInfo"

7 xmlns:image = "urn:deitel:imageInfo">

8

9 <file filename = "book.xml">

10 <description>A book list</description>

11 </file>

12

13 <image:file filename = "funny.jpg">

14 <image:description>A funny picture</image:description>

15 <image:size width = "200" height = "100"/>

16 </image:file>

17

18 </directory>

urn:deitel:textInfo is default namespace

Element file is in default namespace

Specify namespace

Page 58: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

An XML Application• A small framework for an XML application

1. XML document

2. Load the document into a Data Island

3. Bind the Data Island to an HTML Table

4. Bind the Data Island to <span> or <div> elements

5. Add a Navigation Script to your XML

Page 59: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

An XML Application (cont.)

• With a little creativity you can create a full application.

• If you use what you have learned on this page, and a little imagination, you can easily develop this into a full application

• Now, let’s see what we can do with those techniques we just learned?

Demo

Page 60: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

HW # 1

• P. 132 Exercises 5.4 , 5.5 , 5.6

• Due day: Before next lecture (3/11)

• Submit your homework# 1 to TAs

• How to submit? Will be posted on the web page• http://140.116.246.95/Web%20programing/WebPro

gramming.htm

Page 61: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

That’s it for today,Have a nice weekend!

Page 62: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

An XML Application: 1. XML document

• First we start with a simple XML document.• An original demonstration document, the CD

catalog

<?xml version="1.0" encoding="ISO-8859-1"?><CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD>..... more ....

Page 63: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

An XML Application Load the document into a Data Island

• A Data Island can be used to access the XML file.

• To get your XML document "inside" an HTML page, add an XML Data Island to the page.

• The XML file "cd_catalog.xml" will be loaded into an "invisible" Data Island called "xmldso".

• The async="false" attribute is added to the Data Island to make sure that all the XML data is loaded before any other HTML processing takes place.

<xml src="cd_catalog.xml" id="xmldso" async="false"></xml>

Page 64: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

An XML Application: Bind the Data Island to an HTML Table

• An HTML table can be used to display the XML data.

• To make your XML data visible on your HTML page, you must "bind" your XML Data Island to an HTML element.

• To bind your XML data to an HTML table, add an attribute <table datasrc="#xmldso" ….>

• Add data field attributes: <span datafld="TITLE" >

Page 65: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

An XML Application: Bind the Data Island to an HTML Table (cont.)

<table datasrc="#xmldso" width="100%" border="1"><thead><th>Title</th><th>Artist</th><th>Year</th></thead><tr align="left"><td><span datafld="TITLE"></span></td><td><span datafld="ARTIST"></span></td><td><span datafld="YEAR"></span></td></tr></table>

Show

Page 66: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

An XML Application: Bind the Data Island to <span> or <div> elements

• <span> or <div> elements can be used to display XML data.

• You don't have to use a table to display your XML data.

• Data from a Data Island can be displayed anywhere on an HTML page by adding some <span> or <div> elements to your page.

• Use the data source attribute to bind the elements to the Data Island, and the data field attribute to bind each element to an XML element, like this

<span datasrc="#xmldso" datafld="TITLE">

<div datasrc="#xmldso" datafld="TITLE">

Page 67: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

An XML Application: Bind the Data Island to <span> or <div> elements (cont.)

<br />Title:<span datasrc="#xmldso" datafld="TITLE"></span><br />Artist:<span datasrc="#xmldso" datafld="ARTIST"></span><br />Year:<span datasrc="#xmldso" datafld="YEAR"></span>

<br />Title:<div datasrc="#xmldso" datafld="TITLE"></div><br />Artist:<div datasrc="#xmldso" datafld="ARTIST"></div><br />Year:<div datasrc="#xmldso" datafld="YEAR"></div>

Show

Page 68: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

An XML Application: Add a Navigation Script to your XML

• Navigation has to be performed by a script.

• Add navigation to the XML Data Island, create a script that calls the movenext() and moveprevious() methods of the Data Island

<script type="text/javascript">

function movenext(){

x=xmldso.recordset

if (x.absoluteposition < x.recordcount){

x.movenext()

}

}

function moveprevious(){

x=xmldso.recordset

if (x.absoluteposition > 1){

x.moveprevious()

}

}

</script>

Show

Page 69: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML Related Technologies

• XHTML - Extensible HTML– XHTML is the re-formulation of HTML 4.01 in XML.

XHTML 1.0 is the latest version of HTML

• CSS - Cascading Style Sheets– CSS style sheets can be added to XML documents to provide

display information

• XSL - Extensible Style Sheet Language– XSL consists of three parts: XML Document Transformation

(renamed XSLT, see below), a pattern matching syntax (renamed XPath, see below), and a formatting object interpretation

Page 70: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML Related Technologies (cont.)

• XSLT - XML Transformation– XSLT is far more powerful than CSS. It can be used to

transform XML files into many different output formats

• XPath - XML Pattern Matching– XPath is a language for addressing parts of an XML

document. XPath was designed to be used by both XSLT and XPointer

• XLink - XML Linking Language– The XML Linking Language (XLink) allows elements to be

inserted into XML documents in order to create links between XML resources

Page 71: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML Related Technologies (cont.)

• XPointer - XML Pointer Language– The XML Pointer Language (XPointer) supports addressing

into the internal structures of XML documents, such as elements, attributes, and content

• XQL - XML Query Language– The XML Query Language supports query facilities to

extract data from XML documents

Page 72: EXtensible Markup Language (XML) Objectives –Introduction –XML overview through a practical example –XML syntax –Elements and attributes to mark up data

XML Examples