Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care?...

19
Introduction to XML BKF04 Brian Ciccolo

Transcript of Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care?...

Page 1: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

Introduction to XMLBKF04

Brian Ciccolo

Page 2: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

Agenda

• XML revealed

What is it and why should I care?

• Syntax semantics

Elements and tags and attributes, oh my!

• Document validation

Following the rules of the road

Page 3: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

XML Revealed

What is it?

• eXtensible Markup Language

• Structured data

Why should I care?

• It’s used throughout Aspen

• It’s easy to learn

• It’s fun!

Page 4: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

Music Collection Example

A simple hierarchy:•Artists

oAlbumsSongs

•BeatlesoThe White Album

Back in the U.S.S.R.Helter Skelter

oAbbey RoadCome TogetherOh! Darling

Page 5: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

Tags: The Building Blocks of XML

<music-collection>

This is my music collection.

Not very impressive yet.

</music-collection>

Page 6: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

XML Syntax Rule 1Every opened tag must be closed.

<example1>

stuff

</example1>

<example2 />

<example3>

more stuff

Good Bad

Page 7: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

Nested Tags

<music-collection>

<artist>Beatles</artist>

<artist>Frank Sinatra</artist>

<artist>U2</artist>

</music-collection>

Page 8: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

XML Syntax Rule 2All children tags must be closedbefore the parent tag is closed.

<parent>

<child>hi!</child>

</parent>

<parent>

<child>hi!

</parent>

</child>

Good Bad

Page 9: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

Tag Attributes

<album title=“Abbey Road”

year=“1969”>

<song>Come Together</song>

<song>Oh! Darling</song>

</album>

Page 10: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

XML Syntax Rule 3Attributes must be located in the open tag,

and their values must be wrapped in quotation marks.

<pet name=“Lucky” />

<pet name=“Spot”>

<type>Dog</type>

</pet>

<state name=MA />

<state>

<city>NYC</city>

</state name=“NY”>

Good Bad

Page 11: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

Other Syntax Tidbits

• <?xml version="1.0"?>

• <!-- comments -->

• Special charactersCharacter Representation Meaning

> &gt; Greater than

< &lt; Less than

“ &quot; Quote

& &amp; Ampersand

Page 12: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

Validating with DTD

• Document Type Definition

• Defines the elements and attributes allowed

within an XML document

• Syntax based on the XML we just learned

Page 13: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

DTD SymbolsSymbol Definition

() The items within the parentheses define the contents of the element or attribute

? Either 0 or 1 instances of an element is allowed (i.e., the element is optional)

+ 1 or more instances of an element is allowed (i.e., the element is required)

* 0 or more instances of an element is allowed (i.e., the element is not required)

| The items separated by the pipe define the allowed values for an element

EMPTY The element cannot contain body content

CDATA Character data that is not parsed

#PCDATA Character data that is parsed

#REQUIRED The attribute is required, a value must be specified

#IMPLIED The attribute is optional; if a value is not specified, then some default value will be used

Page 14: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

Element Definition

<!ELEMENT artist

(comment?, member*, album+)>

<!ELEMENT comment #PCDATA>

<!ELEMENT member EMPTY>

<!ELEMENT album (song+)>

<!ELEMENT song EMPTY>

Page 15: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

DTD Example 1

<artist>

<member>...</member>

<member>...</member>

<album>...</album>

<album>...</album>

<album>...</album>

</artist>

<!-- snippet 1 -->

<artist>

<member>...</member>

<comment>...</comment>

</artist>

<!-- snippet 2 -->

<song>...</song>

Good Bad

Page 16: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

Attribute Definition

<!ATTLIST album

nameCDATA #REQUIRED

yearCDATA #REQUIRED

format (CD|MP3) "MP3">

Page 17: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

DTD Example 2

<album

name=“The White Album”year=“1968”>...

</album>

<album year=“1969”

format=“CD”

name=“Abbey Road”>...

</album>

<album

name=“Abbey Road”format=“Vinyl”>...

</album>

Good Bad

Page 18: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

Extras

• w3schools.com XML Tutorial

http://www.w3schools.com/xml

• W3C XML Specification

http://www.w3.org/TR/REC-xml

• Notepad++ text editor

http://notepad-plus.sourceforge.net

Page 19: Introduction to XML BKF04 Brian Ciccolo. Agenda XML revealed What is it and why should I care? Syntax semantics Elements and tags and attributes, oh my!

Thank you.

[email protected]