introduction to xml bkf04 brian ciccolo. agenda xml revealed what is it and why should i care?...
TRANSCRIPT
Introduction to XMLBKF04
Brian Ciccolo
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
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!
Music Collection Example
A simple hierarchy:•Artists
oAlbumsSongs
•BeatlesoThe White Album
Back in the U.S.S.R.Helter Skelter
oAbbey RoadCome TogetherOh! Darling
Tags: The Building Blocks of XML
<music-collection>
This is my music collection.
Not very impressive yet.
</music-collection>
XML Syntax Rule 1Every opened tag must be closed.
<example1>
stuff
</example1>
<example2 />
<example3>
more stuff
Good Bad
Nested Tags
<music-collection>
<artist>Beatles</artist>
<artist>Frank Sinatra</artist>
<artist>U2</artist>
</music-collection>
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
Tag Attributes
<album title=“Abbey Road”
year=“1969”>
<song>Come Together</song>
<song>Oh! Darling</song>
</album>
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
Other Syntax Tidbits
• <?xml version="1.0"?>
• <!-- comments -->
• Special charactersCharacter Representation Meaning
> > Greater than
< < Less than
“ " Quote
& & Ampersand
Validating with DTD
• Document Type Definition
• Defines the elements and attributes allowed
within an XML document
• Syntax based on the XML we just learned
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
Element Definition
<!ELEMENT artist
(comment?, member*, album+)>
<!ELEMENT comment #PCDATA>
<!ELEMENT member EMPTY>
<!ELEMENT album (song+)>
<!ELEMENT song EMPTY>
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
Attribute Definition
<!ATTLIST album
nameCDATA #REQUIRED
yearCDATA #REQUIRED
format (CD|MP3) "MP3">
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
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
Thank you.