What You Should Already Know: Home Page
What You Should Already Know: Home Page
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
Before you continue you should have a basic understanding of the following:
!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 /ersions
XML 1.0
XML 1%2 was released as a ,-. /eco$$endation 12% 3ebruary 1445%
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%
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
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
XML can
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%
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%
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 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 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+:
</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
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:
!n HTML so$e ele$ents can be i$properly nested within each other li'e this:
<?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%
pace is 2reser'ed
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%
)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
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.
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 Synta0
<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$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:
9uote
@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!> <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>
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>
<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.
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>
<?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
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.
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%
!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
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
99> - =note> =to>To'e=/to> =fro$>Fani=/fro$> =heading>*e$inder=/heading> =body>%on>t forget $e this weekendG=/body> =/note>
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
=.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%
=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%
- =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>
!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%
!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
=</!.&>-..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>
><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
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
and
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%
@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 !%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%
<?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%
To create an instance of the XML parser in MoDilla browsers# use the following code: JavaScript:
<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:
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
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:
!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%
6sing 8a$espaces
This XML docu$ent carries infor$ation in a table:
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?/%
%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:
!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:
<name><first>Nill</first><last>:ates</last></name>
and the parser will brea' it up into sub9ele$ents li'e this:
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:
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%
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.
<?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%
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%
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
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%
<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
<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