58
4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com by Norman Walsh and Tim Bray . Code and information provided by Tim Bray of Texturality on a XML-HTML converter and John Bower of Microsoft on VML are greatly appreciated.

4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 1

XML

Edward Chow

Some of the presentation material is adapted fromtutorial at msdn.microsoft.com/xml and articles at

xml.com by Norman Walsh and Tim Bray.

Code and information provided by Tim Bray of Texturality on a XML-HTML converter and John Bower

of Microsoft on VML are greatly appreciated.

Page 2: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 2

XML: eXtensible Markup Language

• A markup language for “richly” structured documents.• Structure documents with content info, and structural

info. Markups (tags) are used to specify the structural info.

• It was approved as a W3C recommendation in 2/10/98.• It was used as a meta language to specify other markup

languages, called XML applications.• The list of XML applications grows fast:

– SMIL (Synchronized Multimedia Integration Language)– MathML (Math Markup Language)– VML (Vector Graphic Markup Language)– E-commerce transactions, Server API, …

Page 3: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 3

A SMIL XML Document<smil><head>

<meta name="title" content="mix vincent with vod lecture" /><meta name="author" content="[email protected]" /><meta name="copyright" content="©1998" /><layout type="text/smil-basic-layout"></layout>

</head><body>

<par title="Compose multimedia using smil"><audio src="audio/vincent.ra" id="Soundtrack 1" title="Soundtrack 1" /><audio src="audio/vodpaper.ra" id="Soundtrack 2" title="Soundtrack 2" begin="2s" end="8s" /><video src="file:///d:/uccs/cs525/doc/videof/uccs.avi" id="videoclip 1" title="video clip 1" /><audio src="audio/kissingcamel.ra" id="Soundtrack 3" title="kissingcamel" begin="id(Soundtrack 1)(end)"/>

</par></body>

</smil>

Page 4: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 4

MathMLFor (a + b)2 <msup> <mfenced> <mrow> <mi>a</mi> <mo>+</mo> <mi>b</mi> </mrow> </mfenced> <mn>2</mn> </msup>

Page 5: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 5

VML<html xmlns:v="urn:schemas-microsoft-com:vml"><body><style>v\:* {behavior:url(#default#VML);}</style><v:shape style='top: 0; left: 0; width: 250; height: 250' stroke="true" strokecolor="red" strokeweight="2" fill="true" fillcolor="green" coordorigin="0 0" coordsize="175 175"><v:path v="m 8,65 l 72,65,92,11,112,65,174,65,122,100, 142,155,92,121,42,155,60,100 x e"/> </v:shape></body></html>

Page 6: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 6

How XML is used

XML

SMIL MathML VML

XML applications (Languages)

Specify

XML (Parser)Processor

(Specific)Application

XMLDocument

XML capable Server/Client

Text editor Perl ScriptsXML editor

Page 7: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 7

XML vs. HTML

• The tag semantics and tag set are “rather” fixed.• With CSS1 you can create your own tags.• The standard body adds and depreciates tags

slowly.• XML specifies neither semantics nor tag set.• It is a meta-language for describing markup

language, i.e., define tag set.• The semantics of an XML document will either

be defined by the applications process them or by the stylesheet.

Page 8: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 8

XML vs. SGML

• XML is a restrict form (application profile) of Standard Generalized Markup Language (SGML).

• The syntax of XML specified as Extended Backup-Naur Form (EBNF).

• Modern compiler technique makes the parsing of EBNF-based XML documents fast.

• The full blown SGML syntax is much more complex and a parser can not be easily made and SGML documents take longer time to process.

Page 9: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 9

XML Development Goals

• View XML documents quick and easy as HTML documents.

• Support wide-variety of applications: authoring, browsing, content analysis,…

• Compatible with SGML, allow easy conversion of SGML documents to XML.

• Easy to write programs that process XML documents. (2 weeks for CS graduate).

• Option features keep to minimum or zero.• XML documents should be human-legible and

reasonable clear. View with text editor.

Page 10: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 10

XML Development Goals (2)

• XML design can be prepare quickly.• XML design shall be formal and concise

must be expressed in EBNF, amendable to modern compiler tools and techniques.

• XML document shall be easy to create.• Terseness in XML markup is not

(minimal) important.

Page 11: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 11

How is XML Defined?

• Extensible Markup Language (XML) 1.0 Specification 10 February 1998, Tim Bray, Jean Paoli, C. M.

Sperberg-McQueen.

• XML Linking Language (XLink) 3 March 1998, Eve Maler, Steve DeRose

• XML Pointer Language (XPointer) 3 March 1998, Eve Maler, Steve DeRose

• Extensible Stylesheet Language (XSL) 16 December 1998, James Clark, Stephen Deach

• Namespaces in XML 14 January 1999, Tim Bray, Dave Hollander,Andrew Layman

Page 12: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 12

A Simple XML Document

<?xml version=“1.0” ?> <? For processing instruction

<oldjoke>

<burns>Say <quote>goodnight</quote>

Gracie.</burns>

<allen><quote>Goodnight, Gracie.</quote></allen>

<applause/> /> for empty element </oldjoke> which does not have end-tag

Page 13: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 13

Weather Report in XML

<weather-report> <date>March 25, 1998</date><time>08:00</time> <area> <city>Seattle</city> <state>WA</state> <region>West Coast</region> <country>USA</country> </area> <measurements>

<skies>partly cloudy</skies> <temperature>46</temperature> <wind> <direction>SW</direction> <windspeed>6</windspeed> </wind> <h-index>51</h-index> <humidity>87</humidity> <visibility>10</visibility> <uv-index>1</uv-index> </measurements>

</weather-report>

Page 14: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 14

On-Line Bidding Record<AUCTIONBLOCK><ITEM><TITLE>Risotto</TITLE> <ARTIST>Linda Mann</ARTIST> <DIMENSIONS>20x30 inches</DIMENSIONS> <MATERIALS>Oil</MATERIALS><YEAR>1995</YEAR> <BIDS><BID><PRICE>3200</PRICE>

<TIME>8:59:51 AM</TIME> <BIDDER>Paul</BIDDER>

<TIMESTAMP>10984</TIMESTAMP> </BID>

<BID><PRICE>3100</PRICE> <TIME>8:59:40 AM</TIME>

<BIDDER>John</BIDDER> <TIMESTAMP>10980</TIMESTAMP> </BID> <BID><PRICE>3000</PRICE> <TIME>3:27:08 PM</TIME> <BIDDER>opening price</BIDDER> <TIMESTAMP>10976</TIMESTAMP> </BID> </BIDS> <TIMESTAMP>10996</TIMESTAMP> </ITEM> </AUCTIONBLOCK>

Page 15: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 15

Stock Profolio Record in XML<?xml version="1.0"?><?xml-stylesheet type="text/xsl" href="portfolio.xsl"?><portfolio xmlns:dt="urn:schemas-microsoft-com:datatypes" xml:space="preserve"> <stock exchange="nyse"> <name>zacx corp</name> <symbol>ZCXM</symbol> <price dt:dt="number">28.875</price> </stock> <stock exchange="nasdaq"> <name>zaffymat inc</name> <symbol>ZFFX</symbol> <price dt:dt="number">92.250</price> </stock> <stock exchange="nasdaq"> <name>zysmergy inc</name> <symbol>ZYSZ</symbol> <price dt:dt="number">20.313</price> </stock></portfolio>

Page 16: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 16

Embed HTML in XML<?xml version="1.0" ?><!-- <?xml:stylesheet href="first-x.xsl" type="text/xsl" ?> --><?xml-stylesheet href="first-x.css" type="text/css" ?>

<article xmlns="http://redcloud.uccs.edu/~cs401/" xmlns:html="any-old-bollocks" ><html:h1>Test XML, name space, HMTL tag</html:h1><html:hr></html:hr>It seems to be critical to include<html:p> &lt;<no-op/>?xml-stylesheet href="first-x.css" type="text/css" ?></html:p>Otherwise it won't work.The following is the demonstration of &lt;<no-op/>html:ul> and &lt;<no-op/>html:li> tags.<html:ul><html:li>What is XML</html:li><html:li>How to create it</html:li></html:ul></article>

Page 17: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 17

Six Types of Markups in XML

• Elements: begin with start tag <element>, ends with end-tag </element>

• Entity references: special characters, repeated text, external file content

• Comments: <!-- …….. -->• Processing Instructions: to be pass to application.• Marked (CDATA) Sections: transparent text

<! [CDATA[ …….. ]]> What if we have ]]> as part of the text?

• Document Type Declarations

Page 18: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 18

Element

• Elements are most common form of markup.• Delimited by angle brackets.• <element> is the starting tag, </element> is the

ending tag where “element” the element name.• They identify the nature of the content, they surround.• Some comments may be empty, they need to be

ended with />• Attributes are name-value pairs, occur inside start-

tags.• In XML, all attribute values must be quoted.

Page 19: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 19

Entity References

• Entity references are used to represent special characters (e.g., <), repeated text, external file content.

• Each entity must have unique name.• Entity reference begin with ‘&’ and end with ‘;’• &lt;element>• Character references:• Decimal references: &#8478• Hexadecimal reference: &#x211E Rx

Page 20: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 20

Processing Instruction

• They are escape hatch to provide information to an application.

• Their form: <?name pidata?>where name is called PI target, identifies the PI to the application,pidata is the information passed.

• PI name beginning with xml is reserved for XML standardization.

Page 21: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 21

Document Type Declaration

• A large part of the XML specification deals with declarations that are allowed in XML. They are derived from SGML Document Type Definitions (DTD).

• Declarations express the constraints on – the tag sequence, – nesting of tags, – attribute values and defaults, – format and name of external files, and – entity that may encounter.

• DTD allows a document to communicate meta-info to the parser about its content.

Page 22: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 22

4 Types of DTD in XML

• Element Type Declarations: identify the names of elements and the nature of their content.

• Attribute list declarations• Entity declarations• Notation declarations

Page 23: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 23

Element Type Declaration

• Element Type Declarations: identify the names of elements and the nature of their content.

• Form: <!ELEMENT element_name content model>• e.g., <!ELEMENT oldjoke (burns+, allen, applause?)>• Content model define what element may contain. It

follows the typical regular expression usage. – ‘,’ specify succession among elements– + may repeat more than once and must occur at least once.– ? may be absent, may occur exactly once– Name without punctuation must occur exactly once.– The names referenced in content model must appear in DTD

for XML processor to check the validity of the document.

Page 24: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 24

Content Models

• Beside element name, special symbol #PCDATA (parseable character data) is reserved to indicate character data.

• Element contains only other elements are said to have “element content”

• Element contains both other element and #PCDATA are said to have “mixed content”.

• E.g., <!ELEMENT burns (#PCDATA | quote)*>– | or relationship.– * may occur zero or more times.– Here burn my contain zero or more characters and quote tags,

mixed in any order.– #PCDATA must precede other elements.

Page 25: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 25

Empty and Any Content Model

• EMPTY (All upper case) indicate no content and no end-tag.

• ANY (All upper case) indicate any content allowed. Useful in document conversion. Avoid this in production environment.

Page 26: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 26

Element Declaration for the Simple XML Example

• <!ELEMENT oldjoke (burn+, allen, applause?)>

• <!ELEMENT burns (#PCDATA | quote)*>

• <!ELEMENT allen (#PCDATA | quote)*>

• <!ELEMENT quote (#PCDATA)*>

• <!ELEMENT applause EMPTY>

Page 27: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 27

Attribute List Declarations• Identify which elements may have attributes, what

attribute they may have, what values the attributes may hold and what value is the default.

<!ATTLIST oldjoke name ID #REQUIRED

label CDATA #IMPLIED status (funny|notfunny) ‘funny’>• Each attribute has 3 parts: name, type, default value.• oldjoke element has 3 attributes.• CDATA string (character data).

Page 28: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 28

6 possible Attribute Types

• CDATA: strings, any text allowed.• ID: value of an ID attribute must be a name.

Element must have a single ID attribute.• IDREF: allow multiple IDREF value separated by

white space.• ENTITY (ENTITIES): must be name of a single

entity (or multiple entity names separated with white space.

• NMTOKEN (NMTOKENS): restrict form of string attribute, must be single word.

• A list of Names (enumerated type)

Page 29: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 29

4 possible Default Values

• #REQURIED• #IMPLIED: attribute value not required, no default

provided. XML processor must proceed without one.

• “value”• #FIXED “value”: not required but when appear, it

must have the specified value. Use to associate semantics with an element.

• XML processor perform attribute value normalization on attribute value, recursively resolve character references or entity references.

Page 30: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 30

Entity Declarations

• Associate a name with fragment of content. These content can be regular text, DTD, references to external files.

<!ENTITY ATI “ArborText, Inc.”><!ENTITY boilerplate SYSTEM “/standard/legal.xml”><!ENTITY ATIlogo SYSTEM “/standard/logo.gif NDATA

GIF87A>• Internal entities: first example. Use &AT I; any where in

the document to insert ArborText, Inc. • It is a short cut for frequently typed text or text expected to

change.• 5 predefine internal entities: &lt; &gt; &amp; &apos; &quot;

Page 31: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 31

External Entities

• Associate a name with the content of another file.

• The content will be inserted and parsed as part of the referring document. 2nd example &boilerplate, include the /standard/legal.xml file and parse it.

• Binary data, indicated by NDATA, (figures or other non-XML content) is not parsed and may only be referenced in an attribute. 3rd example. &ATIlogo can be used as the value of an ENTITY attribute.

Page 32: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 32

Parameter Entities

• Can only occur in DTD.

• Identified by % in front of its name.

• Referred as %name.

• They are expanded immediately. Other normal entity references will not be expanded.

Page 33: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 33

Parameter Entities Example

• <!ENTITY % personcontent “#PCDATA | quote”>

• <!ELEMENT burn (%personcontent;)*>

• <!ELEMENT allen (%personcontent;)*>

Page 34: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 34

Notation Declarations

• Identify specific types of external binary data.

• This information is passed to the processing application.

<!NOTATION GIF87A SYSTEM “GIF”>

Page 35: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 35

Where DTD is required

• XML content can be processed without DTD.• Structure of data can enforced with DTD.• Here are instances where DTD is required:• Authoring Environments. They need it to enforce

the content models of documents.• Default Attribute Values. When document relies on

default attribute values. (at least part of the DTD needs to be processed.)

• White Space Handling: semantic associate with element content is different from that with mixed content.

Page 36: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 36

Including a DTD

• DTD must be the first thing in the document after optional processing instructions and comments.

<?XML version=“0.1” standalone=“no”?><!DOCTYYPE chapter SYSTEM “dbook.dtd” [<!ENTITY %ulink.module “IGNORE”><!ELEMENT ulink (#PCDATA)*><!ATTLIST ulink xml:link CDATA #FIXED “SIMPLE” xml-attribute CDATA #FIXED “HREF URL” URL CDATA #REQUIRED>]><chapter> …</chapter>

Page 37: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 37

DTD Processing• DTD identifies the root element of the document (In this

case the “chapter”).• All XML documents must have a single root element that

contain all the content of the document.• Additional declaration may come from an external DTD

(external subset), or be included directly (internal subset).• Here dbook.dtd include element and attribute declarations

for the ulink (simple link) element in the internal subset.• Declarations in Internal subset override those in external

subset.• The standalone=“no” indicates both external and internal

subset must be processed.

Page 38: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 38

Well-formed Documents

• Document is well-formed if it obeys the syntax of XML.• Document including sequence of markup characters that can not be parsed

or are invalid is not well-formed.• Document must conform to the grammar of XML documents (in DTD)• Some markup constructs such as parameter entity reference with %, are

only allowed in specific places such as attribute value.• No attribute may appear more than once on the same start tag.• String attribute values cannot contain references to external entities.• Non-empty tags must be properly nested.• Parameter entities must be declared before referenced.• All entities except the amp, lt, gt, apos, quot must be declared.• A binary entity cannot be reference in the flow of content, only in attribute.• Neither text nor parameter entities are allowed to be recursive, directly, or

indirectly.• By definition, if a document is not well-formed, it is not XML.

Page 39: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 39

Xlink Working Draft 1.0

• Xlink specifies the relationship between resources or portion of resources.

• Since XML does not have fixed tag set, links is identified by xml:link attribute.

• Simple link: <link xml:link=“simple” href=“locator”>Link Text </link>here location can be URL, query, or extended pointer..

• Extended link:<elink xml:link=“extended” role=“annotatio”><locator xml:link=“locator” href=“vin.smil” show=“new”>Multimedia SMIL</locator><locator xml:link=“locator” href=“stock.xml” show=“embed”>Stock</locator><locator xml:link=“locator” href=“star.vml”show=“replace”>Star Picture</locator></elink>

Page 40: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 40

XPointer Working Draft 1.0

• XPointers operate on the tree defined by the elements and other markup constructs of an XML document.

• An XPointer consists of a series of location terms, each of which specifies a location, usually relative to the location specified by the prior location term.

• Each location term has a keyword (such as id, child, ancestor, and so on) and can have arguments such as an instance number, element type, or attribute.

• For example, the location term child(2,CHAP) refers to the second child element whose type is CHAP.

• child(2,oldjoke).(3,.) : locate the 3rd child (no restriction) of the 2nd oldjoke element in the document.

Page 41: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 41

XPointers

• Span regions of the tree:span(child(2,oldjoke), child(3,oldjoke))select 2nd and 3rd olkjoke in the document.

• Selection by ID, attribute value, and string matching:span(root()child(3,sect1)string(1,”Here”,0), root()child(3,sect1)string(1,”Here”,4))select 1st instance of the work “here” in the 3rd section of the document.

• Note here we do not have add anchor the referred document.

Page 42: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 42

XSL

• Extensible Stylesheet Language, a working draft, to be complete later 1999.

• A language to specify the association of presentation style with XML information

• It contains two parts: – Transformation language for preparing document for

display.• XSL can be used to convert (e.g., reordering) one XML

document to another XML document• Transformation to an HTML document can be handled as a

special case

– Formatting Object (FO) Set for actual visual styling.

Page 43: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 43

Presentation Process

• First, the result tree is constructed from the source tree.– achieved by associating patterns with templates. – A pattern is matched against elements in the source

tree. – When a pattern is matched, its template is instantiated

to create part of the result tree– source tree can be filtered and reordered, and arbitrary

structure can be added.• Second, the result tree is interpreted to produce

formatted output on a display, on paper, in speech or onto other media.

Page 44: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 44

XSL for XML

• Enabling display: The XSL Transformation Language enables display of XML by transforming XML into grammar and structure suitable for display—for instance, HTML or the XSL Formatting Objects language.

• Direct browsing of XML files: Internet Explorer 5 can apply XSL style sheets that produce HTML, allowing direct browsing of the XML files.

• Content delivery to downlevel browsers: XSL transformations can be executed on the server to provide HTML documents for downlevel browsers.

• Schema Translation: The transformation process is independent of any particular output grammar and can be used for translating XML data from one schema to another.

• Converting XML through querying, sorting, and filtering: The transformation can be used for general-purpose transformations within a single grammar, including filtering, sorting, and

summarizing data.

Page 45: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 45

Result Tree Construction• A template can contain elements that specify literal

result element structure. • A template can also contain elements that are

instructions for creating result tree fragments.• When a template is instantiated, each instruction is

executed and replaced by the result tree fragment that it creates.

• Instructions can select and process descendant elements

• The result tree is constructed by finding the template rule for the root node and instantiating its template.

Page 46: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 46

XSL name space

• XSL uses XML namespaces, to distinguish elements that are instructions to the XSL processor from elements that specify literal result tree structure.

• Instruction elements all belong to the XSL namespace, xmlns:xsl=“http://www.w3.org/TR/WD-xsl”

• An XSL stylesheet contains an xsl:stylesheet document element. This element may contain xsl:template elements specifying template rules.

• A stylesheet contains a set of template rules. A template rule has two parts: a pattern which is matched against nodes in the source tree and a template which can be instantiated to form part of the result tree.

Page 47: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 47

XSL Elementsxsl:apply-templatesDirects the XSL processor to find the appropriate template to apply based on the

results of the pattern.xsl:attributeCreates an attribute node and attaches it to the output element.xsl:chooseProvides multiple conditional testing in conjunction with the xsl:otherwise and xsl:when

elements.xsl:commentGenerates a comment in the output.xsl:copyCopies the target node from the source to the output.xsl:elementCreates an element with the specified name in the output.xsl:eval * Computes a string of generated text.xsl:for-eachAllows application of the same template to multiple nodes.xsl:ifAllows conditional subpatterns within a template.xsl:otherwiseProvides multiple conditional testing in conjunction with the xsl:choose and xsl:when

elements.xsl:piGenerates a processing instruction in the output.xsl:script * Defines global variable declarations and functions.xsl:stylesheetDefines the set of templates to be applied to the input source tree to generate the output

source tree.xsl:templateDefines a template for the output of nodes of a specific pattern.xsl:value-ofEvaluates an XSL pattern in the select attribute and returns the value of the requested

node as text, which is inserted into the template.xsl:whenProvides multiple conditional testing in conjunction with the xsl:choose and xsl:otherwise

elements.* Microsoft proprietary extensions to support scripting.

Page 48: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 48

XSL PatternXSL Patterns are constructed using the operators and special characters shown

in the following table./ Child operator; selects immediate children of the left-side collection.

When this path operator appears at the start of the pattern, it indicates that children should be selected from the root node.

// Recursive descent; searches for the specified element at any depth. When this path operator appears at the start of the pattern, it indicates recursive descent from the root node.

. Indicates the current context.* Wildcard; selects all elements regardless of the element name. @ Attribute; prefix for an attribute name.@* Attribute Wildcard; selects all attributes regardless of name.: Namespace separator; separates the namespace prefix from the

element or attribute name.! * Applies the specified method to the reference node.( ) * Groups operations to explicitly establish precedence.[ ] Applies a filter pattern. [ ] * Subscript operator. Used for indexing within a collection.

Page 49: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 49

XSL Pattern Examples

• http://msdn/microsoft.com/xml/xsl/reference/Examples.asp.• //author

Find all author elements anywhere within the current document

• /bookstore[@specialty = "textbooks"] Find all bookstores where the value of the specialty attribute is equal to "textbooks":

• author[first-name][2] finds the third author element that has a first-name

• author[degree and not(publication)] Find all author elements that contain at least one degree element and that contain no publication elements:

Page 50: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 50

A Grade XSL Example: grade.xml<?xml version="1.0"?><?xml:stylesheet type="text/xsl" href="xslex1s.xsl" ?><GRADES xmlns:dt="urn:schemas-microsoft-com:datatypes" xml:space="preserve"> <CLASS>CS401</CLASS> <YEAR>1999</YEAR> <SEMESTER>Spring</SEMESTER> <STUDENTS> <STUDENT PROGRAM="CS"> <NAME>Edward Chow</NAME> <EMAIL>[email protected]</EMAIL> <PHONE>262-3110</PHONE> <HW><NUMBER>1</NUMBER> <GRADE>88</GRADE> </HW> <MIDTERM>88</MIDTERM> <FINAL dt:dt="number">99</FINAL> </STUDENT> <STUDENT PROGRAM="EE"> <NAME>Roy Rogers</NAME>

Page 51: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 51

A Grade XSL Example<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"><xsl:script><![CDATA[ counter = 0; function even(e) { counter = counter+1; return counter%2 == 0; }]]></xsl:script> <xsl:template match="/"><HTML> <BODY STYLE="font-family:Arial, helvetica, sans-serif; font-size:12pt; background-color:#EEEEEE"> <CENTER><H2> Grade Web Page for <xsl:value-of select="GRADES/CLASS"/> <xsl:value-of select="GRADES/SEMESTER"/> <xsl:value-of select="GRADES/YEAR"/> </H2></CENTER>

Page 52: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 52

<TABLE Border="2" STYLE="background-color:teal; color:white;"> <TR STYLE="background-color:blue; color:white;">

<TH>NAME</TH><TH>EMAIL</TH><TH>HW1</TH><TH>HW2</TH><TH>HW3</TH> <TH>HW4</TH><TH>HW5</TH><TH>MIDTERM</TH><TH>FINAL</TH></TR>

<xsl:for-each select="GRADES/STUDENTS/STUDENT" order-by=“-FINAL"> <!-- -Final for descending order --> <TR> <xsl:if expr="even(this)"> <xsl:attribute name="STYLE">background-color:magenta</xsl:attribute> <xsl:attribute name="TITLE">even row</xsl:attribute> </xsl:if> <TD><xsl:value-of select="NAME"/></TD> <TD><xsl:value-of select="EMAIL"/></TD> <xsl:for-each select="HW"> <TD><xsl:value-of select="GRADE"/></TD> </xsl:for-each> <TD><xsl:value-of select="MIDTERM"/></TD> </TR> </xsl:for-each> </TABLE>

Page 53: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 53

Formatted Grade Web Page

Page 54: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 54

Simple Example: Simple.xml<?xml version='1.0'?><?xml:stylesheet type="text/xsl" href="simple.xsl" ?><breakfast-menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description>two of our famous Belgian Waffles with plenty of real maple

syrup</description> <calories>650</calories> </food> <food> <name>Strawberry Belgian Waffles</name> <price>$7.95</price> <description>light Belgian waffles coverred with strawberrys and whipped

cream</description> <calories>900</calories> </food></breakfast-menu>

Page 55: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 55

IE5.0 Display of simple.xml

Page 56: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 56

Simple.xsl<?xml version="1.0"?><HTML xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <BODY STYLE="font-family:Arial, helvetica, sans-serif; font-size:12pt; background-color:#EEEEEE"> <xsl:for-each select="breakfast-menu/food"> <DIV STYLE="background-color:teal; color:white; padding:4px"> <SPAN STYLE="font-weight:bold; color:white"><xsl:value-of select="name"/></SPAN> - <xsl:value-of select="price"/> </DIV> <DIV STYLE="margin-left:20px; margin-bottom:1em; font-size:10pt"> <xsl:value-of select="description"/> <SPAN STYLE="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving) </SPAN> </DIV> </xsl:for-each> </BODY></HTML>

Page 57: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 57

Transform Viewer Example

Page 58: 4/8/99 C. Edward Chow Page 1 XML Edward Chow Some of the presentation material is adapted from tutorial at msdn.microsoft.com/xml and articles at xml.com

4/8/99 C. Edward Chow Page 58

Homework#9

• Create a XML document with your catalog database content in Homework #8.

• Create a XSL document that transforms the XML document as a HTML with a table form with all the fields, similar to that in gradeS3.xml and grade.xsl. You can choose to sort by the name or sort by the price.

• Use http://frodo/xml/msdemos/demos/xslex1/gradeS3.xml and xslex1s3.xsl as a template

• Save the .xml and .xsl file in your directory at frodo.• Verify its correctness using IE 5.0.• Send me email with the url to .xml file when done.