Tutorial.01

advertisement
TUTORIAL 1
XP
CREATING AN XML DOCUMENT
New Perspectives on XML, 2nd Edition
Tutorial 1
1
XP
Tutorial 1 project
1. Complete Case Problem 1: Jackson Electronics
– Include your name in the upper left hand corner
2. Post the completed project on your web site
3. Send an e-mail before Tuesday 11:59 pm
To: jim@larson-tech.om
Subject: Case Problem 1 by <your name>
Body: url to your completed project
New Perspectives on XML, 2nd Edition
Tutorial 1
2
Course project 1: Using an
existing XML vocabulary
XP
Goal: To use an existing XML vocabulary for implementing a computing project
in which you are interested
1. Review http://en.wikipedia.org/wiki/Xml_languages
2. Select an XML vocabulary that matches your interests
3. Familiarize yourself with XML vocabulary by reading its Wikipedia entry.
4. Determine if there is an XML vocabulary processor/interpreter available for
free (or possibly a 30-day free trial). You may need to do several Google
searches to identify and locate these processors
5. Prepare a classroom presentation that demonstrates the following
a. create the document using the XML vocabulary. You may use an editing
tool that generates the XML vocabulary code
b. Display the XML code
c. Render the document using the XML vocabulary processor/interpreter
New Perspectives on XML, 2nd Edition
Tutorial 1
3
Course project 1: Using an
existing XML vocabulary
Example Projects
XP
1. Use MathXML to create a tutorial for deriving the quadratic equation
2. Use MusicXML to create a document for an original music composition that
can be renderer as an audio file and displayed as a musical score
3. Use Scalable Vector Graphics (SVG) to display two-dimensional geometric
figures for use in a tutorial for calculating areas of rectangles, triangles, and
parallelograms
4. Use RDF to create Entity-Relationship description of a database that you
designed for your database management class
5. Use SCXML to create a state transition diagram for a software project that you
have worked on
6. Use DTTA to create documentation for a computer program you have written
New Perspectives on XML, 2nd Edition
Tutorial 1
4
XP
Course project 2: Define and use
an new XML vocabulary
Goal: To gain proficiency in using XML design and development tools
1. Define a new XML vocabulary by creating a CSS or SCHEMA
2. Create an XML document using that vocabulary
3. Validate the XML document using its DTD or SCHEMA
4. Using either CSS or XSLT, render the document on a laptop display
5. Using either CSS or XSLT, render the document on a small hand-held display
6. Using either CSS or XSLT, render the document as a PDF file.
New Perspectives on XML, 2nd Edition
Tutorial 1
5
Course project 2: Define and use
XP
an new XML vocabulary
Example Projects
1. Enter and display recipes (at least three yummy dishes)
2. Enter and display ancestry information including stories and antidotes
3. Enter and display three guided tours with pictures of points of interest in
Portland
4. Music genre including description and examples from YouTube
New Perspectives on XML, 2nd Edition
Tutorial 1
6
XP
Project Approval process
Write a proposal that includes:
1. Student Name(s)
2. Project Name
3. Goal of the project
4. Intended user of the project software
5. Software tools and languages to be used to implement the project
6. Estimated time in person-hours to (a) learn how to use the software tools and
languages (b) design the project, (c) implement and test the project
7. Learning goals: what do you hope to learn by completing this project
Send an e-mail before Tuesday 11:59 pm:
To: jim@larson-tech.om
Subject: Course project <n> proposal by <your name>
New Perspectives on XML, 2nd Edition
Tutorial 1
7
XP
Introductions
• Who is your instructor?
• Who you are?
– As an undergraduate, what was your major and
minor?
– What do you know about XML?
– Why are you taking this class?
New Perspectives on XML, 2nd Edition
Tutorial 1
8
XP
INTRODUCING 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.
New Perspectives on XML, 2nd Edition
Tutorial 1
9
XP
Extensibility
• Some extensions may require changes to the browser or
perhaps a different browser
– Existing browsers can render the document containing the
extension
• New data types that can be represented using existing presentation
styles
– New monetary unit, changing employee number from 7 to 9 digits
– Requires a new browser to interpret and render a document
• VoiceXML
• X3D
•
Use caution when using extensions
– May not be supported on the standard browsers
• Limit portability across browsers
New Perspectives on XML, 2nd Edition
Tutorial 1
10
XP
INTRODUCING 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?
New Perspectives on XML, 2nd Edition
Tutorial 1
11
XP
THE 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
New Perspectives on XML, 2nd Edition
Tutorial 1
12
XP
THE 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.
New Perspectives on XML, 2nd Edition
Tutorial 1
13
XP
THE 10 PRIMARY XML DESIGN
GOALS
1.
2.
XML must be easily usable over the Internet
XML must support a wide variety of applications
– Databases, word processing, spreadsheets, financial
transactions, e-mail, etc
3. XML must be compatible with SGML
4. 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
New Perspectives on XML, 2nd Edition
Tutorial 1
14
XP
THE 10 PRIMARY XML DESIGN
GOALS — CONTINUED
6.
XML documents should be clear and easily understood
by nonprogrammers
– Tree-like structure
– Meaningful elements names
7. The XML design should be prepared quickly
8. The design of XML must be exact and concise
9. XML documents must be easy to create
10. Terseness in XML markup is of minimum importance
New Perspectives on XML, 2nd Edition
Tutorial 1
15
XML VOCABULARIES
(a.k.a. XML application)
XP
http://en.wikipedia.org/wiki/Xml_languages
New Perspectives on XML, 2nd Edition
Tutorial 1
16
XP
WELL-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 inn a document
• Specified by developers or standards body
New Perspectives on XML, 2nd Edition
Tutorial 1
17
XP
Creating 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 embeded
invisible control characters
• May be created using any of several XML editing tools
New Perspectives on XML, 2nd Edition
Tutorial 1
18
THE STRUCTURE OF AN XMLXP
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
New Perspectives on XML, 2nd Edition
Tutorial 1
19
THE STRUCTURE OF AN XMLXP
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.
New Perspectives on XML, 2nd Edition
Tutorial 1
20
THE STRUCTURE OF AN XMLXP
DOCUMENT: CREATING 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
New Perspectives on XML, 2nd Edition
Tutorial 1
21
THE STRUCTURE OF AN XMLXP
DOCUMENT:
THE 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.
Do not use curley quotes!
• 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
New Perspectives on XML, 2nd Edition
Tutorial 1
22
THE STRUCTURE OF AN XMLXP
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
New Perspectives on XML, 2nd Edition
Tutorial 1
23
XP
ELEMENTS
• 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>
New Perspectives on XML, 2nd Edition
Tutorial 1
24
XP
ELEMENT 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>
New Perspectives on XML, 2nd Edition
Tutorial 1
25
XP
EMPTY ELEMENTS
• Empty element = element with no content
• May contain attributes
attribute
• HTML Example:
<br/>
<img src="url" alt="some_text"/>
• VoiceXML Example:
</break strength = “strong”>
New Perspectives on XML, 2nd Edition
Tutorial 1
26
XP
ELEMENTS
• 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>
New Perspectives on XML, 2nd Edition
Tutorial 1
27
XP
ELEMENTS AND ATTRIBUTES
• All elements must be nested within a single
document element, called the root element. There
can be only one root element.
New Perspectives on XML, 2nd Edition
Tutorial 1
28
XP
WORKING 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>
Do not use curley quotes!
New Perspectives on XML, 2nd Edition
Tutorial 1
29
XP
GWORKING WITH ATTRIBUTES
• Attribute name must begin with a letter or underscore
(_)
• Spaces are not allowed
• Do not begin with the test string “xml”
• An attribute name can appear only once within an
element
• Attribute names are case sensitive
A frequent error
New Perspectives on XML, 2nd Edition
Tutorial 1
30
ELEMENTS AND ATTRIBUTES: XP
ADDING ELEMENTS TO THE
JAZZ.XML FILE
This figure shows the revised document
New Perspectives on XML, 2nd Edition
Tutorial 1
31
XP
CHARACTER 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;
New Perspectives on XML, 2nd Edition
Tutorial 1
32
XP
CHARACTER REFERENCES
• 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.
New Perspectives on XML, 2nd Edition
Tutorial 1
33
XP
CHARACTER REFERENCES
This figure shows commonly used character reference numbers
New Perspectives on XML, 2nd Edition
Tutorial 1
34
XP
CHARACTER REFERENCES
This figure shows the revised Jazz.XML file
character
reference
New Perspectives on XML, 2nd Edition
Tutorial 1
35
XP
PARSED 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
New Perspectives on XML, 2nd Edition
Tutorial 1
36
XP
CDATA 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
] ]>
New Perspectives on XML, 2nd Edition
Tutorial 1
37
XP
CDATA 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>
New Perspectives on XML, 2nd Edition
Tutorial 1
38
XP
CDATA SECTIONS
This figure shows the revised Jazz.XML file
CDATA section
New Perspectives on XML, 2nd Edition
Tutorial 1
39
XP
PARSING AN XML DOCUMENT
New Perspectives on XML, 2nd Edition
Tutorial 1
40
XP
DISPLAYING 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.
New Perspectives on XML, 2nd Edition
Tutorial 1
41
XP
DISPLAYING AN XML
DOCUMENT IN A WEB BROWSER
New Perspectives on XML, 2nd Edition
Tutorial 1
42
XP
LINKING 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:
Also used by
– Cascading Style Sheets (CSS)
html
– Extensible Style Sheets (XSL)
documents
New Perspectives on XML, 2nd Edition
Tutorial 1
43
XP
LINKING 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
New Perspectives on XML, 2nd Edition
Tutorial 1
44
APPLYING A STYLE TO AN
ELEMENT
XP
• 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.
New Perspectives on XML, 2nd Edition
Tutorial 1
45
APPLYING A STYLE TO AN
ELEMENT
XP
• For example:
artist {color:red; font-weight:bold}
• will display the text of the artist element in a red
boldface type.
New Perspectives on XML, 2nd Edition
Tutorial 1
46
XP
CREATING 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.
New Perspectives on XML, 2nd Edition
Tutorial 1
47
CREATING PROCESSING
INSTRUCTIONS
XP
• 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.
New Perspectives on XML, 2nd Edition
Tutorial 1
48
XP
THE JW.CSS STYLE SHEET
This figure shows the cascading style sheet stored in the jw.css file
New Perspectives on XML, 2nd Edition
Tutorial 1
49
LINKING TO THE JW.CSS
STYLE SHEET
XP
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
New Perspectives on XML, 2nd Edition
Tutorial 1
50
THE JAZZ.XML DOCUMENT XP
FORMATTED WITH THE JW.CSS
STYLE SHEET
This figure shows the
formatted jazz.xml file
New Perspectives on XML, 2nd Edition
Tutorial 1
51
XP
White 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
New Perspectives on XML, 2nd Edition
Tutorial 1
52
Download