xml 2nd edition tutorial 1 creating an xml document

44
XML 2nd EDITION Tutorial 1 Creating An Xml Document

Upload: stephen-conley

Post on 01-Jan-2016

240 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XML2nd EDITION

Tutorial 1Creating An Xml Document

Page 2: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPIntroducing XML• XML stands for Extensible Markup Language. A

markup language specifies the structure and content of a document.

• Because it is extensible, XML can be used to create a wide variety of document types.

Page 3: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPIntroducing XML• XML is a subset of the Standard Generalized

Markup Language (SGML) which was introduced in the 1980s.– What is your experience with SGML?

• SGML is very complex and can be costly. • These reasons led to the creation of Hypertext

Markup Language (HTML), a more easily used markup language. – What is your experience with HTML?

Page 4: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPThe Limits of HTML• HTML was designed for formatting text on a Web

page. • It was not designed for dealing with the content of a

Web page. • HTML is not extensible. • Browser developers have added features to HTML– Non standard features decrease portability of HTML– Confusing mix of HTML features

Page 5: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPThe Limits of HTML• HTML cannot be applied consistently. • Different browsers require different standards– The final document be rendered differently on one

browser compared with another.

• HTML 5.0 is the latest standard specification being created by the World Wide Web Consortium– Still evolving– Browsers differ in how much of HTML 5.0 they support.

Page 6: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPThe 10 Primary XML Design Goals1. XML must be easily usable over the Internet2. XML must support a wide variety of applications– Databases, word processing, spreadsheets, financial

transactions, e-mail, etc…3. XML must be compatible with SGML4. It must be easy to write programs that process XML

documents– Many tools for developing and process XML documents

5. The number of optional features in XML must be kept to a minimum, ideally zero

Page 7: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXP

7

The 10 Primary XML Design Goals6. XML documents should be clear and easily

understood by nonprogrammers– Tree-like structure– Meaningful elements names

7. The XML design should be prepared quickly8. The design of XML must be exact and concise9. XML documents must be easy to create10. Terseness in XML markup is of minimum

importance

Page 8: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPWell-formed and Valid XML Documents• An XML document is well-formed if it contains no

syntax errors and fulfills all of the specifications for XML code as defined by the W3C.

• An XML document is valid if it is well-formed and also satisfies the rules laid out in the DTD or schema attached to the document.– DTD = Document Type Definition

• Both DTD and schema• Rules for how to structure data in a document• Specified by developers or standards body

Page 9: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPCreating an XML Document• Document– Description of data– Application (may require a special browser to

process)• Created using a text editor– Do not use Microsoft Word because of embedded

invisible control characters• May be created using any of several XML

editing tools

Page 10: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPThe Structure of an XML Document• XML documents consist of three parts– The prolog– The document body– The epilog

• The prolog is optional and provides information about the document itself

Page 11: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPThe Structure of an XML Document• The document body contains the document’s

content in a hierarchical tree structure.

• The epilog is also optional and contains any final comments or processing instructions.

Page 12: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPCreating the Prolog• The prolog consists of four parts in the

following order:– XML declaration– Miscellaneous statements or comments– Processing instructions– Document type declaration• May be embedded in the XML document• May reference an external document

Page 13: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPThe XML Declaration• First line of code in an XML document. – Tells the processor what follows is written using XML.– Provide any information about how the parser should

interpret the code.• The complete syntax is:

<?xml version=“version number” encoding=“encoding type” standalone=“yes | no” ?>

• A sample declaration might look like this: <?xml version=“1.0” encoding=“UTF-8” standalone=“yes” ?

>– Other encodings:

http://www.iana.org/assignments/character-sets

Page 14: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPInserting Comments• Comments or miscellaneous statements – Go after the declaration. – Comments may appear anywhere after the

declaration.• The syntax for comments is:

<!- - comment text - ->• This is the same syntax for HTML comments

Page 15: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPElements• Elements are the basic building blocks of XML files.• Elements contain an opening tag and a closing tag– Content is stored between tags

• A closed element, has the following syntax: <element_name>Content</element_name>

• Example: <Artist>Miles Davis</Artist>

Page 16: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPElement Names• Element names – Case sensitive– Must begin with letter or underscore character– May not contain blank spaces– Can not begin with “xml”– Name should appear in opening and closing tag

• Example: <Artist>Miles Davis</Artist>

Page 17: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPEmpty Elements• Empty element = element with no content• May contain attributes • HTML Example:

<br/> <img src="url" alt="some_text"/>

• VoiceXML Example:</break strength = “strong”>

Page 18: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPElements• Nested elements are called child elements.• Elements must be nested correctly. Child

elements must be enclosed within their parent elements.

• Example: <album>Kind of Blue

<track>So What (:22)</track><track>Blue in Green (5:37)</track>

</album>

Page 19: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPCharting the Number of Child Elements

New Perspectives on Microsoft PowerPoint 2010 19

Page 20: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXP

New Perspectives on Microsoft PowerPoint 2010 20

Page 21: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPElements and Attributes

• All elements must be nested within a single document element, called the root element. There can be only one root element.

Page 22: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPWorking with Attributes• An attribute is a feature or characteristic of an

element. Attributes are text strings and must be placed in single or double quotes. The syntax is:

<album attribute=“value”> … </element_name>

Page 23: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPWorking with Attributes• Attribute name must begin with a letter or

underscore (_)• Spaces are not allowed• Do not begin with the text string “xml”• An attribute name can appear only once within

an element• Attribute names are case sensitive

Page 24: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPAdding Elements to the jazz.xml file

This figure shows the revised document

Page 25: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPCharacter References• Special characters, such as the symbol for the British

pound, can be inserted into your XML document by using a character reference. The syntax is: &#nnn;

• Character is a character reference number or name from the ISO/IEC character set.http://en.wikipedia.org/wiki/ISO/IEC_8859-1

• Character references in XML are the same as in HTML.

Page 26: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPCharacter References

This figure shows commonly used character reference numbers

Page 27: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPCharacter References

This figure shows the revised Jazz.XML file

character reference

Page 28: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPParsed Character Data• Parsed character data, or pcdata consists of

all those characters that XML treats as parts of the code of XML document– The XML declaration– The opening and closing tags of an element– Empty element tags– Character or entity references– Comments

Page 29: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPCDATA Sections• A CDATA (character data) section is a large block of

text the XML processor will interpret only as text.

• The syntax to create a CDATA section is:

<! [CDATA [Text Block

] ]>

Page 30: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPCDATA Sections• In this example, a CDATA section stores several HTML

tags within an element named HTMLCODE:

<htmlcode> <![CDATA[ <h1>The Jazz Warehouse</h1> <h2>Your Online Store for Jazz Music</h2> ] ]> </htmlcode>

Page 31: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPCDATA Sections

This figure shows the revised Jazz.XML file

CDATA section

Page 32: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPParsing an XML Document

Page 33: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPDisplaying an XML Document in a Web Browser• XML documents can be opened in Internet

Explorer or in Netscape Navigator.

• If there are no syntax errors. IE will display the document’s contents in an expandable/collapsible outline format including all markup tags.

• Netscape will display the contents but neither the tags nor the nested elements.

Page 34: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXP

Page 35: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPLinking to a Style Sheet• Unlike HTML documents, XML documents do not

include formatting information• Need to link XML document to a style sheet to

format the document. • The XML processor will combine the style sheet with

the XML document and apply any formatting codes defined in the style sheet to display a formatted document.

• Style sheet languages used with XML:– Cascading Style Sheets (CSS) – Extensible Style Sheets (XSL)

Page 36: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPLinking to a Style Sheet• There are some important benefits to using

style sheets:– By separating content from format, you can

concentrate on the appearance of the document– Different style sheets can be applied to the same

XML document– Any style sheet changes will be automatically

reflected in any Web page based upon the style sheet

Page 37: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPApplying a Style to an Element • To create a style sheet to a document, use the

following syntax:

selector {attribute1:value1; attribute2:value2; …}

• selector is an element (or set of elements) from the XML document.

• attribute and value are the style attributes and attribute values to be applied to the document.

Page 38: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPApplying a Style to an Element • For example:

artist {color:red; font-weight:bold}

• will display the text of the artist element in a red boldface type.

Page 39: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPCreating Processing Instructions• The link from the XML document to a style

sheet is created using a processing statement.

• A processing instruction is a command that gives instructions to the XML parser.

Page 40: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPCreating Processing Instructions• For example:

<?xml-stylesheet type=“style” href=“sheet” ?>

• Style is the type of style sheet to access and sheet is the name and location of the style sheet.

Page 41: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPThe jw.css Style Sheet

This figure shows the cascading style sheet stored in the jw.css file

Page 42: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPThe jw.css Style Sheet

This figure shows how to link the JW.css style sheet to the Jazz.xml file

processing instruction to access the jw.css style sheet

Page 43: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPThe jazz.xml Document Formatted with The jw.css Style Sheet

This figure shows the formatted jazz.xml file

Page 44: XML 2nd EDITION Tutorial 1 Creating An Xml Document

XPXPXPXPXPWhite Space

• HTML strips white space<p> This is a paragraph </p>

is treated the same as <p> This is a paragraph </p>

• In XML<pragraph>This is a paragraph</paragraph>

is treated as This is a paragraph