You are on page 1of 56

XML stands for EXtensible Markup Language. XML is designed to describe data and to focus on what data is.

In our XML tutorial you will learn about XML and the difference between XML and HTML. You will also learn how to start using XML in your applications. XML was designed to describe data and to focus on what data is. HTML was designed to display data and to focus on how data looks.

What You

hould !lready "now

Before you continue you should have a basic understanding of the following:

HTML / XHTML JavaScript or BScript

!f you want to study these sub"ects first# find the tutorials on our Ho$e page%

What is XML#
XML stands for &Xtensible Mar'up Language XML is a $arkup language $uch li'e HTML XML was designed to describe data XML tags are not predefined% (ou $ust define your own tags XML uses a %ocu$ent Type %efinition )*T*+ or an XML XML is a ,-. /eco$$endation che$a to describe the data XML with a *T* or XML Sche$a is designed to be self&descripti'e

XML is a W() *eco$$endation


The &0tensible Mar'up Language )XML+ beca$e a ,-. /eco$$endation 12% 3ebruary 1445% (ou can read $ore about XML standards in our ,-. tutorial% XML was designed to describe+ store+ carry and e,change data. XML -.. is the latest 'ersion of XML.

XML /ersions
XML 1.0
XML 1%2 was released as a ,-. /eco$$endation 12% 3ebruary 1445%

XML 1.0 (Second Edition)


XML 1%2 )S&+ was released as a ,-. /eco$$endation 6% 7ctober 8222% Second &dition is only a correction to XML 1%2 that incorporates the first9edition errata )bug fi0es+%

XML 1.0 (Third Edition)


Third &dition is only a correction to XML 1%2 that incorporates the first9 and second9edition errata )bug fi0es+%

XML 1.1
XML 1%1 was released as a ,or'ing *raft 1-% *ece$ber 8221# and as a .andidate /eco$$endation 1:% 7ctober 8228% XML 1%1 allows al$ost any ;nicode characters to be used in na$es%

0ther W() XML Technologies


XML Namespaces
XML na$espaces defines a $ethod for defining ele$ent and attribute na$es used in XML by associating the$ with ;/! references%

XML Linking (XLink, XPointer, and XML Base)


The XML Lin'ing Language )XLin'+# allows you to insert lin's into XML docu$ents% The XML <ointer Language )X<ointer+# allows the lin's to address into specific parts of an XML docu$ent% XML Base is a standard for defining a default reference to e0ternal XML resources )si$ilar to =base> in HTML+%

X nc!"de
X!nclude is a $echanis$ for $erging XML docu$ents using ele$ents# attributes# and ;/! references%

W() XML
pecification XML 1%2

pecifications and Ti$eline


%raft 1 2roposal *eco$$endation 12% 3eb 1445 26% 7ct 8222 2?% 3eb 822? 2?% 3eb 822? 16% @ug 8226 1?% Jan 1444 2?% 3eb 822?

XML 1%2 )8%&d+ XML 1%2 )-%&d+ XML 1%1 XML 1%1 )8%&d+ XML Aa$espaces XML 1%1 Aa$espaces

XML 1%1 Aa$espaces S& XML !nfoset XML !nfoset )8%&d+ XML Base XLin' 1%2 X<ointer 3ra$ewor' X<ointer ele$ent)+ sche$e X<ointer 0$lns)+ sche$e X!nclude 1%2 X!nclude 1%2 S& XML <rocessing Model 2:% @pr 2?

16% @ug 8226 8?% 7ct% 8221 2?% 3eb% 822? 8B% Jun 8221 8B% Jun 8221 8:% Mar 8228:% Mar 8228:% Mar 82282% *ec 822? 1:% Aov 8226

The Main %ifference 3etween XML and HTML


XML was designed to carry data. XML is not a replace$ent for HTML% XML and HTML were designed with different goals: XML was designed to describe data and to focus on what data is% HTML was designed to display data and to focus on how data loo's% HTML is about displaying infor$ation# while XML is about describing infor$ation%

XML %oes not %0 !nything


XML was not designed to %0 anything. Maybe it is a little hard to understand# but XML does not *7 anything% XML was created to structure# store and to send infor$ation% The following e0a$ple is a note to Tove fro$ Jani# stored as XML:

<note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>


The note has a header and a $essage body% !t also has sender and receiver infor$ation% But still# this XML docu$ent does not *7 anything% !t is "ust pure infor$ation wrapped in XML tags% So$eone $ust write a piece of software to send# receive or display it%

XML is 4ree and E,tensible


XML tags are not predefined. You $ust 5in'ent5 your own tags. The tags used to $ar' up HTML docu$ents and the structure of HTML docu$ents are predefined% The author of HTML docu$ents can only use tags that are defined in the HTML standard )li'e =p># =h1># etc%+% XML allows the author to define his own tags and his own docu$ent structure% The tags in the e0a$ple above )li'e =to> and =fro$>+ are not defined in any XML standard% These tags are CinventedC by the author of the XML docu$ent%

XML is a )o$ple$ent to HTML


XML is not a replace$ent for HTML. !t is i$portant to understand that XML is not a replace$ent for HTML% !n future ,eb develop$ent it is $ost li'ely that XML will be used to describe the data# while HTML will be used to for$at and display the sa$e data% My best description of XML is this: XML is a cross&platfor$+ software and hardware independent tool for trans$itting infor$ation.

XML in 4uture Web %e'elop$ent


XML is going to be e'erywhere. ,e have been participating in XML develop$ent since its creation% !t has been a$aDing to see how Euic'ly the XML standard has been developed and how Euic'ly a large nu$ber of software vendors have adopted the standard% ,e strongly believe that XML will be as i$portant to the future of the ,eb as HTML has been to the foundation of the ,eb and that XML will be the $ost co$$on tool for all data $anipulation and data trans$ission%

How can XML be Used?


It is i$portant to understand that XML was designed to store+ carry+ and e,change data. XML was not designed to display data.

XML can

eparate %ata fro$ HTML

With XML+ your data is stored outside your HTML. ,hen HTML is used to display data# the data is stored inside your HTML% ,ith XML# data can be stored in separate XML files% This way you can concentrate on using HTML for data layout and display# and be sure that changes in the underlying data will not reEuire any changes to your HTML% XML data can also be stored inside HTML pages as C*ata !slandsC% (ou can still concentrate on using HTML only for for$atting and displaying the data%

XML is 6sed to E,change %ata


With XML+ data can be e,changed between inco$patible syste$s. !n the real world# co$puter syste$s and databases contain data in inco$patible for$ats% 7ne of the $ost ti$e9consu$ing challenges for developers has been to e0change data between such syste$s over the !nternet% .onverting the data to XML can greatly reduce this co$ple0ity and create data that can be read by $any different types of applications%

XML and 373


With XML+ financial infor$ation can be e,changed o'er the Internet. &0pect to see a lot about XML and B8B )Business To Business+ in the near future% XML is going to be the $ain language for e0changing financial infor$ation between businesses over the !nternet% @ lot of interesting B8B applications are under develop$ent%

XML )an be 6sed to

hare %ata

With XML+ plain te,t files can be used to share data. Since XML data is stored in plain te0t for$at# XML provides a software9 and hardware9independent way of sharing data% This $a'es it $uch easier to create data that different applications can wor' with% !t also $a'es it easier to e0pand or upgrade a syste$ to new operating syste$s# servers# applications# and new browsers%

XML )an be 6sed to

tore %ata

With XML+ plain te,t files can be used to store data. XML can also be used to store data in files or in databases% @pplications can be written to store and retrieve infor$ation fro$ the store# and generic applications can be used to display the data%

XML )an Make your %ata More 6seful


With XML+ your data is a'ailable to $ore users. Since XML is independent of hardware# software and application# you can $a'e your data available to other than only standard HTML browsers% 7ther clients and applications can access your XML files as data sources# li'e they are accessing databases% (our data can be $ade available to all 'inds of Creading $achinesC )agents+# and it is easier to $a'e your data available for blind people# or people with other disabilities%

XML )an be 6sed to )reate 8ew Languages

XML is the $other of W!2 and WML. The ,ireless Mar'up Language ),ML+# used to $ar'up !nternet applications for handheld devices li'e $obile phones# is written in XML% (ou can read $ore about ,ML in our ,ML tutorial% The W!2 protocol was designed to show internet&contents on wireless clients+ like $obile phones. WML is the language used to create pages to be displayed in a W!2 browser. In our W!2 tutorial you will learn about W!2 and WML. You will learn how to con'ert your HTML pages to pocket for$at+ so that your infor$ation can be accessed fro$ wireless clients+ like $obile phones.

If %e'elopers Ha'e

ense

If they %0 ha'e sense+ all future applications will e,change their data in XML. The future $ight give us word processors# spreadsheet applications and databases that can read each otherFs data in a pure te0t for$at# without any conversion utilities in between% ,e can only pray that Microsoft and all the other software vendors will agree%

XML Syntax Rules


The synta, rules of XML are 'ery si$ple and 'ery strict. The rules are 'ery easy to learn+ and 'ery easy to use. 3ecause of this+ creating software that can read and $anipulate XML is 'ery easy.

!n E,a$ple XML %ocu$ent


XML docu$ents use a self&describing and si$ple synta,.

<?xml version !"#$! en%oding !&'()**+,)"!?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
The first line in the docu$ent 9 the XML declaration 9 defines the XML version and the character encoding used in the docu$ent% !n this case the docu$ent confor$s to the 1%2 specification of XML and uses the !S7955:491 )Latin91/,est &uropean+ character set% The ne0t line describes the root ele$ent of the docu$ent )li'e it was saying: Cthis docu$ent is a noteC+:

<note>
The ne0t ? lines describe ? child ele$ents of the root )to# fro$# heading# and body+:

<to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body>


@nd finally the last line defines the end of the root ele$ent:

</note>
.an you detect fro$ this e0a$ple that the XML docu$ent contains a Aote to Tove fro$ JaniG *onFt you agree that XML is pretty self9descriptiveG

!ll XML Ele$ents Must Ha'e a )losing Tag


With XML+ it is illegal to o$it the closing tag. !n HTML so$e ele$ents do not have to have a closing tag% The following code is legal in HTML:

<->This is a -aragra-h <->This is another -aragra-h


!n XML all ele$ents $ust have a closing tag# li'e this:

<->This is a -aragra-h</-> <->This is another -aragra-h</->


8ote: (ou $ight have noticed fro$ the previous e0a$ple that the XML declaration did not have a closing tag% This is not an error% The declaration is not a part of the XML docu$ent itself% !t is not an XML ele$ent# and it should not have a closing tag%

XML Tags are )ase

ensiti'e

6nlike HTML+ XML tags are case sensiti'e. ,ith XML# the tag =Letter> is different fro$ the tag =letter>% 7pening and closing tags $ust therefore be written with the sa$e case:

<.essage>This is in%orre%t</message> <message>This is %orre%t</message>

XML Ele$ents Must be 2roperly 8ested


I$proper nesting of tags $akes no sense to XML.

!n HTML so$e ele$ents can be i$properly nested within each other li'e this:

<b><i>This text is bold and itali%</b></i>


!n XML all ele$ents $ust be properly nested within each other li'e this:

<b><i>This text is bold and itali%</i></b>

XML %ocu$ents Must Ha'e a *oot Ele$ent


!ll XML docu$ents $ust contain a single tag pair to define a root ele$ent. @ll other ele$ents $ust be within this root ele$ent% @ll ele$ents can have sub ele$ents )child ele$ents+% Sub ele$ents $ust be correctly nested within their parent ele$ent:

<root> <%hild> <s/b%hild>#####</s/b%hild> </%hild> </root>

XML !ttribute /alues Must be 9uoted


With XML+ it is illegal to o$it :uotation $arks around attribute 'alues. XML ele$ents can have attributes in na$e/value pairs "ust li'e in HTML% !n XML the attribute value $ust always be Euoted% Study the two XML docu$ents below% The first one is incorrect# the second is correct:

<?xml version !"#$! en%oding !&'()**+,)"!?> <note date "0/""/0$$0> <to>Tove</to> <from>Jani</from> </note> <?xml version !"#$! en%oding !&'()**+,)"!?> <note date !"0/""/0$$0!> <to>Tove</to> <from>Jani</from> </note>
The error in the first docu$ent is that the date attribute in the note ele$ent is not Euoted% This is correct: dateHC18/11/8228C% This is incorrect: dateH18/11/8228%

With XML+ White

pace is 2reser'ed

With XML+ the white space in your docu$ent is not truncated%

This is unli'e HTML% ,ith HTML# a sentence li'e this: Hello $y na$e is Tove#

will be displayed li'e this: Hello $y na$e is Tove# because HTML reduces $ultiple# consecutive white space characters to a single white space%

With XML+ )* 1 L4 is )on'erted to L4


With XML+ a new line is always stored as L4% *o you 'now what a typewriter isG ,ell# a typewriter is a $echanical device which was used last century to produce printed docu$ents% :9+ @fter you have typed one line of te0t on a typewriter# you have to $anually return the printing carriage to the left $argin position and $anually feed the paper up one line% !n ,indows applications# a new line is nor$ally stored as a pair of characters: carriage return )./+ and line feed )L3+% The character pair bears so$e rese$blance to the typewriter actions of setting a new line% !n ;ni0 applications# a new line is nor$ally stored as a L3 character% Macintosh applications use only a ./ character to store a new line%

)o$$ents in XML
The synta0 for writing co$$ents in XML is si$ilar to that of HTML% =I99 This is a co$$ent 99>

There is 8othing

pecial !bout XML

There is nothing special about XML% !t is "ust plain te0t with the addition of so$e XML tags enclosed in angle brac'ets% Software that can handle plain te0t can also handle XML% !n a si$ple te0t editor# the XML tags will be visible and will not be handled specially% !n an XML9aware application however# the XML tags can be handled specially% The tags $ay or $ay not be visible# or have a functional $eaning# depending on the nature of the application%

XML E!ements
XML Ele$ents are e,tensible and they ha'e relationships. XML Ele$ents ha'e si$ple na$ing rules.

XML Ele$ents are E,tensible


XML docu$ents can be e,tended to carry $ore infor$ation.

Loo' at the following XML A7T& e0a$ple:

<note> <to>Tove</to> <from>Jani</from> <body>Don't forget me this weekend!</body> </note>


LetFs i$agine that we created an application that e0tracted the =to># =fro$># and =body> ele$ents fro$ the XML docu$ent to produce this output: ME !;E

To< Tove 4ro$< Jani *onFt forget $e this wee'endI !$agine that the author of the XML docu$ent added so$e e0tra infor$ation to it:

<note> <date>0$$0)$*)$"</date> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
Should the application brea' or crashG Ao% The application should still be able to find the =to># =fro$># and =body> ele$ents in the XML docu$ent and produce the sa$e output% XML docu$ents are E,tensible.

XML Ele$ents ha'e *elationships


Ele$ents are related as parents and children. To understand XML ter$inology# you have to 'now how relationships between XML ele$ents are na$ed# and how ele$ent content is described% !$agine that this is a description of a boo': My 4irst XML !ntroduction to XML

,hat is HTML ,hat is XML

XML Synta0

&le$ents $ust have a closing tag &le$ents $ust be properly nested

!$agine that this XML docu$ent describes the boo':

<book> <title>.y 1irst 2.3</title> <-rod id !44)5+6! media !-a-er!></-rod> <%ha-ter>&ntrod/%tion to 2.3 <-ara>7hat is 8T.3</-ara> <-ara>7hat is 2.3</-ara> </%ha-ter> <%ha-ter>2.3 'yntax <-ara>9lements m/st have a %losing tag</-ara> <-ara>9lements m/st be -ro-erly nested</-ara> </%ha-ter> </book>
Boo' is the root ele$ent% Title# prod# and chapter are child ele$ents of boo'% Boo' is the parent ele$ent of title# prod# and chapter% Title# prod# and chapter are siblings )or sister ele$ents+ because they have the sa$e parent%

Ele$ents ha'e )ontent


Ele$ents can ha'e different content types. @n XML ele$ent is everything fro$ )including+ the ele$entFs start tag to )including+ the ele$entFs end tag% @n ele$ent can have ele$ent content# $i,ed content# si$ple content# or e$pty content% @n ele$ent can also have attributes% !n the e0a$ple above# boo' has ele$ent content# because it contains other ele$ents% .hapter has $i,ed content because it contains both te0t and other ele$ents% <ara has si$ple content )or te,t content+ because it contains only te0t% <rod has e$pty content# because it carries no infor$ation% !n the e0a$ple above only the prod ele$ent has attributes% The attribute na$ed id has the 'alue C--96:BC% The attribute na$ed $edia has the 'alue CpaperC%

Ele$ent 8a$ing
XML ele$ents $ust follow these na$ing rules<

Aa$es can contain letters# nu$bers# and other characters Aa$es $ust not start with a nu$ber or punctuation character

Aa$es $ust not start with the letters 0$l )or XML# or X$l# etc+ Aa$es cannot contain spaces

Ta'e care when you CinventC ele$ent na$es and follow these si$ple rules: @ny na$e can be used# no words are reserved# but the idea is to $a'e na$es descriptive% Aa$es with an underscore separator are nice% &0a$ples: =firstJna$e># =lastJna$e>% @void C9C and C%C in na$es% 3or e0a$ple# if you na$e so$ething Cfirst9na$e#C it could be a $ess if your software tries to subtract na$e fro$ first% 7r if you na$e so$ething Cfirst%na$e#C your software $ay thin' that Cna$eC is a property of the ob"ect Cfirst%C &le$ent na$es can be as long as you li'e# but donFt e0aggerate% Aa$es should be short and si$ple# li'e this: =boo'Jtitle> not li'e this: =theJtitleJofJtheJboo'>% XML docu$ents often have a corresponding database# in which fields e0ist corresponding to ele$ents in the XML docu$ent% @ good practice is to use the na$ing rules of your database for the ele$ents in the XML docu$ents% Aon9&nglish letters li'e KLM are perfectly legal in XML ele$ent na$es# but watch out for proble$s if your software vendor doesnFt support the$% The C:C should not be used in ele$ent na$es because it is reserved to be used for so$ething called na$espaces )$ore later+%

XML #ttri$"tes
XML ele$ents can ha'e attributes in the start tag+ =ust like HTML. !ttributes are used to pro'ide additional infor$ation about ele$ents.

XML !ttributes
XML ele$ents can ha'e attributes. 3ro$ HTML you will re$e$ber this: =!MN S/.HCco$puter%gifC>% The S/. attribute provides additional infor$ation about the !MN ele$ent% !n HTML )and in XML+ attributes provide additional infor$ation about ele$ents:

<img sr% !%om-/ter#gif!> <a href !demo#as-!>


@ttributes often provide infor$ation that is not a part of the data% !n the e0a$ple below# the file type is irrelevant to the data# but i$portant to the software that wants to $anipulate the ele$ent:

<file ty-e !gif!>%om-/ter#gif</file>

9uote

tyles+ 5fe$ale5 or >fe$ale>#

@ttribute values $ust always be enclosed in Euotes# but either single or double Euotes can be used% 3or a personFs se0# the person tag can be written li'e this:

<-erson sex !female!>


or li'e this:

<-erson sex 'female'>


8ote< !f the attribute value itself contains double Euotes it is necessary to use single Euotes# li'e in this e0a$ple:

<gangster name ':eorge !'hotg/n! ;iegler'>


8ote< !f the attribute value itself contains single Euotes it is necessary to use double Euotes# li'e in this e0a$ple:

<gangster name !:eorge ''hotg/n' ;iegler!>

6se of Ele$ents 's. !ttributes


%ata can be stored in child ele$ents or in attributes. Ta'e a loo' at these e0a$ples:

<-erson sex !female!> <firstname><nna</firstname> <lastname>'mith</lastname> </-erson> <-erson> <sex>female</sex> <firstname><nna</firstname> <lastname>'mith</lastname> </-erson>
!n the first e0a$ple se0 is an attribute% !n the last# se0 is a child ele$ent% Both e0a$ples provide the sa$e infor$ation% There are no rules about when to use attributes# and when to use child ele$ents% My e0perience is that attributes are handy in HTML# but in XML you should try to avoid the$% ;se child ele$ents if the infor$ation feels li'e data%

My 4a'orite Way
I like to store data in child ele$ents. The following three XML docu$ents contain e0actly the sa$e infor$ation: @ date attribute is used in the first e0a$ple:

<note date !"0/""/0$$0!> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
@ date ele$ent is used in the second e0a$ple:

<note> <date>"0/""/0$$0</date> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
@n e0panded date ele$ent is used in the third: )TH!S !S M( 3@ 7/!T&+:

<note> <date> <day>"0</day> <month>""</month> <year>0$$0</year> </date> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>

!'oid using attributes#


hould you a'oid using attributes# So$e of the proble$s with using attributes are:

attributes cannot contain $ultiple values )child ele$ents can+ attributes are not easily e0pandable )for future changes+ attributes cannot describe structures )child ele$ents can+ attributes are $ore difficult to $anipulate by progra$ code attribute values are not easy to test against a *ocu$ent Type *efinition )*T*+ 9 which is used to define the legal ele$ents of an XML docu$ent

!f you use attributes as containers for data# you end up with docu$ents that are difficult to read and $aintain% Try to use ele$ents to describe data% ;se attributes only to provide infor$ation that is not relevant to the data% *onFt end up li'e this )this is not how XML should be used+:

<note day !"0! month !""! year !0$$0! to !Tove! from !Jani! heading !Reminder! body !Don't forget me this weekend!!>

</note>

!n E,ception to $y !ttribute *ule


*ules always ha'e e,ceptions. My rule about attributes has one e0ception: So$eti$es ! assign !* references to ele$ents% These !* references can be used to access XML ele$ents in $uch the sa$e way as the A@M& or !* attributes in HTML% This e0a$ple de$onstrates this:

<messages> <note id !-+$"!> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> <note id !-+$0!> <to>Jani</to> <from>Tove</from> <heading>Re= Reminder</heading> <body>& will not!</body> </note> </messages>
The !* in these e0a$ples is "ust a counter# or a uniEue identifier# to identify the different notes in the XML file# and not a part of the note data% ,hat ! a$ trying to say here is that $etadata )data about data+ should be stored as attributes# and that data itself should be stored as ele$ents%

XML Validation
XML with correct synta, is Well 4or$ed XML. XML 'alidated against a %T% is /alid XML.

Well 4or$ed XML %ocu$ents


! 5Well 4or$ed5 XML docu$ent has correct XML synta,. @ C,ell 3or$edC XML docu$ent is a docu$ent that confor$s to the XML synta0 rules that were described in the previous chapters:

XML docu$ents $ust have a root ele$ent XML ele$ents $ust have a closing tag XML tags are case sensitive XML ele$ents $ust be properly nested XML attribute values $ust always be Euoted

<?xml version !"#$! en%oding !&'()**+,)"!?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>

/alid XML %ocu$ents


! 5/alid5 XML docu$ent also confor$s to a %T%. @ C alidC XML docu$ent is a C,ell 3or$edC XML docu$ent# which also confor$s to the rules of a *ocu$ent Type *efinition )*T*+:

<?xml version !"#$! en%oding !&'()**+,)"!?> <!D(>T?@9 note '?'T9. !&nternalAote#dtd!> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>

XML %T%
! %T% defines the legal ele$ents of an XML docu$ent. The purpose of a *T* is to define the legal building bloc's of an XML docu$ent% !t defines the docu$ent structure with a list of legal ele$ents% (ou can read $ore about *T*# and how to validate your XML docu$ents in our *T* tutorial%

The purpose of a %T% ?%ocu$ent Type %efinition@ is to define the legal building blocks of an XML docu$ent. It defines the docu$ent structure with a list of legal ele$ents and attributes.

XML
XML

che$a
che$a is an XML based alternati'e to %T%.

,-. supports an alternative to *T* called XML Sche$a% (ou can read $ore about XML Sche$a in our Sche$a tutorial% XML che$a is an XML&based alternati'e to %T%s. che$a describes the structure of an XML docu$ent. che$a language is also referred to as XML che$a %efinition

!n XML The XML ?X %@.

In this tutorial you will learn how to read and create XML

che$as+ why

XML che$as are $ore powerful than %T%s+ and how to use the XML che$a language in your application.

! ;eneral XML /alidator


To help you chec' the synta0 of your 0$l files# we have created this lin' so that you can synta09 chec' any XML file%

XML %a!idator
XML Errors will top you
Errors in XML docu$ents will stop your XML progra$. The ,-. XML specification states that a progra$ should not continue to process an XML docu$ent if it finds an error% The reason is that XML software should be easy to write# and that all XML docu$ents should be co$patible% ,ith HTML it was possible to create docu$ents with lots of errors )li'e when you forget an end tag+% 7ne of the $ain reasons that HTML browsers are so big and inco$patible# is that they have their own ways to figure out what a docu$ent should loo' li'e when they encounter an HTML error% ,ith XML this should not be possible%

ynta,&check your XML & IE 0nly


To help you synta09chec' your 0$l# we have used MicrosoftFs XML parser to create an XML validator% <aste your XML in the te0t area below# and synta09chec' it by pressing the C alidateC button%

ynta,&check your XML 4ile & IE 0nly


(ou can also synta09chec' your XML file by typing the ;/L of your file into the input field below# and then press the C alidateC button

3ilena$e: http://w w w .w 3schools.com/xml/note_error.xml


Validate

!f you want to synta09chec' an error9free XML file# you can paste the following address into the filena$e field: http://www%w-schools%co$/0$l/cdJcatalog%0$l 8ote< !f you get the error C@ccess deniedC when accessing this file# it is because your !nternet &0plorer security settings do not allow access across do$ainsI

XML Browser Support


8early all $a=or browsers ha'e support for XML and X LT.

MoAilla 4irefo,
@s of version 1%2%8# 3irefo0 has support for XML and XSLT )and .SS+%

MoAilla
MoDilla includes &0pat for XML parsing and has support to display XML O .SS% MoDilla also has so$e support for Aa$espaces% MoDilla is available with an XSLT i$ple$entation%

8etscape
@s of version 5# Aetscape uses the MoDilla engine# and therefore it has the sa$e XML / XSLT support as MoDilla%

0pera
@s of version 4# 7pera has support for XML and XSLT )and .SS+% .SS% ersion 5 supports only XML O

Internet E,plorer
@s of version 6# !nternet &0plorer supports XML# Aa$espaces# .SS# XSLT# and X<ath% 8ote< !nternet &0plorer : also has XML support# but the XSL part is 80T co$patible with the official ,-. XSL /eco$$endationI

%ie&ing XML 'i!es


*aw XML files can be 'iewed in MoAilla+ 4irefo,+ 0pera+ Internet E,plorer+ and 8etscape BC. Howe'er+ to $ake XML docu$ents display as nice web pages+ you will ha'e to add so$e display infor$ation.

/iewing XML 4iles


In 4irefo, and Internet E,plorer< 7pen the XML file )typically by clic'ing on a lin'+ 9 The XML docu$ent will be displayed with color9 coded root and child ele$ents% @ plus )O+ or $inus sign )9+ to the left of the ele$ents can be clic'ed to e0pand or collapse the ele$ent structure% To view the raw XML source )without the O and 9 signs+# select C iew <age SourceC or C iew SourceC fro$ the browser $enu% In 8etscape B< 7pen the XML file# then right9clic' in XML file and select C iew <age SourceC% The XML docu$ent will then be displayed with color9coded root and child ele$ents% In 0pera D and E< !n 7pera B: 7pen the XML file# then right9clic' in XML file and select C3ra$eC / C iew SourceC% The XML docu$ent will be displayed as plain te0t% !n 7pera 5: 7pen the XML file# then right9clic' in XML file and select CSourceC% The XML docu$ent will be displayed as plain te0t% Loo' at this XML file: note%0$l

=G0$l versionHC1%2C encodingHC!S7955:491C G> 9 =I99

9dited with 2.3 '-y v0$$6 Bhtt-=//www#altova#%omC

99> - =note> =to>To'e=/to> =fro$>Fani=/fro$> =heading>*e$inder=/heading> =body>%on>t forget $e this weekendG=/body> =/note>

8ote< %o not e,pect XML files to be for$atted like HTML docu$entsG

/iewing an In'alid XML 4ile


!f an erroneous XML file is opened# the browser will report the error% Loo' at this XML file: noteJerror%0$l

The XML page cannot be displayed


.annot view XML input using XSL style sheet% <lease correct the error and then clic' the /efresh button# or try again later%

End tag >4fro$> does not $atch the start tag >fro$>. Error processing resource >http<11www.w(schools.co$1,$l1noteHerror.,$l...

<from>Jani</1from> )))))))))))))D

0ther XML E,a$ples


iewing so$e XML docu$ents will help you get the XML feeling% @n XML .* catalog This is $y fatherFs .* collection# stored as XML data )old and boring titles ! guess%%% :9++%

=G0$l versionHC1%2C encodingHC!S7955:491C G> 9 =I99

9dited with 2.3 '-y v0$$6 Bhtt-=//www#altova#%omC


99> =.@T@L7N> =.*> =T!TL&>E$pire 3urles:ue=/T!TL&> =@/T!ST>3ob %ylan=/@/T!ST> =.7;AT/(>6 !=/.7;AT/(> =.7M<@A(>)olu$bia=/.7M<@A(> =</!.&>-..I.=/</!.&> =(&@/>-IEJ=/(&@/> =/.*> =.*> =T!TL&>Hide your heart=/T!TL&> =@/T!ST>3onnie Tyler=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>)3 *ecords=/.7M<@A(> =</!.&>I.I.=/</!.&> =(&@/>-IEE=/(&@/> =/.*> =.*> =T!TL&>;reatest Hits=/T!TL&> =@/T!ST>%olly 2arton=/@/T!ST> =.7;AT/(>6 !=/.7;AT/(> =.7M<@A(>*)!=/.7M<@A(> =</!.&>I.I.=/</!.&> =(&@/>-IE7=/(&@/> =/.*> =.*> =T!TL&> till got the blues=/T!TL&> =@/T!ST>;ary Moore=/@/T!ST> =.7;AT/(>6"=/.7;AT/(>

=.7M<@A(>/irgin records=/.7M<@A(> =</!.&>-..7.=/</!.&> =(&@/>-II.=/(&@/> =/.*> =.*> =T!TL&>Eros=/T!TL&> =@/T!ST>Eros *a$aAAotti=/@/T!ST> =.7;AT/(>E6=/.7;AT/(> =.7M<@A(>3M;=/.7M<@A(> =</!.&>I.I.=/</!.&> =(&@/>-IID=/(&@/> =/.*> =.*> =T!TL&>0ne night only=/T!TL&> =@/T!ST>3ee ;ees=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>2olydor=/.7M<@A(> =</!.&>-..I.=/</!.&> =(&@/>-IIE=/(&@/> =/.*> =.*> =T!TL&> yl'ias Mother=/T!TL&> =@/T!ST>%r.Hook=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>)3 =/.7M<@A(> =</!.&>E.-.=/</!.&> =(&@/>-ID(=/(&@/> =/.*> =.*> =T!TL&>Maggie May=/T!TL&> =@/T!ST>*od tewart=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>2ickwick=/.7M<@A(> =</!.&>E.J.=/</!.&> =(&@/>-II.=/(&@/> =/.*> =.*> =T!TL&>*o$anAa=/T!TL&> =@/T!ST>!ndrea 3ocelli=/@/T!ST> =.7;AT/(>E6=/.7;AT/(> =.7M<@A(>2olydor=/.7M<@A(> =</!.&>-..E.=/</!.&> =(&@/>-IIB=/(&@/> =/.*> =.*> =T!TL&>When a $an lo'es a wo$an=/T!TL&> =@/T!ST>2ercy ledge=/@/T!ST> =.7;AT/(>6 !=/.7;AT/(> =.7M<@A(>!tlantic=/.7M<@A(>

=</!.&>E.D.=/</!.&> =(&@/>-IED=/(&@/> =/.*> =.*> =T!TL&>3lack angel=/T!TL&> =@/T!ST> a'age *ose=/@/T!ST> =.7;AT/(>E6=/.7;AT/(> =.7M<@A(>Mega=/.7M<@A(> =</!.&>-..I.=/</!.&> =(&@/>-IIJ=/(&@/> =/.*> =.*> =T!TL&>-III ;ra$$y 8o$inees=/T!TL&> =@/T!ST>Many=/@/T!ST> =.7;AT/(>6 !=/.7;AT/(> =.7M<@A(>;ra$$y=/.7M<@A(> =</!.&>-..7.=/</!.&> =(&@/>-III=/(&@/> =/.*> =.*> =T!TL&>4or the good ti$es=/T!TL&> =@/T!ST>"enny *ogers=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>Mucik Master=/.7M<@A(> =</!.&>E.D.=/</!.&> =(&@/>-IIJ=/(&@/> =/.*> =.*> =T!TL&>3ig Willie style=/T!TL&> =@/T!ST>Will $ith=/@/T!ST> =.7;AT/(>6 !=/.7;AT/(> =.7M<@A(>)olu$bia=/.7M<@A(> =</!.&>I.I.=/</!.&> =(&@/>-IID=/(&@/> =/.*> =.*> =T!TL&>Tupelo Honey=/T!TL&> =@/T!ST>/an Morrison=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>2olydor=/.7M<@A(> =</!.&>E.7.=/</!.&> =(&@/>-ID-=/(&@/> =/.*> =.*> =T!TL&> ouls'ille=/T!TL&> =@/T!ST>Forn Hoel=/@/T!ST> =.7;AT/(>8orway=/.7;AT/(> =.7M<@A(>WE!=/.7M<@A(> =</!.&>D.I.=/</!.&>

=(&@/>-IIB=/(&@/> =/.*> =.*> =T!TL&>The 'ery best of=/T!TL&> =@/T!ST>)at te'ens=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>Island=/.7M<@A(> =</!.&>E.I.=/</!.&> =(&@/>-II.=/(&@/> =/.*> =.*> =T!TL&> top=/T!TL&> =@/T!ST> a$ 3rown=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>! and M=/.7M<@A(> =</!.&>E.I.=/</!.&> =(&@/>-IEE=/(&@/> =/.*> =.*> =T!TL&>3ridge of pies=/T!TL&> =@/T!ST>T>2au=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(> iren=/.7M<@A(> =</!.&>D.I.=/</!.&> =(&@/>-IED=/(&@/> =/.*> =.*> =T!TL&>2ri'ate %ancer=/T!TL&> =@/T!ST>Tina Turner=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>)apitol=/.7M<@A(> =</!.&>E.I.=/</!.&> =(&@/>-IE(=/(&@/> =/.*> =.*> =T!TL&>Midt o$ natten=/T!TL&> =@/T!ST>"i$ Larsen=/@/T!ST> =.7;AT/(>E6=/.7;AT/(> =.7M<@A(>Medley=/.7M<@A(> =</!.&>D.E.=/</!.&> =(&@/>-IE(=/(&@/> =/.*> =.*> =T!TL&>2a'arotti ;ala )oncert=/T!TL&> =@/T!ST>Luciano 2a'arotti=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>%E))!=/.7M<@A(> =</!.&>I.I.=/</!.&> =(&@/>-II-=/(&@/>

=/.*> - =.*> =T!TL&>The dock of the bay=/T!TL&> =@/T!ST>0tis *edding=/@/T!ST> =.7;AT/(>6 !=/.7;AT/(> =.7M<@A(>!tlantic=/.7M<@A(> =</!.&>D.I.=/</!.&> =(&@/>-IED=/(&@/> =/.*> - =.*> =T!TL&>2icture book=/T!TL&> =@/T!ST> i$ply *ed=/@/T!ST> =.7;AT/(>E6=/.7;AT/(> =.7M<@A(>Elektra=/.7M<@A(> =</!.&>D.7.=/</!.&> =(&@/>-IEJ=/(&@/> =/.*> - =.*> =T!TL&>*ed=/T!TL&> =@/T!ST>The )o$$unards=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>London=/.7M<@A(> =</!.&>D.E.=/</!.&> =(&@/>-IED=/(&@/> =/.*> - =.*> =T!TL&>6nchain $y heart=/T!TL&> =@/T!ST>Foe )ocker=/@/T!ST> =.7;AT/(>6 !=/.7;AT/(> =.7M<@A(>EMI=/.7M<@A(> =</!.&>E.7.=/</!.&> =(&@/>-IED=/(&@/> =/.*> =/.@T@L7N>

@n XML plant catalog This is a plant catalog fro$ a plant shop# stored as XML data%

=G0$l versionHC1%2C encodingHC!S7955:491C G> 9 =I99

9dited with 2.3 '-y v0$$6 Bhtt-=//www#altova#%omC

99> - =.@T@L7N> - =<L@AT> =.7MM7A>3loodroot=/.7MM7A> =B7T@A!.@L> anguinaria canadensis=/B7T@A!.@L> =P7A&>K=/P7A&>

=L!NHT>Mostly hady=/L!NHT> =</!.&>L7.KK=/</!.&> =@ @!L@B!L!T(>.(-JII=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>)olu$bine=/.7MM7A> =B7T@A!.@L>!:uilegia canadensis=/B7T@A!.@L> =P7A&>(=/P7A&> =L!NHT>Mostly hady=/L!NHT> =</!.&>LI.(D=/</!.&> =@ @!L@B!L!T(>.(.BII=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>Marsh Marigold=/.7MM7A> =B7T@A!.@L>)altha palustris=/B7T@A!.@L> =P7A&>K=/P7A&> =L!NHT>Mostly unny=/L!NHT> =</!.&>LB.E-=/</!.&> =@ @!L@B!L!T(>.J-DII=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>)owslip=/.7MM7A> =B7T@A!.@L>)altha palustris=/B7T@A!.@L> =P7A&>K=/P7A&> =L!NHT>Mostly hady=/L!NHT> =</!.&>LI.I.=/</!.&> =@ @!L@B!L!T(>.(.BII=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>%utch$an>s&3reeches=/.7MM7A> =B7T@A!.@L>%icentra cucullaria=/B7T@A!.@L> =P7A&>(=/P7A&> =L!NHT>Mostly hady=/L!NHT> =</!.&>LB.KK=/</!.&> =@ @!L@B!L!T(>.-7.II=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>;inger+ Wild=/.7MM7A> =B7T@A!.@L>!saru$ canadense=/B7T@A!.@L> =P7A&>(=/P7A&> =L!NHT>Mostly hady=/L!NHT> =</!.&>LI..(=/</!.&> =@ @!L@B!L!T(>.K-EII=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>Hepatica=/.7MM7A> =B7T@A!.@L>Hepatica a$ericana=/B7T@A!.@L> =P7A&>K=/P7A&> =L!NHT>Mostly hady=/L!NHT>

=</!.&>LK.KJ=/</!.&> =@ @!L@B!L!T(>.-7BII=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>Li'erleaf=/.7MM7A> =B7T@A!.@L>Hepatica a$ericana=/B7T@A!.@L> =P7A&>K=/P7A&> =L!NHT>Mostly hady=/L!NHT> =</!.&>L(.II=/</!.&> =@ @!L@B!L!T(>.-.7II=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>Fack&In&The&2ulpit=/.7MM7A> =B7T@A!.@L>!risae$a triphyllu$=/B7T@A!.@L> =P7A&>K=/P7A&> =L!NHT>Mostly hady=/L!NHT> =</!.&>L(.7(=/</!.&> =@ @!L@B!L!T(>.7.-II=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>Mayapple=/.7MM7A> =B7T@A!.@L>2odophyllu$ peltatu$=/B7T@A!.@L> =P7A&>(=/P7A&> =L!NHT>Mostly hady=/L!NHT> =</!.&>L7.IE=/</!.&> =@ @!L@B!L!T(>.B.JII=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>2hlo,+ Woodland=/.7MM7A> =B7T@A!.@L>2hlo, di'aricata=/B7T@A!.@L> =P7A&>(=/P7A&> =L!NHT> un or hade=/L!NHT> =</!.&>L7.E.=/</!.&> =@ @!L@B!L!T(>.-77II=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>2hlo,+ 3lue=/.7MM7A> =B7T@A!.@L>2hlo, di'aricata=/B7T@A!.@L> =P7A&>(=/P7A&> =L!NHT> un or hade=/L!NHT> =</!.&>LJ.JI=/</!.&> =@ @!L@B!L!T(>.7-BII=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A> pring&3eauty=/.7MM7A> =B7T@A!.@L>)laytonia /irginica=/B7T@A!.@L> =P7A&>D=/P7A&> =L!NHT>Mostly hady=/L!NHT> =</!.&>LB.JI=/</!.&>

=@ @!L@B!L!T(>.7.-II=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>Trilliu$=/.7MM7A> =B7T@A!.@L>Trilliu$ grandifloru$=/B7T@A!.@L> =P7A&>J=/P7A&> =L!NHT> un or hade=/L!NHT> =</!.&>L(.I.=/</!.&> =@ @!L@B!L!T(>.K7III=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>Wake *obin=/.7MM7A> =B7T@A!.@L>Trilliu$ grandifloru$=/B7T@A!.@L> =P7A&>J=/P7A&> =L!NHT> un or hade=/L!NHT> =</!.&>L(.7.=/</!.&> =@ @!L@B!L!T(>.77-II=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>/iolet+ %og&Tooth=/.7MM7A> =B7T@A!.@L>Erythroniu$ a$ericanu$=/B7T@A!.@L> =P7A&>K=/P7A&> =L!NHT> hade=/L!NHT> =</!.&>LI..K=/</!.&> =@ @!L@B!L!T(>.7.-II=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>Trout Lily=/.7MM7A> =B7T@A!.@L>Erythroniu$ a$ericanu$=/B7T@A!.@L> =P7A&>K=/P7A&> =L!NHT> hade=/L!NHT> =</!.&>LB.IK=/</!.&> =@ @!L@B!L!T(>.(7KII=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>!dder>s&Tongue=/.7MM7A> =B7T@A!.@L>Erythroniu$ a$ericanu$=/B7T@A!.@L> =P7A&>K=/P7A&> =L!NHT> hade=/L!NHT> =</!.&>LI.JE=/</!.&> =@ @!L@B!L!T(>.K-(II=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>!ne$one=/.7MM7A> =B7T@A!.@L>!ne$one blanda=/B7T@A!.@L> =P7A&>B=/P7A&> =L!NHT>Mostly hady=/L!NHT> =</!.&>LE.EB=/</!.&> =@ @!L@B!L!T(>-77BIE=/@ @!L@B!L!T(>

=/<L@AT> - =<L@AT> =.7MM7A>;recian Windflower=/.7MM7A> =B7T@A!.@L>!ne$one blanda=/B7T@A!.@L> =P7A&>B=/P7A&> =L!NHT>Mostly hady=/L!NHT> =</!.&>LI.-B=/</!.&> =@ @!L@B!L!T(>.D-.II=/@ @!L@B!L!T(> =/<L@AT> - =<L@AT> =.7MM7A>3ee 3al$=/.7MM7A> =B7T@A!.@L>Monarda didy$a=/B7T@A!.@L> =P7A&>K=/P7A&> =L!NHT> hade=/L!NHT> =</!.&>LK.JI=/</!.&> =@ @!L@B!L!T(>.J.(II=/@ @!L@B!L!T(> =/<L@AT> - =<L@AT> =.7MM7A>3erga$ot=/.7MM7A> =B7T@A!.@L>Monarda didy$a=/B7T@A!.@L> =P7A&>K=/P7A&> =L!NHT> hade=/L!NHT> =</!.&>LD.-B=/</!.&> =@ @!L@B!L!T(>.K7DII=/@ @!L@B!L!T(> =/<L@AT> - =<L@AT> =.7MM7A>3lack&Eyed usan=/.7MM7A> =B7T@A!.@L>*udbeckia hirta=/B7T@A!.@L> =P7A&>!nnual=/P7A&> =L!NHT> unny=/L!NHT> =</!.&>LI.E.=/</!.&> =@ @!L@B!L!T(>.B-EII=/@ @!L@B!L!T(> =/<L@AT> - =<L@AT> =.7MM7A>3uttercup=/.7MM7A> =B7T@A!.@L>*anunculus=/B7T@A!.@L> =P7A&>K=/P7A&> =L!NHT> hade=/L!NHT> =</!.&>L7.JD=/</!.&> =@ @!L@B!L!T(>.B-.II=/@ @!L@B!L!T(> =/<L@AT> - =<L@AT> =.7MM7A>)rowfoot=/.7MM7A> =B7T@A!.@L>*anunculus=/B7T@A!.@L> =P7A&>K=/P7A&> =L!NHT> hade=/L!NHT> =</!.&>LI.(K=/</!.&> =@ @!L@B!L!T(>.K.(II=/@ @!L@B!L!T(> =/<L@AT>

- =<L@AT> =.7MM7A>3utterfly Weed=/.7MM7A> =B7T@A!.@L>!sclepias tuberosa=/B7T@A!.@L> =P7A&>!nnual=/P7A&> =L!NHT> unny=/L!NHT> =</!.&>L7.DE=/</!.&> =@ @!L@B!L!T(>.B(.II=/@ @!L@B!L!T(> =/<L@AT> - =<L@AT> =.7MM7A>)in:uefoil=/.7MM7A> =B7T@A!.@L>2otentilla=/B7T@A!.@L> =P7A&>!nnual=/P7A&> =L!NHT> hade=/L!NHT> =</!.&>LD..B=/</!.&> =@ @!L@B!L!T(>.J7JII=/@ @!L@B!L!T(> =/<L@AT> - =<L@AT> =.7MM7A>2ri$rose=/.7MM7A> =B7T@A!.@L>0enothera=/B7T@A!.@L> =P7A&>( & J=/P7A&> =L!NHT> unny=/L!NHT> =</!.&>LB.JB=/</!.&> =@ @!L@B!L!T(>.-(.II=/@ @!L@B!L!T(> =/<L@AT> - =<L@AT> =.7MM7A>;entian=/.7MM7A> =B7T@A!.@L>;entiana=/B7T@A!.@L> =P7A&>K=/P7A&> =L!NHT> un or hade=/L!NHT> =</!.&>LD.E-=/</!.&> =@ @!L@B!L!T(>.J-EII=/@ @!L@B!L!T(> =/<L@AT> - =<L@AT> =.7MM7A>3lue ;entian=/.7MM7A> =B7T@A!.@L>;entiana=/B7T@A!.@L> =P7A&>K=/P7A&> =L!NHT> un or hade=/L!NHT> =</!.&>LE.JB=/</!.&> =@ @!L@B!L!T(>.J.7II=/@ @!L@B!L!T(> =/<L@AT> - =<L@AT> =.7MM7A>Facob>s Ladder=/.7MM7A> =B7T@A!.@L>2ole$oniu$ caeruleu$=/B7T@A!.@L> =P7A&>!nnual=/P7A&> =L!NHT> hade=/L!NHT> =</!.&>LI.7B=/</!.&> =@ @!L@B!L!T(>.77-II=/@ @!L@B!L!T(> =/<L@AT> - =<L@AT>

=.7MM7A>;reek /alerian=/.7MM7A> =B7T@A!.@L>2ole$oniu$ caeruleu$=/B7T@A!.@L> =P7A&>!nnual=/P7A&> =L!NHT> hade=/L!NHT> =</!.&>LK.(B=/</!.&> =@ @!L@B!L!T(>.D-KII=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>)alifornia 2oppy=/.7MM7A> =B7T@A!.@L>EschscholAia californica=/B7T@A!.@L> =P7A&>!nnual=/P7A&> =L!NHT> un=/L!NHT> =</!.&>LD.EI=/</!.&> =@ @!L@B!L!T(>.(7DII=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A> hooting tar=/.7MM7A> =B7T@A!.@L>%odecatheon=/B7T@A!.@L> =P7A&>!nnual=/P7A&> =L!NHT>Mostly hady=/L!NHT> =</!.&>LE.B.=/</!.&> =@ @!L@B!L!T(>.J-(II=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A> nakeroot=/.7MM7A> =B7T@A!.@L>)i$icifuga=/B7T@A!.@L> =P7A&>!nnual=/P7A&> =L!NHT> hade=/L!NHT> =</!.&>LJ.B(=/</!.&> =@ @!L@B!L!T(>.D--II=/@ @!L@B!L!T(> =/<L@AT> =<L@AT> =.7MM7A>)ardinal 4lower=/.7MM7A> =B7T@A!.@L>Lobelia cardinalis=/B7T@A!.@L> =P7A&>7=/P7A&> =L!NHT> hade=/L!NHT> =</!.&>L(..7=/</!.&> =@ @!L@B!L!T(>.777II=/@ @!L@B!L!T(> =/<L@AT> =/.@T@L7N>
@ Si$ple 3ood Menu This is a brea'fast food $enu fro$ a restaurant# stored as XML data%

=G0$l versionHC1%2C encodingHC!S7955:491C G> 9 =I99

9dited with 2.3 '-y v0$$6 Bhtt-=//www#altova#%omC


99> - =brea'fastJ$enu>

- =food> =na$e>3elgian Waffles=/na$e> =price>LJ.IJ=/price> =description>two of our fa$ous 3elgian Waffles with plenty of real $aple syrup=/description> =calories>BJ.=/calories> =/food> - =food> =na$e> trawberry 3elgian Waffles=/na$e> =price>LD.IJ=/price> =description>light 3elgian waffles co'ered with strawberries and whipped crea$=/description> =calories>I..=/calories> =/food> - =food> =na$e>3erry&3erry 3elgian Waffles=/na$e> =price>LE.IJ=/price> =description>light 3elgian waffles co'ered with an assort$ent of fresh berries and whipped crea$=/description> =calories>I..=/calories> =/food> - =food> =na$e>4rench Toast=/na$e> =price>LK.J.=/price> =description>thick slices $ade fro$ our ho$e$ade sourdough bread=/description> =calories>B..=/calories> =/food> - =food> =na$e>Ho$estyle 3reakfast=/na$e> =price>LB.IJ=/price> =description>two eggs+ bacon or sausage+ toast+ and our e'er&popular hash browns=/description> =calories>IJ.=/calories> =/food> =/brea'fastJ$enu>

Why %oes XML %isplay Like This#


XML docu$ents do not carry infor$ation about how to display the data% Since XML tags are CinventedC by the author of the XML docu$ent# browsers do not 'now if a tag li'e =table> describes an HTML table or a dining table% ,ithout any infor$ation about how to display the data# $ost browsers will "ust display the XML docu$ent as it is%

!n the ne0t chapters# we will ta'e a loo' at different solutions to the display proble$# using .SS# XSL# JavaScript# and XML *ata !slands%

(isp!a)ing XML &ith *SS


With ) ?)ascading tyle heets@ you can add display infor$ation to an XML docu$ent.

%isplaying your XML 4iles with )

!t is possible to use .SS to for$at an XML docu$ent% Below is an e0a$ple of how to use a .SS style sheet to for$at an XML docu$ent: Ta'e a loo' at this XML file: The .* catalog

=G0$l versionHC1%2C encodingHC!S7955:491C G> 9 =I99

9dited with 2.3 '-y v0$$6 Bhtt-=//www#altova#%omC


99> - =.@T@L7N> - =.*> =T!TL&>E$pire 3urles:ue=/T!TL&> =@/T!ST>3ob %ylan=/@/T!ST> =.7;AT/(>6 !=/.7;AT/(> =.7M<@A(>)olu$bia=/.7M<@A(> =</!.&>-..I.=/</!.&> =(&@/>-IEJ=/(&@/> =/.*> - =.*> =T!TL&>Hide your heart=/T!TL&> =@/T!ST>3onnie Tyler=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>)3 *ecords=/.7M<@A(> =</!.&>I.I.=/</!.&> =(&@/>-IEE=/(&@/> =/.*> - =.*> =T!TL&>;reatest Hits=/T!TL&> =@/T!ST>%olly 2arton=/@/T!ST> =.7;AT/(>6 !=/.7;AT/(> =.7M<@A(>*)!=/.7M<@A(> =</!.&>I.I.=/</!.&> =(&@/>-IE7=/(&@/> =/.*> - =.*> =T!TL&> till got the blues=/T!TL&> =@/T!ST>;ary Moore=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>/irgin records=/.7M<@A(>

=</!.&>-..7.=/</!.&> =(&@/>-II.=/(&@/> =/.*> =.*> =T!TL&>Eros=/T!TL&> =@/T!ST>Eros *a$aAAotti=/@/T!ST> =.7;AT/(>E6=/.7;AT/(> =.7M<@A(>3M;=/.7M<@A(> =</!.&>I.I.=/</!.&> =(&@/>-IID=/(&@/> =/.*> =.*> =T!TL&>0ne night only=/T!TL&> =@/T!ST>3ee ;ees=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>2olydor=/.7M<@A(> =</!.&>-..I.=/</!.&> =(&@/>-IIE=/(&@/> =/.*> =.*> =T!TL&> yl'ias Mother=/T!TL&> =@/T!ST>%r.Hook=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>)3 =/.7M<@A(> =</!.&>E.-.=/</!.&> =(&@/>-ID(=/(&@/> =/.*> =.*> =T!TL&>Maggie May=/T!TL&> =@/T!ST>*od tewart=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>2ickwick=/.7M<@A(> =</!.&>E.J.=/</!.&> =(&@/>-II.=/(&@/> =/.*> =.*> =T!TL&>*o$anAa=/T!TL&> =@/T!ST>!ndrea 3ocelli=/@/T!ST> =.7;AT/(>E6=/.7;AT/(> =.7M<@A(>2olydor=/.7M<@A(> =</!.&>-..E.=/</!.&> =(&@/>-IIB=/(&@/> =/.*> =.*> =T!TL&>When a $an lo'es a wo$an=/T!TL&> =@/T!ST>2ercy ledge=/@/T!ST> =.7;AT/(>6 !=/.7;AT/(> =.7M<@A(>!tlantic=/.7M<@A(> =</!.&>E.D.=/</!.&>

=(&@/>-IED=/(&@/> =/.*> =.*> =T!TL&>3lack angel=/T!TL&> =@/T!ST> a'age *ose=/@/T!ST> =.7;AT/(>E6=/.7;AT/(> =.7M<@A(>Mega=/.7M<@A(> =</!.&>-..I.=/</!.&> =(&@/>-IIJ=/(&@/> =/.*> =.*> =T!TL&>-III ;ra$$y 8o$inees=/T!TL&> =@/T!ST>Many=/@/T!ST> =.7;AT/(>6 !=/.7;AT/(> =.7M<@A(>;ra$$y=/.7M<@A(> =</!.&>-..7.=/</!.&> =(&@/>-III=/(&@/> =/.*> =.*> =T!TL&>4or the good ti$es=/T!TL&> =@/T!ST>"enny *ogers=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>Mucik Master=/.7M<@A(> =</!.&>E.D.=/</!.&> =(&@/>-IIJ=/(&@/> =/.*> =.*> =T!TL&>3ig Willie style=/T!TL&> =@/T!ST>Will $ith=/@/T!ST> =.7;AT/(>6 !=/.7;AT/(> =.7M<@A(>)olu$bia=/.7M<@A(> =</!.&>I.I.=/</!.&> =(&@/>-IID=/(&@/> =/.*> =.*> =T!TL&>Tupelo Honey=/T!TL&> =@/T!ST>/an Morrison=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>2olydor=/.7M<@A(> =</!.&>E.7.=/</!.&> =(&@/>-ID-=/(&@/> =/.*> =.*> =T!TL&> ouls'ille=/T!TL&> =@/T!ST>Forn Hoel=/@/T!ST> =.7;AT/(>8orway=/.7;AT/(> =.7M<@A(>WE!=/.7M<@A(> =</!.&>D.I.=/</!.&> =(&@/>-IIB=/(&@/>

=/.*> - =.*> =T!TL&>The 'ery best of=/T!TL&> =@/T!ST>)at te'ens=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>Island=/.7M<@A(> =</!.&>E.I.=/</!.&> =(&@/>-II.=/(&@/> =/.*> - =.*> =T!TL&> top=/T!TL&> =@/T!ST> a$ 3rown=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>! and M=/.7M<@A(> =</!.&>E.I.=/</!.&> =(&@/>-IEE=/(&@/> =/.*> - =.*> =T!TL&>3ridge of pies=/T!TL&> =@/T!ST>T>2au=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(> iren=/.7M<@A(> =</!.&>D.I.=/</!.&> =(&@/>-IED=/(&@/> =/.*> - =.*> =T!TL&>2ri'ate %ancer=/T!TL&> =@/T!ST>Tina Turner=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>)apitol=/.7M<@A(> =</!.&>E.I.=/</!.&> =(&@/>-IE(=/(&@/> =/.*> - =.*> =T!TL&>Midt o$ natten=/T!TL&> =@/T!ST>"i$ Larsen=/@/T!ST> =.7;AT/(>E6=/.7;AT/(> =.7M<@A(>Medley=/.7M<@A(> =</!.&>D.E.=/</!.&> =(&@/>-IE(=/(&@/> =/.*> - =.*> =T!TL&>2a'arotti ;ala )oncert=/T!TL&> =@/T!ST>Luciano 2a'arotti=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>%E))!=/.7M<@A(> =</!.&>I.I.=/</!.&> =(&@/>-II-=/(&@/> =/.*>

- =.*> =T!TL&>The dock of the bay=/T!TL&> =@/T!ST>0tis *edding=/@/T!ST> =.7;AT/(>6 !=/.7;AT/(> =.7M<@A(>!tlantic=/.7M<@A(> =</!.&>D.I.=/</!.&> =(&@/>-IED=/(&@/> =/.*> - =.*> =T!TL&>2icture book=/T!TL&> =@/T!ST> i$ply *ed=/@/T!ST> =.7;AT/(>E6=/.7;AT/(> =.7M<@A(>Elektra=/.7M<@A(> =</!.&>D.7.=/</!.&> =(&@/>-IEJ=/(&@/> =/.*> - =.*> =T!TL&>*ed=/T!TL&> =@/T!ST>The )o$$unards=/@/T!ST> =.7;AT/(>6"=/.7;AT/(> =.7M<@A(>London=/.7M<@A(> =</!.&>D.E.=/</!.&> =(&@/>-IED=/(&@/> =/.*> - =.*> =T!TL&>6nchain $y heart=/T!TL&> =@/T!ST>Foe )ocker=/@/T!ST> =.7;AT/(>6 !=/.7;AT/(> =.7M<@A(>EMI=/.7M<@A(> =</!.&>E.7.=/</!.&> =(&@/>-IED=/(&@/> =/.*> =/.@T@L7N>

Then loo' at this style sheet: The .SS file

><T<3(: E ba%kgro/nd)%olor= FffffffG width= "$$HG I >D E dis-lay= blo%kG margin)bottom= 4$-tG margin)left= $G I T&T39 E

%olor= F11$$$$G font)siJe= 0$-tG I <RT&'T E %olor= F$$$$11G font)siJe= 0$-tG I >(KATR?L@R&>9L?9<RL>(.@<A? E dis-lay= blo%kG %olor= F$$$$$$G margin)left= 0$-tG I

3inally# view: The .* catalog for$atted with the .SS file

Below is a fraction of the XML file% The second line# M#,$l&stylesheet typeN5te,t1css5 hrefN5cdHcatalog.css5#O # lin's the XML file to the .SS file:

<?xml version !"#$! en%oding !&'()**+,)"!?> <?xml)stylesheet ty-e !text/%ss! href !%dM%atalog#%ss!?> <><T<3(:> <>D> <T&T39>9m-ire N/rlesO/e</T&T39>

<<RT&'T>Nob Dylan</<RT&'T> <>(KATR?>K'<</>(KATR?> <>(.@<A?>>ol/mbia</>(.@<A?> <@R&>9>"$#,$</@R&>9> <?9<R>",*+</?9<R> </>D> <>D> <T&T39>8ide yo/r heart</T&T39> <<RT&'T>Nonnie Tyler</<RT&'T> <>(KATR?>KP</>(KATR?> <>(.@<A?>>N' Re%ords</>(.@<A?> <@R&>9>,#,$</@R&>9> <?9<R>",**</?9<R> </>D> # # # # </><T<3(:>
8ote< 3or$atting XML with .SS is A7T the future of how to style XML docu$ents% XML docu$ent should be styled by using the ,-.Fs XSL standardI

*isplaying XML with XSL


With X L you can add display infor$ation to your XML docu$ent.

%isplaying XML with X L


X L is the preferred style sheet language of XML. XSL )the eXtensible Stylesheet Language+ is far $ore sophisticated than .SS% 7ne way to use XSL is to transfor$ XML into HTML before it is displayed by the browser as de$onstrated in these e0a$ples: iew the XML file#

the XSL style sheet#

and

iew the result%

Below is a fraction of the XML file% The second line# M#,$l&stylesheet typeN5te,t1,sl5 hrefN5si$ple.,sl5#O # lin's the XML file to the XSL file:

<?xml version !"#$! en%oding !&'()**+,)"!?> <?xml)stylesheet ty-e !text/xsl! href !sim-le#xsl!?> <breakfastMmen/> <food> <name>Nelgian 7affles</name> <-ri%e>Q+#,+</-ri%e> <des%ri-tion> two of o/r famo/s Nelgian 7affles </des%ri-tion> <%alories>5+$</%alories> </food> </breakfastMmen/>
!f you want to learn $ore about XSL# please visit our XSL tutorial%

XML (ata s!and


With Internet E,plorer+ the unofficial M,$lO tag can be used to create an XML data island.

XML %ata E$bedded in HTML

@n XML data island is XML data e$bedded into an HTML page% Here is how it wor'sQ assu$e we have the following XML docu$ent )Cnote%0$lC+:

<?xml version !"#$! en%oding !&'()**+,)"!?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
Then# in an HTML docu$ent# you can e$bed the XML file above with the =0$l> tag% The id attribute of the =0$l> tag defines an !* for the data island# and the src attribute points to the XML file to e$bed:

<html> <body> <xml id !note! sr% !note#xml!></xml> </body> </html>


However# the e$bedded XML data is# up to this point# not visible for the user% The ne0t step is to for$at and display the data in the data island by binding it to HTML ele$ents%

3ind %ata Island to HTML Ele$ents


!n the ne0t e0a$ple# we will e$bed an XML file called CcdJcatalog%0$lC into an HTML file% iew CcdJcatalog%0$lC%

The HTML file loo's li'e this:

<html> <body> <xml id !%d%at! sr% !%dM%atalog#xml!></xml> <table border !"! datasr% !F%d%at!> <tr> <td><s-an datafld !<RT&'T!></s-an></td> <td><s-an datafld !T&T39!></s-an></td> </tr> </table> </body> </html>
&0a$ple e0plained: The datasrc attribute of the =table> tag binds the HTML table ele$ent to the XML data island% The datasrc attribute refers to the id attribute of the data island% =td> tags cannot be bound to data# so we are using =span> tags% The =span> tag allows the datafld attribute to refer to the XML ele$ent to be displayed% !n this case# it is datafldHC@/T!STC for the =@/T!ST> ele$ent and datafldHCT!TL&C for the =T!TL&> ele$ent in the XML file% @s the XML is read# additional rows are created for each =.*> ele$ent% !f you are running !& :%2 or higher# you can try it yourself%

@lso try this e0a$ple# de$onstrating =thead># =tbody># and =tfoot>%

XML in +ea! Li,e


! real&life e,a$ple of how XML can be used to carry infor$ation.

E,a$ple< XML 8ews


XML8ews is a specification for e,changing news and other infor$ation. ;sing such a standard $a'es it easier for both news producers and news consu$ers to produce# receive# and archive any 'ind of news infor$ation across different hardware# software# and progra$$ing languages% @n e0a$ple XMLAews docu$ent:

<?xml version !"#$! en%oding !&'()**+,)"!?> <nitf> <head> <title>>olombia 9arthO/ake</title> </head> <body> <headline> <hl">"R4 Dead in >olombia 9arthO/ake</hl"> </headline> <byline> <bytag>Ny Jared PotlerL <sso%iated @ress 7riter</bytag> </byline> <dateline> <lo%ation>NogotaL >olombia</lo%ation> <date>.onday Jan/ary 0+ ",,, 6=0* 9T</date> </dateline> </body> </nitf>

XML <arser
To read and update+ create and $anipulate an XML docu$ent+ you will need an XML parser.

E,a$ples
<arse an XML file 9 .rossbrowser e0a$ple This e0a$ple is a cross9browser e0a$ple that loads an e0isting XML docu$ent )Cnote%0$lC+ into the XML parser% <arse an XML string 9 .rossbrowser e0a$ple This e0a$ple is a cross9browser e0a$ple on how to load and parse an XML string%

2arsing XML %ocu$ents


To $anipulate an XML docu$ent# you need an XML parser% The parser loads the docu$ent into your co$puterFs $e$ory% 7nce the docu$ent is loaded# its data can be $anipulated using the *7M% The *7M treats the XML docu$ent as a tree% To learn $ore about the XML *7M# please read our XML *7M tutorial% There are so$e differences between MicrosoftFs XML parser and the XML parser used in MoDilla browsers% !n this tutorial we will show you how to create cross browser scripts that will wor' in both !nternet &0plorer and MoDilla browsers%

Microsoft>s XML 2arser


MicrosoftFs XML parser is a .7M co$ponent that co$es with !nternet &0plorer : and higher% 7nce you have installed !nternet &0plorer# the parser is available to scripts% MicrosoftFs XML parser supports all the necessary functions to traverse the node tree# access the nodes and their attribute values# insert and delete nodes# and convert the node tree bac' to XML% To create an instance of MicrosoftFs XML parser# use the following code: JavaScript:

var xmlDo% new <%tive2(bSe%tB!.i%rosoft#2.3D(.!CG


BScript:

set xmlDo% >reate(bSe%tB!.i%rosoft#2.3D(.!C


@S<:

set xmlDo% 'erver#>reate(bSe%tB!.i%rosoft#2.3D(.!C


The following code frag$ent loads an e0isting XML docu$ent )Cnote%0$lC+ into MicrosoftFs XML parser:

var xmlDo% new <%tive2(bSe%tB!.i%rosoft#2.3D(.!CG xmlDo%#asyn% !false!G xmlDo%#loadB!note#xml!CG


The first line of the script above creates an instance of the XML parser% The second line turns off asynchroniDed loading# to $a'e sure that the parser will not continue e0ecution of the script before the docu$ent is fully loaded% The third line tells the parser to load an XML docu$ent called Cnote%0$lC%

XML 2arser in MoAilla+ 4irefo,+ and 0pera


MoDillaFs XML parser supports all the necessary functions to traverse the node tree# access the nodes and their attribute values# insert and delete nodes# and convert the node tree bac' to XML%

To create an instance of the XML parser in MoDilla browsers# use the following code: JavaScript:

var xmlDo% do%/ment#im-lementation#%reateDo%/mentB!ns!L!root!Ln/llCG


The first para$eter# ns# defines the na$espace used for the XML docu$ent% The second para$eter# root# is the XML root ele$ent in the XML file% The third para$eter# null# is always null because it is not i$ple$ented yet% The following code frag$ent loads an e0isting XML docu$ent )Cnote%0$lC+ into MoDillasF XML parser:

var xmlDo% do%/ment#im-lementation#%reateDo%/mentB!!L!!Ln/llCG xmlDo%#loadB!note#xml!CG


The first line of the script above creates an instance of the XML parser% The second line tells the parser to load an XML docu$ent called Cnote%0$lC%

2arsing an XML 4ile & ! )ross browser E,a$ple


The following e0a$ple is a cross browser e0a$ple that loads an e0isting XML docu$ent )Cnote%0$lC+ into the XML parser:

<html> <head> <s%ri-t ty-e !text/Savas%ri-t!> var xmlDo%G f/n%tion load2.3BC E // %ode for &9 if Bwindow#<%tive2(bSe%tC E xmlDo% new <%tive2(bSe%tB!.i%rosoft#2.3D(.!CG xmlDo%#asyn% falseG xmlDo%#loadB!note#xml!CG getmessageBCG I // %ode for .oJillaL 1irefoxL (-eraL et%# else if Bdo%/ment#im-lementation TT do%/ment#im-lementation#%reateDo%/mentC E xmlDo% do%/ment#im-lementation#%reateDo%/mentB!!L!!Ln/llCG xmlDo%#loadB!note#xml!CG xmlDo%#onload getmessageG I else E alertB'?o/r browser %annot handle this s%ri-t'CG I I f/n%tion getmessageBC E do%/ment#get9lementNy&dB!to!C#inner8T.3 xmlDo%#get9lementsNyTagAameB!to!CU$V#%hildAodesU$V#nodeWal/eG do%/ment#get9lementNy&dB!from!C#inner8T.3

xmlDo%#get9lementsNyTagAameB!from!CU$V#%hildAodesU$V#nodeWal/eG do%/ment#get9lementNy&dB!message!C#inner8T.3 xmlDo%#get9lementsNyTagAameB!body!CU$V#%hildAodesU$V#nodeWal/eG I </s%ri-t> </head> <body onload !load2.3BC!> <h">74'%hools &nternal Aote</h"> <-><b>To=</b> <s-an id !to!></s-an><br /> <b>1rom=</b> <s-an id !from!></s-an><br /> <b>.essage=</b> <s-an id !message!></s-an> </-> </body> </html>
7utput:

-.Schoo!s nterna! Note


To< Tove 4ro$< Jani Message< *onFt forget $e this wee'endI

I$portant 8ote
To e0tract the te0t )Jani+ fro$ an XML ele$ent li'e: =fro$>Jani=/fro$># the correct synta0 is:

get9lementsNyTagAameB!from!CU$V#%hildAodesU$V#nodeWal/e
IM20*T!8T< get&le$entsByTagAa$e returns an array of nodes% The array contains all ele$ents with the specified na$e within the XML docu$ent% !n this case there is only one Cfro$C ele$ent# but you still have to specify the array inde0 ) R2S +%

2arsing an XML

tring & ! )ross browser E,a$ple

The following code is a cross9browser e0a$ple on how to load and parse an XML string:

<html> <body> <s%ri-t ty-e !text/Savas%ri-t!> var text !<note>!G text textX!<to>Tove</to>!G text textX!<from>Jani</from>!G text textX!<heading>Reminder</heading>!G text textX!<body>Don't forget me this weekend!</body>!G text textX!</note>!G // %ode for &9 if Bwindow#<%tive2(bSe%tC E var do% new <%tive2(bSe%tB!.i%rosoft#2.3D(.!CG do%#asyn% !false!G do%#load2.3BtextCG

I // %ode for .oJillaL 1irefoxL (-eraL et%# else E var -arser new D(.@arserBCG var do% -arser#-arse1rom'tringBtextL!text/xml!CG I // do%/ment9lement always re-resents the root node var x do%#do%/ment9lementG do%/ment#writeB!Text of first %hild element= !CG do%/ment#writeBx#%hildAodesU$V#%hildAodesU$V#nodeWal/eCG do%/ment#writeB!<br />!CG do%/ment#writeB!Text of se%ond %hild element= !CG do%/ment#writeBx#%hildAodesU"V#%hildAodesU$V#nodeWal/eCG </s%ri-t> </body> </html>
7utput: Te0t of first child ele$ent: Tove Te0t of second child ele$ent: Jani 8ote< !nternet &0plorer uses the loadXML)+ $ethod to parse an XML string# while MoDilla browsers uses the *7M<arser ob"ect%

XML Aa$espaces
XML 8a$espaces pro'ide a $ethod to a'oid ele$ent na$e conflicts.

8a$e )onflicts
Since ele$ent na$es in XML are not predefined# a na$e conflict will occur when two different docu$ents use the sa$e ele$ent na$es% This XML docu$ent carries infor$ation in a table:

<table> <tr> <td><--les</td> <td>Nananas</td> </tr> </table>


This XML docu$ent carries infor$ation about a table )a piece of furniture+:

<table> <name><fri%an >offee Table</name> <width>*$</width> <length>"0$</length> </table>

!f these two XML docu$ents were added together# there would be an ele$ent na$e conflict because both docu$ents contain a =table> ele$ent with different content and definition%

ol'ing 8a$e )onflicts 6sing a 2refi,


This XML docu$ent carries infor$ation in a table:

<h=table> <h=tr> <h=td><--les</h=td> <h=td>Nananas</h=td> </h=tr> </h=table>


This XML docu$ent carries infor$ation about a piece of furniture:

<f=table> <f=name><fri%an >offee Table</f=name> <f=width>*$</f=width> <f=length>"0$</f=length> </f=table>


Aow there will be no na$e conflict because the two docu$ents use a different na$e for their =table> ele$ent )=h:table> and =f:table>+% By using a prefi0# we have created two different types of =table> ele$ents%

6sing 8a$espaces
This XML docu$ent carries infor$ation in a table:

<h=table xmlns=h !htt-=//www#w4#org/TR/htmlR/!> <h=tr> <h=td><--les</h=td> <h=td>Nananas</h=td> </h=tr> </h=table>


This XML docu$ent carries infor$ation about a piece of furniture:

<f=table xmlns=f !htt-=//www#w4s%hools#%om/f/rnit/re!> <f=name><fri%an >offee Table</f=name> <f=width>*$</f=width> <f=length>"0$</f=length> </f=table>


!nstead of using only prefi0es# we have added an 0$lns attribute to the =table> tag to give the prefi0 a Eualified na$e associated with a na$espace%

The XML 8a$espace ?,$lns@ !ttribute

The XML na$espace attribute is placed in the start tag of an ele$ent and has the following synta0:

xmlns=namespace-prefix !namespaceURI!
,hen a na$espace is defined in the start tag of an ele$ent# all child ele$ents with the sa$e prefi0 are associated with the sa$e na$espace% Aote that the address used to identify the na$espace is not used by the parser to loo' up infor$ation% The only purpose is to give the na$espace a uniEue na$e% However# very often co$panies use the na$espace as a pointer to a real ,eb page containing infor$ation about the na$espace% Try to go to http://www%w-%org/T//ht$l?/%

6nifor$ *esource Identifier ?6*I@


@ 6nifor$ *esource Identifier );/!+ is a string of characters which identifies an !nternet /esource% The $ost co$$on ;/! is the 6nifor$ *esource Locator );/L+ which identifies an !nternet do$ain address% @nother# not so co$$on type of ;/! is the 6ni'ersal *esource 8a$e );/A+% !n our e0a$ples we will only use ;/Ls%

%efault 8a$espaces
*efining a default na$espace for an ele$ent saves us fro$ using prefi0es in all the child ele$ents% !t has the following synta0:

xmlns !namespaceURI!
This XML docu$ent carries infor$ation in a table:

<table xmlns !htt-=//www#w4#org/TR/htmlR/!> <tr> <td><--les</td> <td>Nananas</td> </tr> </table>


This XML docu$ent carries infor$ation about a piece of furniture:

<table xmlns !htt-=//www#w4s%hools#%om/f/rnit/re!> <name><fri%an >offee Table</name> <width>*$</width> <length>"0$</length> </table>

8a$espaces in *eal 6se


,hen you start using XSL# you will soon see na$espaces in real use% XSL style sheets are used to transfor$ XML docu$ents into other for$ats# li'e HTML%

!f you ta'e a close loo' at the XSL docu$ent below# you will see that $ost of the tags are HTML tags% The tags that are not HTML tags have the prefi0 0sl# identified by the na$espace Chttp://www%w-%org/1444/XSL/Transfor$C:

<?xml version !"#$! en%oding !&'()**+,)"!?> <xsl=stylesheet version !"#$! xmlns=xsl !htt-=//www#w4#org/",,,/2'3/Transform!> <xsl=tem-late mat%h !/!> <html> <body> <h0>.y >D >olle%tion</h0> <table border !"!> <tr> <th align !left!>Title</th> <th align !left!><rtist</th> </tr> <xsl=for)ea%h sele%t !%atalog/%d!> <tr> <td><xsl=val/e)of sele%t !title!/></td> <td><xsl=val/e)of sele%t !artist!/></td> </tr> </xsl=for)ea%h> </table> </body> </html> </xsl=tem-late> </xsl=stylesheet>

XML *(#T#
!ll te,t in an XML docu$ent will be parsed by the parser. 0nly te,t inside a )%!T! section will be ignored by the parser.

2arsed %ata
XML parsers nor$ally parse all the te,t in an XML docu$ent. ,hen an XML ele$ent is parsed# the te0t between the XML tags is also parsed:

<message>This text is also -arsed</message>


The parser does this because XML ele$ents can contain other ele$ents# as in this e0a$ple# where the =na$e> ele$ent contains two other ele$ents )first and last+:

<name><first>Nill</first><last>:ates</last></name>
and the parser will brea' it up into sub9ele$ents li'e this:

<name> <first>Nill</first> <last>:ates</last> </name>

Escape )haracters
Illegal XML characters ha'e to be replaced by entity references. !f you place a character li'e C=C inside an XML ele$ent# it will generate an error because the parser interprets it as the start of a new ele$ent% (ou cannot write so$ething li'e this:

<message>if salary < "$$$ then</message>


To avoid this# you have to replace the C=C character with an entity reference# li'e this:

<message>if salary TltG "$$$ then</message>


There are : predefined entity references in XML: TltQ TgtQ Ta$pQ TaposQ TEuotQ = > T F C less than greater than a$persand apostrophe Euotation $ar'

8ote< 7nly the characters C=C and CTC are strictly illegal in XML% @postrophes# Euotation $ar's and greater than signs are legal# but it is a good habit to replace the$%

)%!T!
E'erything inside a )%!T! section is ignored by the parser. !f your te0t contains a lot of C=C or CTC characters 9 as progra$ code often does 9 the XML ele$ent can be defined as a .*@T@ section% @ .*@T@ section starts with CMGP)%!T!PC and ends with CQQOC:

<s%ri-t> <!U>D<T<U f/n%tion mat%hwoBaLbC E if Ba < b TT a < $C then E ret/rn " I else E ret/rn $ I I VV> </s%ri-t>
!n the e0a$ple above# everything inside the .*@T@ section is ignored by the parser%

Notes on *(#T# sections/


@ .*@T@ section cannot contain the string CSS>C# therefore# nested .*@T@ sections are not allowed% @lso $a'e sure there are no spaces or line brea's inside the CSS>C string%

XML Encoding
XML docu$ents $ay contain foreign characters+ like 8orwegian R S T + or 4rench U V W. To let your XML parser understand these characters+ you should sa'e your XML docu$ents as 6nicode.

Windows 7... 8otepad


,indows 8222 Aotepad can save files as ;nicode% Save the XML file below as ;nicode )note that the docu$ent does not contain any encoding attribute+:

<?xml version !"#$!?> <note> <from>Jani</from> <to>Tove</to> <message>Aorwegian= YZ[# 1ren%h= \]^</message> </note>
The file above# noteJencodeJnoneJu%0$l will A7T generate an error in !& :O# 3irefo0# or 7pera# but it ,!LL generate an error in Aetscape 6%8%

Windows 7... 8otepad with Encoding


,indows 8222 Aotepad files saved as ;nicode use C;T3916C encoding% !f you add an encoding attribute to XML files saved as ;nicode# windows encoding values will generate an error% The following encoding )open it+# will A7T give an error $essage:

<?xml version !"#$! en%oding !windows)"0+0!?>


The following encoding )open it+# will A7T give an error $essage:

<?xml version !"#$! en%oding !&'()**+,)"!?>


The following encoding )open it+# will A7T give an error $essage:

<?xml version !"#$! en%oding !KT1)*!?>

The following encoding )open it+# will A7T generate an error in !& :O# 3irefo0# or 7pera# but it ,!LL generate an error in Aetscape 6%8%

<?xml version !"#$! en%oding !KT1)"5!?>

Error Messages
!f you try to load an XML docu$ent into !nternet &0plorer# you can get two different errors indicating encoding proble$s: !n in'alid character was found in te,t content. (ou will get this error $essage if a character in the XML docu$ent does not $atch the encoding attribute% Aor$ally you will get this error $essage if your XML docu$ent contains CforeignC characters# and the file was saved with a single9byte encoding editor li'e Aotepad# and no encoding attribute was specified% witch fro$ current encoding to specified encoding not supported. (ou will get this error $essage if your file was saved as ;nicode/;T3916 but the encoding attribute specified a single9byte encoding li'e ,indows918:8# !S7955:491 or ;T395% (ou can also get this error $essage if your docu$ent was saved with single9byte encoding# but the encoding attribute specified a double9byte encoding li'e ;T3916%

)onclusion
The conclusion is that the encoding attribute has to specify the encoding used when the docu$ent was saved% My best advice to avoid errors is:

;se an editor that supports encoding Ma'e sure you 'now what encoding it uses ;se the sa$e encoding attribute in your XML docu$ents

XML on the Server


XML can be generated on a ser'er without installing any XML controls.

toring XML on the

er'er

XML files can be stored on an !nternet server e0actly the sa$e way as HTML files% Start ,indows Aotepad and write the following lines:

<?xml version !"#$! en%oding !&'()**+,)"!?> <note> <from>Jani</from> <to>Tove</to> <message>Remember me this weekend</message> </note>
Save the file on your web server with a proper na$e li'e Cnote%0$lC%

;enerating XML with ! 2


XML can be generated on a ser'er without any installed XML software. To generate an XML response fro$ the server 9 si$ply write the following code and save it as an @S< file on the web server:

<H res-onse#>ontentTy-e !text/xml! res-onse#7riteB!<?xml version '"#$' en%oding '&'()**+,)"'?>!C res-onse#7riteB!<note>!C res-onse#7riteB!<from>Jani</from>!C res-onse#7riteB!<to>Tove</to>!C res-onse#7riteB!<message>Remember me this weekend</message>!C res-onse#7riteB!</note>!C H>
Aote that the content type of the response $ust be set to Cte0t/0$lC% See how the @S< file will be returned fro$ the server% !f you donFt 'now how to write @S<# please visit our @S< tutorial

;etting XML 4ro$ a %atabase


XML can be generated fro$ a database without any installed XML software. To generate an XML database response fro$ the server# si$ply write the following code and save it as an @S< file on the web server:

<H res-onse#>ontentTy-e !text/xml! set %onn 'erver#>reate(bSe%tB!<D(DN#>onne%tion!C %onn#-rovider !.i%rosoft#Jet#(39DN#R#$G! %onn#o-en server#ma--athB!/db/database#mdb!C sOl !sele%t fnameLlname from tbl:/estNook! set rs >onn#9xe%/teBsOlC rs#.ove1irstBC res-onse#writeB!<?xml version '"#$' en%oding '&'()**+,)"'?>!C res-onse#writeB!<g/estbook>!C while Bnot rs#9(1C res-onse#writeB!<g/est>!C res-onse#writeB!<fname>! T rsB!fname!C T !</fname>!C res-onse#writeB!<lname>! T rsB!lname!C T !</lname>!C res-onse#writeB!</g/est>!C rs#.oveAextBC wend rs#%loseBC %onn#%loseBC res-onse#writeB!</g/estbook>!C H>
See the real life database output fro$ the @S< file above%

The e0a$ple above uses @S< with @*7% !f you donFt 'now how to use @*7# please visit our @*7 tutorial%

XML @pplication

You might also like