new perspectives on xml tutorial 1 – creating an xml document

67
1 XP New Perspectives on XML Tutorial 1 – Creating an XML Document Jason C. H. Chen, Ph.D. Professor of MIS School of Business Administration Gonzaga University Spokane, WA 99223 USA [email protected] http://barney.gonzaga.edu/~chen

Upload: lucia

Post on 07-Jan-2016

59 views

Category:

Documents


3 download

DESCRIPTION

New Perspectives on XML Tutorial 1 – Creating an XML Document. Jason C. H. Chen, Ph.D. Professor of MIS School of Business Administration Gonzaga University Spokane, WA 99223 USA [email protected] http://barney.gonzaga.edu/~chen. Introducing XML. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: New Perspectives on XML Tutorial 1 – Creating an XML Document

1

XP

New Perspectives on XMLTutorial 1 – Creating an XML

Document

Jason C. H. Chen, Ph.D.Professor of MIS

School of Business AdministrationGonzaga University

Spokane, WA 99223 [email protected]

http://barney.gonzaga.edu/~chen

Page 2: New Perspectives on XML Tutorial 1 – Creating an XML Document

2

XPIntroducing 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: New Perspectives on XML Tutorial 1 – Creating an XML Document

3

XPOrigin of XML

XML

SGML

GML

Page 4: New Perspectives on XML Tutorial 1 – Creating an XML Document

4

XPIntroducing XML

• XML is a subset of a the Standard Generalized Markup Language (SGML) which was introduced in the 1980s. 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. XML can be seen as sitting between SGML and HTML – easier to learn than SGML, but more robust than HTML.

Page 5: New Perspectives on XML Tutorial 1 – Creating an XML Document

5

XPDifferences to HTML

Sto computer

Page 6: New Perspectives on XML Tutorial 1 – Creating an XML Document

6

XPDifferences to HTML

(Parser)

Page 7: New Perspectives on XML Tutorial 1 – Creating an XML Document

7

XPThe 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. Additional features have been added to HTML, but they do not solve data description or cataloging issues in an HTML document.

• Because HTML is not extensible, it cannot be modified to meet specific needs. Browser developers have added features making HTML more robust, but this has resulted in a confusing mix of different HTML standards.

• HTML cannot be applied consistently. Different browsers require different standards making the final document appear differently on one browser compared with another.

Page 8: New Perspectives on XML Tutorial 1 – Creating an XML Document

8

XPThe 10 Primary XML Design Goals

1. XML must be easily usable over the Internet• XML was developed with the Web in mind.

• XML supports major Web protocols such as HTTP and MIME

2. XML must support a wide variety of applications• XML can be used for other applications such as

databases, financial transactions, and voice mail

3. XML must be compatible with SGML• because XML is a subset of SGML, many software tools

developed for SGML cab be adapted to XML

Page 9: New Perspectives on XML Tutorial 1 – Creating an XML Document

9

XPThe 10 Primary XML Design Goals

4. It must be easy to write programs that process XML documents

• It is easy for nonprogrammers to write XML code

5. The number of optional features in XML must be kept small

• SGML supports a wide range of optional features and can be large and cumbersome.

• XML removed this aspect of SGML making it a more suitable Web-development tool

6. XML documents should be clear and easily understood

• Like HTML, XML documents are text files.• The contents of an XML document follow a tree-like

structure

Page 10: New Perspectives on XML Tutorial 1 – Creating an XML Document

10

XPThe 10 Primary XML Design Goals

7. The XML design should be prepared quickly• If the Web community adopted XML, it was going to

be a viable alternative to HTML• The W3C had to quickly settle on a design for XML

before competing standards emerged

8. The design of XML must be exact and concise• XML can be easily processed by computer programs

making it easy for programmers to develop programs

9. XML documents must be easy to create• for XML to be practical, XML documents must be as

easy to create as HTML documents

10. Terseness in XML markup is of minimal importance

Page 11: New Perspectives on XML Tutorial 1 – Creating an XML Document

11

XPXML Parsers

• An XML processor (also called XML parser) evaluates the document to make sure it conforms to all XML specifications for structure and syntax.

• XML parsers are strict. It is this rigidity built into XML that ensures XML code accepted by the parser will work the same everywhere.

• Microsoft’s parser is called MSXML and is built directly in IE versions 5.0 and above.

• Netscape developed its own parser, called Mozilla, which is built into version 6.0 and above.

Page 12: New Perspectives on XML Tutorial 1 – Creating an XML Document

12

XPThe Document Creation Process

This figure shows the document creation process

Page 13: New Perspectives on XML Tutorial 1 – Creating an XML Document

13

XPWorking with XML Applications

• XML has the ability to create markup languages, called XML applications. Many have been developed to work with specific types of documents.

• Each application uses a defined set of tag names called a vocabulary. This makes it easier to exchange information between different organizations and computer applications.

Page 14: New Perspectives on XML Tutorial 1 – Creating an XML Document

14

XPXML ApplicationsThis figure shows some XML applications

Page 15: New Perspectives on XML Tutorial 1 – Creating an XML Document

15

XPThe 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

• 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 16: New Perspectives on XML Tutorial 1 – Creating an XML Document

16

XPThe Structure of an XML Document:

Creating the Prolog

• The prolog consists of four parts in the following order:– XML declaration– Miscellaneous statements or comments– Document type declaration– Miscellaneous statements or comments

• This order has to be followed or the parser will generate an error message.

• None of these four parts is required, but it is good form to include them.

Page 17: New Perspectives on XML Tutorial 1 – Creating an XML Document

17

XPWell-Formed and Valid XML

Documents

• There are two categories of XML documents– Well-formed

– Valid

• 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 (Document Type Definition) or schema attached to the document.

Page 18: New Perspectives on XML Tutorial 1 – Creating an XML Document

18

XPWell-formedness Constraints

• Follow the syntax rules setup by W3C – XML 1.0 Specification (http://www.w3.org/TR/REC-xml)

• The first line must be a declaration– <? .. ?> Prolog declaration

Page 19: New Perspectives on XML Tutorial 1 – Creating an XML Document

19

XPWell-formedness constraints

for data document

• A unique Root Element must contain all the other elements

• Tags must use strict nesting

– Each element nests inside any enclosing elements properly

– Tags can't overlap

Page 20: New Perspectives on XML Tutorial 1 – Creating an XML Document

20

XP

• Naming conventions:– Names consist of one or more nonspace

characters. If the name is a single character, it must be a letter

– Names can only begin with a letter or underscore. Beyond the first character, any character can be used, including Unicode characters

– Element and attribute names are case-sensitive (this one trips up a lot of people)

Well-formedness Constraints for data document

Page 21: New Perspectives on XML Tutorial 1 – Creating an XML Document

21

XPThe Structure of an XML Document: The

XML Declaration

• The XML declaration is always the first line of code in an XML document. It tells the processor what follows is written using XML. It can also 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” ?>

Page 22: New Perspectives on XML Tutorial 1 – Creating an XML Document

22

XPXML Declaration

xml-stylesheet type=“text/xsl” href=“my.xsl”

Processing Instructions

Page 23: New Perspectives on XML Tutorial 1 – Creating an XML Document

23

XPExamples

Not Well Formed

<title>Tootsie</title>

<title>Jurassic Park</title>

<title>

Mission Impossible

</title>

Well Formed <videocollection>

<title>Tootsie</title>

<title>Jurassic Park</title>

<title>

Mission Impossible

</title>

</videocollection>Why not?

Page 24: New Perspectives on XML Tutorial 1 – Creating an XML Document

24

XPMore examples

Well Formed

<videocollection><title>Tootsie</title><title>Jurassic Park</title><title>

Mission Impossible

</title><title>Tootsie</title>

</videocollection>

Not Well Formed

<videocollection>

<title>

Tootsie

</videocollection>

</title>

Page 25: New Perspectives on XML Tutorial 1 – Creating an XML Document

25

XPMore examples

Not Well Formed

<CREW>Sydney Pollak

</crew>

<crew> Sydney Pollak</Crew>

Well Formed

<crew>Sydney Pollak

</crew>

Page 26: New Perspectives on XML Tutorial 1 – Creating an XML Document

26

XPWell-formedness Constraints

for data document

• Start tag with a mapping end tag

– Also allow EmptyElement

– e.g., <EmptyElement attribute=“ ” />

– <Cover_image Source=“leaves.gif” />

• Attributes are only allowed a single value enclosed in quotes, in start tag

– e.g., <Weight unit = ‘lb’>165</Weight>

Page 27: New Perspectives on XML Tutorial 1 – Creating an XML Document

27

XPMore examples

Well Formed

<title id="1">Tootsie

</title>

Not Well Formed

<title id="1>Tootsie

</title><title id=1>

Tootsie</title>

Page 28: New Perspectives on XML Tutorial 1 – Creating an XML Document

28

XPThe Structure of an XML Document:

Inserting 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 29: New Perspectives on XML Tutorial 1 – Creating an XML Document

29

XPElements and Attributes

• Elements are the basic building blocks of XML files.

• XML supports two types of elements:– Closed elements, and – empty elements

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

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

Page 30: New Perspectives on XML Tutorial 1 – Creating an XML Document

30

XPElements and Attributes

• Element names are case sensitive• Elements can be nested, as follows:

<CD>Kind of Blue<TRACK>So What ((:22)</TRACK>

<TRACK>Blue in Green (5:37)</TRACK>

</CD>

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

must be enclosed within their parent elements.

Page 31: New Perspectives on XML Tutorial 1 – Creating an XML Document

31

XPElements and Attributes

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

• An open or empty element is an element that contains no content. They can be used to mark sections of the document for the XML parser.

• 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:

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

Page 32: New Perspectives on XML Tutorial 1 – Creating an XML Document

32

XPElements and Attributes: Adding elements to

the Jazz.XML File

This figure shows the revised document

prolog

document elements{

Page 33: New Perspectives on XML Tutorial 1 – Creating an XML Document

33

XPXML Editors

This figure shows available XML editors

Page 34: New Perspectives on XML Tutorial 1 – Creating an XML Document

34

XPYour turn …

• Type in the code using Notepad• Click File then “Save As”• Change “Save as type“ from “*.txt” to “All Files”• Type in Jazz-1.xml on the “File name” prompt

See next slide

Page 35: New Perspectives on XML Tutorial 1 – Creating an XML Document

35

XPUse of Notepad as xml editor

Page 36: New Perspectives on XML Tutorial 1 – Creating an XML Document

36

XP

Page 37: New Perspectives on XML Tutorial 1 – Creating an XML Document

37

XPCharacter 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:

&#character;

• Character is a entity reference number or name from the ISO/IEC character set.

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

Page 38: New Perspectives on XML Tutorial 1 – Creating an XML Document

38

XPCharacter References

This figure shows commonly used character reference numbers

Page 39: New Perspectives on XML Tutorial 1 – Creating an XML Document

39

XPCharacter References

This figure shows the revised Jazz.XML file

character reference

Page 40: New Perspectives on XML Tutorial 1 – Creating an XML Document

40

XPYour turn …

• Modify Jazz-1.xml by adding Character References

• Save the new version as Jazz-2.xml

Page 41: New Perspectives on XML Tutorial 1 – Creating an XML Document

41

XP

Page 42: New Perspectives on XML Tutorial 1 – Creating an XML Document

42

XP Well-formedness constraints: Character Data

Sections - CDATA

• A CDATA section is a large block of text the XML processor will interpret only as text.

• Used to include characters that could be confused with markup delimiters, such as “<“ or “>”

• to be displayed as they are within the inner [ ]• The syntax to create a CDATA section is:

<! [CDATA [Text Block

] ]>

Page 43: New Perspectives on XML Tutorial 1 – Creating an XML Document

43

XPCDATA Sections: An Example

Page 44: New Perspectives on XML Tutorial 1 – Creating an XML Document

44

XPCDATA 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 45: New Perspectives on XML Tutorial 1 – Creating an XML Document

45

XPCDATA Sections

CDATA section

This figure shows the revised Jazz.xml file

Page 46: New Perspectives on XML Tutorial 1 – Creating an XML Document

46

XPYour turn …

• Modify Jazz-2.xml by creating CDATA section• Save the version as Jazz-3.xml

Page 47: New Perspectives on XML Tutorial 1 – Creating an XML Document

47

XP

Page 48: New Perspectives on XML Tutorial 1 – Creating an XML Document

48

XPDisplaying 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 49: New Perspectives on XML Tutorial 1 – Creating an XML Document

49

XPDisplaying an XML Document in a Web

Browser

• To display the Jazz.xml file in a Web browser:

1. Start the browser and open the Jazz.xml file located in the Tutorial.01/Tutorial folder of your Data Disk.

2. Click the minus (-) symbols.

3. Click the resulting plus (+) symbols.

Page 50: New Perspectives on XML Tutorial 1 – Creating an XML Document

50

XPDisplaying an XML Document in a Web

BrowserThis figure shows the revised Jazz.XML file as seen in Internet

Explorer 6.0 and Netscape 6.2

Netscape 7.2 can display the same result as I.E.

Page 51: New Perspectives on XML Tutorial 1 – Creating an XML Document

51

XP

Page 52: New Perspectives on XML Tutorial 1 – Creating an XML Document

52

XP

Page 53: New Perspectives on XML Tutorial 1 – Creating an XML Document

53

XPLinking to a Style Sheet

• The easiest way to turn an XML document into a formatted document is to link the document to a style sheet.

• The XML document and the style sheet are combined by the XML processor to display a single formatted document.

Page 54: New Perspectives on XML Tutorial 1 – Creating an XML Document

54

XPLinking to a Style Sheet

There are two main style sheet languages used with XML:– Cascading Style Sheets (CSS) and Extensible Style

Sheets (XSL)

• CSS is supported by most browsers and is relatively easy to learn and use.

• XSL is more powerful, but not as easy to use as CSS.

Page 55: New Perspectives on XML Tutorial 1 – Creating an XML Document

55

XPLinking 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 56: New Perspectives on XML Tutorial 1 – Creating an XML Document

56

XPApplying a Style to an Element

• To apply 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 57: New Perspectives on XML Tutorial 1 – Creating an XML Document

57

XPApplying 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 58: New Perspectives on XML Tutorial 1 – Creating an XML Document

58

XPCreating 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.

• 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 59: New Perspectives on XML Tutorial 1 – Creating an XML Document

59

XPLinking to the 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 60: New Perspectives on XML Tutorial 1 – Creating an XML Document

60

XPThe JW.css Style Sheet

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

Page 61: New Perspectives on XML Tutorial 1 – Creating an XML Document

61

XPFinal Turn …

Part I.• Modify Jazz-3.xml by creating Processing

Instruction• Add style sheet

<?xml-stylesheet type="text/css" href="JW.css" ?>• Save the final version as Jazz-4.xml Part II.• Create the JW.css style sheet (How?)• Part III.• Open the Jazz-4.xml from the Web Browser

Page 62: New Perspectives on XML Tutorial 1 – Creating an XML Document

62

XP

Page 63: New Perspectives on XML Tutorial 1 – Creating an XML Document

63

XPThe Jazz.xml Document Formatted with the

JW.css Style Sheet

This figure shows the formatted Jazz.XML file

Page 64: New Perspectives on XML Tutorial 1 – Creating an XML Document

64

XPAnother Run

• Jazz-4A.xml

• JW-A.css<?xml-stylesheet type="text/css" href="JW-A.css" ?>

ARTIST {display:block; font-size: 12pt; color: gold; font-style:italic; font-family:Times New Roman, Serif; margin-left: 12pt}

TRACK {display:list-item; font-size: 12pt; color:black; list-style-type: disc; font-family: Aria; margin-left: 25pt}

Page 65: New Perspectives on XML Tutorial 1 – Creating an XML Document

65

XPLast Run

• Jazz-4B.xml

• JW-B.css<?xml-stylesheet type="text/css" href="JW-B.css" ?>

Page 66: New Perspectives on XML Tutorial 1 – Creating an XML Document

66

XPSummary

• Separate content from presentation style.

• Data communication between applications.

Style 1

Style 2

Style N

Presentation 1

Presentation 1

Presentation N

XMLFILE … …

Page 67: New Perspectives on XML Tutorial 1 – Creating an XML Document

67

XP

• To be well formed– the first requirement for a XML document

• Conditions– Only one root element– Elements are well nested and mapped– Naming conventions– Use of Character Data Section– Use of Comments

Summary