Sie sind auf Seite 1von 60

Introduction to XML

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 Should 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 Sche#a to describe the data
XML with a *T* or XML Sche$a is designed to be self%descripti&e
XML is a ,-. /eco$$endation
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%
The #ain difference between 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 does not $* anything
1
XML was not designed to $* anything.
Maybe it is a little hard to understand# but XML does not *6 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 *6 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 free and e+tensible
XML tags are not predefined. You #ust ,in&ent, 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 7p8#
7h18# 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 7to8 and 7fro$8+ are not defined in any XML standard% These
tags are 9invented9 by the author of the XML docu$ent%
XML is a co#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 future Web de&elop#ent
XML is going to be e&erywhere.
,e have been participating in XML develop$ent since its creation% !t has been a$a:ing to see how
;uic'ly the XML standard has been developed and how ;uic'ly a large nu$ber of software vendors
have adopted the standard%
2
,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%
XML .oke
<uestion: ,hen should ! use XML=
>nswer: ,hen you need a bu::word in your resu$e%
How can XML be /sed"
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 Separate $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 re;uire any changes to your HTML%
XML data can also be stored inside HTML pages as 9*ata !slands9% (ou can still concentrate on using
HTML only for for$atting and displaying the data%
XML is used to 0+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% 6ne of the
$ost ti$e?consu$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 121
With XML- financial infor#ation can be e+changed o&er the Internet.
&0pect to see a lot about XML and B@B )Business To Business+ in the near future%
3
XML is going to be the $ain language for e0changing financial infor$ation between businesses over
the !nternet% > lot of interesting B@B applications are under develop$ent%
XML can be used to Share $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 software? and hardware?independent
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 can be used to Store $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 can #ake your $ata #ore /seful
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%
6ther 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 9reading $achines9 )agents+# and it is
easier to $a'e your data available for blind people# or people with other disabilities%
XML can be used to (reate new Languages
XML is the #other of W3 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%
If $e&elopers ha&e Sense
If they $* ha&e sense- all future applications will e+change their data in XML.
4
The future $ight give us word processors# spreadsheet applications and databases that can read
each otherAs 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 Synta+
The synta+ rules of XML are &ery si#ple and &ery strict. The rules are &ery easy to learn-
and &ery easy to use.
1ecause of this- creating software that can read and #anipulate XML is &ery easy.
n e+a#ple XML docu#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 ? the XML declaration ? 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 !S6?55B4?1 )Latin?1/,est &uropean+ character set%
The ne0t line describes the root ele$ent of the docu$ent )li'e it was saying: 9this docu$ent is a
note9+:
<note>
The ne0t C lines describe C 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 Dote to Tove fro$ Jani= *onAt
you agree that XML is pretty self?descriptive=
5
ll XML ele#ents #ust ha&e a closing 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</->
4ote: (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 case sensiti&e
/nlike HTML- XML tags are case sensiti&e.
,ith XML# the tag 7Letter8 is different fro$ the tag 7letter8%
6pening 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>
ll XML ele#ents #ust be properly nested
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>
ll XML docu#ents #ust ha&e a root 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%
6
>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>
ttribute &alues #ust always be 5uoted
With XML- it is illegal to o#it 5uotation #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 ;uoted% 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 ;uoted%
This is correct: dateE91@/11/@22@9% This is incorrect: dateE1@/11/@22@%
With XML- white space is preser&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- () 6 L7 is con&erted to L7
7
With XML- a new line is always stored as L7%
*o you 'now what a typewriter is= ,ell# a typewriter is a $echanical device which was used last
century to produce printed docu$ents% :?+
>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 Fni0 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%
7G?? This is a co$$ent ??8
There is nothing special about 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 XML?aware 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 0le#ents
XML 0le#ents are e+tensible and they ha&e relationships.
XML 0le#ents ha&e si#ple na#ing rules.
XML 0le#ents are 0+tensible
XML docu#ents can be e+tended to carry #ore infor#ation.
Loo' at the following XML D6T& e0a$ple:
<note>
<to>Tove</to>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
8
</note>
LetAs i$agine that we created an application that e0tracted the 7to8# 7fro$8# and 7body8
ele$ents fro$ the XML docu$ent to produce this output:
M0SS80
To9 Tove
7ro#9 Jani
*onAt forget $e this wee'endG
!$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 crash=
Do% The application should still be able to find the 7to8# 7fro$8# and 7body8 ele$ents in the XML
docu$ent and produce the sa$e output%
XML docu#ents are 0+tensible.
XML 0le#ents ha&e )elationships
0le#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 7irst 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
9
!$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%
0le#ents ha&e (ontent
0le#ents can ha&e different content types.
>n XML ele#ent is everything fro$ )including+ the ele$entAs start tag to )including+ the ele$entAs
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% Hara has si#ple content )or
te+t content+ because it contains only te0t% Hrod 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 9--?IBJ9% The attribute na$ed $edia has the &alue 9paper9%
0le#ent 4a#ing
XML ele#ents #ust follow these na#ing rules9
Da$es can contain letters# nu$bers# and other characters
Da$es $ust not start with a nu$ber or punctuation character
Da$es $ust not start with the letters 0$l )or XML or X$l %%+
Da$es cannot contain spaces
Ta'e care when you 9invent9 ele$ent na$es and follow these si$ple rules:
10
>ny na$e can be used# no words are reserved# but the idea is to $a'e na$es descriptive% Da$es
with an underscore separator are nice%
&0a$ples: 7firstKna$e8# 7lastKna$e8%
>void 9?9 and 9%9 in na$es% 3or e0a$ple# if you na$e so$ething 9first?na$e#9 it could be a $ess if
your software tries to subtract na$e fro$ first% 6r if you na$e so$ething 9first%na$e#9 your
software $ay thin' that 9na$e9 is a property of the ob"ect 9first%9
&le$ent na$es can be as long as you li'e# but donAt e0aggerate% Da$es should be short and
si$ple# li'e this: 7boo'Ktitle8 not li'e this: 7theKtitleKofKtheKboo'8%
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%
Don?&nglish letters li'e LMN are perfectly legal in XML ele$ent na$es# but watch out for proble$s if
your software vendor doesnAt support the$%
The 9:9 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 ttributes
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: 7!MO S/.E9co$puter%gif98% The S/. attribute provides
additional infor$ation about the !MO 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>
;uote Styles- ,fe#ale, or <fe#ale<"
>ttribute values $ust always be enclosed in ;uotes# but either single or double ;uotes can be used%
3or a personAs se0# the person tag can be written li'e this:
11
<-erson sex!female!>
or li'e this:
<-erson sex'female'>
4ote9 !f the attribute value itself contains double ;uotes it is necessary to use single ;uotes# li'e in
this e0a$ple:
<gangster name':eorge !'hotg/n! ;iegler'>
4ote9 !f the attribute value itself contains single ;uotes it is necessary to use double ;uotes# li'e in
this e0a$ple:
<gangster name!:eorge ''hotg/n' ;iegler!>
/se of 0le#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$% Fse child ele$ents if
the infor$ation feels li'e data%
My 7a&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:
12
<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>6/!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"
Should 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*+ ? 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% Fse attributes only to provide infor$ation that is
not relevant to the data%
*onAt 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!
13
body!Don't forget me this weekend!!>
</note>
n 0+ception to #y ttribute rule
)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 D>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 uni;ue 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 =alidation
XML with correct synta+ is Well 7or#ed XML.
XML &alidated against a $T$ is =alid XML.
,Well 7or#ed, XML docu#ents
,Well 7or#ed, XML docu#ent has correct XML synta+.
> 9,ell 3or$ed9 XML docu$ent is a docu$ent that confor$s to the XML synta0 rules that were
described in the previous chapters:
<?xml version!"#$! en%oding!&'()**+,)"!?>
14
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
,=alid, XML docu#ents
,=alid, XML docu#ent also confor#s to a $T$.
> 9alid9 XML docu$ent is a 9,ell 3or$ed9 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%
XML Sche#a
XML Sche#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%
0rrors will Stop you
0rrors in XML docu#ents will stop the XML progra#.
The ,-. XML specification states that a progra$ should not continue to process an XML docu$ent
if it finds a validation 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+%
6ne 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%
15
,ith XML this should not be possible%
general XML =alidator
To help you validate your 0$l files# we have created this lin' so that you can alidate any XML file%
XML $*M =alidation
=alidating your XML
To help you validate your 0$l# we have used MicrosoftAs XML parser to create an XML validator%
Haste your XML in the te0t area below# and validate it by pressing the validate button%
=alidating your XML file
(ou can also validate your XML files by typing the F/L of your file into the input field below# and
then press the sub$it button
3ilena$e:
http://www.w3schools.com/dom/note_error.xml
Validate
!f you want to validate an error?free XML file# you can paste the following address into the filena$e
field: http://www%w-schools%co$/do$/cdKcatalog%0$l
4ote9 !f you get the error 9>ccess denied9 when accessing this file# it is because your !nternet
&0plorer security settings do not allow access across do$ainsG
XML 1rowser Support
Most Internet browsers support XML. Howe&er- not all browsers ha&e full support for
XML- XML 4a#espaces- and XSLT.
16
Internet 0+plorer >
!nternet &0plorer I has full XML support# including Da$espaces# Style sheets in .SS# and XSLT 1%2%
The built?in XML Harser -%2 in !nternet &0plorer I%2 and ,indows XH is based on both the ,-. XSLT
1%2 and the ,-. XHath 1%2 /eco$$endations%
Internet 0+plorer ?
!nternet &0plorer B also has XML support# but the XSL part is 4*T co$patible with the official ,-.
XSL /eco$$endation%
7irefo+ @.A.2
The 3irefo0 browser has support for XML and XSLT )and XML P .SS+%
MoBilla @.C.D
Mo:illa includes &0pat for XML parsing and has support to display XML P .SS% Mo:illa also has so$e
support for Da$espaces%
Mo:illa is available with an XSLT i$ple$entation%
*pera D
6pera supports XML and XML P .SS%
4etscape >
Detscape supports XML%
Internet 0+plorer >.A
!nternet &0plorer I%2 has full XML support# including support for Da$espaces% Style sheets in .SS
and as well as XSLT 1%2 are supported%
!nternet &0plorer has the following XML support:
iewing of XML docu$ents
3ull support for ,-. *T* standards
XML e$bedded in HTML as *ata !slands
Binding XML data to HTML ele$ents
17
Transfor$ing and displaying XML with XSL
*isplaying XML with .SS
>ccess to the XML *6M
!nternet &0plorer also has support for Behaviors:
Behaviors is a Microsoft?only technology
Behaviors can separate scripts fro$ an HTML page%
Behaviors can store XML data on the clientAs dis'%
&0a$ples of all these features are given in this tutorial%
=iewing XML 7iles
)aw XML files can be &iewed in MoBilla- 7irefo+- *pera- Internet 0+plorer- and in 4etscape
>E.
Howe&er- to #ake XML docu#ents to display like nice web pages- you will ha&e to add
so#e display infor#ation.
=iewing XML 7iles
In 7irefo+ and Internet 0+plorer9
6pen the XML file )typically by clic'ing on a lin'+ ? The XML docu$ent will be displayed with color?
coded root and child ele$ents% > plus )P+ or $inus sign )?+ 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 P and
? signs+# select 9iew Hage Source9 or 9iew Source9 fro$ the browser $enu%
In 4etscape >9
6pen the XML file# then right?clic' in XML file and select 9iew Hage Source9% The XML docu$ent will
then be displayed with color?coded root and child ele$ents%
In *pera C and D9
!n 6pera J: 6pen the XML file# then right?clic' in XML file and select 93ra$e9 / 9iew Source9% The
XML docu$ent will be displayed as plain te0t% !n 6pera 5: 6pen the XML file# then right?clic' in XML
file and select 9Source9% The XML docu$ent will be displayed as plain te0t%
Loo' at this XML file: note%0$l
4ote9 $o not e+pect XML files to be for#atted like HTML docu#entsF
=iewing an In&alid XML 7ile
!f an erroneous XML file is opened# the browser will report the error%
18
Loo' at this XML file: noteKerror%0$l
*ther XML 0+a#ples
iewing so$e XML docu$ents will help you get the XML feeling%
>n XML .* catalog
This is $y fatherAs .* collection# stored as XML data )old and boring titles ! guess%%% :?++%
<?xml version="1.0" encoding="IS!8859!1" ?"
! <#!!
$di%ed &i%' ()* S+, v4.2
!!"
- <-./.*0"
- <-1"
</I/*$"Empire Burlesque<2/I/*$"
<.3/IS/"Bob Dylan<2.3/IS/"
<-45/36"USA<2-45/36"
<-)7.56"Columbia<2-)7.56"
<73I-$"10.90<273I-$"
<6$.3"1985<26$.3"
<2-1"
- <-1"
</I/*$"Hide your ear!<2/I/*$"
<.3/IS/"Bonnie "yler<2.3/IS/"
<-45/36"U#<2-45/36"
<-)7.56"CBS $e%ords<2-)7.56"
<73I-$"9.90<273I-$"
<6$.3"1988<26$.3"
<2-1"
- <-1"
</I/*$"&rea!es! Hi!s<2/I/*$"
<.3/IS/"Dolly 'ar!on<2.3/IS/"
<-45/36"USA<2-45/36"
<-)7.56"$CA<2-)7.56"
<73I-$"9.90<273I-$"
<6$.3"198(<26$.3"
<2-1"
- <-1"
</I/*$"S!ill )o! !e blues<2/I/*$"
<.3/IS/"&ary *oore<2.3/IS/"
<-45/36"U#<2-45/36"
<-)7.56"+ir)in re%ords<2-)7.56"
<73I-$"10.(0<273I-$"
<6$.3"1990<26$.3"
<2-1"
- <-1"
</I/*$"Eros<2/I/*$"
<.3/IS/"Eros $ama,,o!!i<2.3/IS/"
<-45/36"EU<2-45/36"
<-)7.56"B*&<2-)7.56"
<73I-$"9.90<273I-$"
<6$.3"199-<26$.3"
<2-1"
- <-1"
</I/*$".ne ni)! only<2/I/*$"
<.3/IS/"Bee &ees<2.3/IS/"
<-45/36"U#<2-45/36"
<-)7.56"'olydor<2-)7.56"
<73I-$"10.90<273I-$"
<6$.3"1998<26$.3"
<2-1"
- <-1"
</I/*$"Syl/ias *o!er<2/I/*$"
<.3/IS/"Dr.Hoo0<2.3/IS/"
19
<-45/36"U#<2-45/36"
<-)7.56"CBS<2-)7.56"
<73I-$"8.10<273I-$"
<6$.3"19-1<26$.3"
<2-1"
- <-1"
</I/*$"*a))ie *ay<2/I/*$"
<.3/IS/"$od S!e2ar!<2.3/IS/"
<-45/36"U#<2-45/36"
<-)7.56"'i%02i%0<2-)7.56"
<73I-$"8.50<273I-$"
<6$.3"1990<26$.3"
<2-1"
- <-1"
</I/*$"$oman,a<2/I/*$"
<.3/IS/"Andrea Bo%elli<2.3/IS/"
<-45/36"EU<2-45/36"
<-)7.56"'olydor<2-)7.56"
<73I-$"10.80<273I-$"
<6$.3"1993<26$.3"
<2-1"
- <-1"
</I/*$"4en a man lo/es a 2oman<2/I/*$"
<.3/IS/"'er%y Sled)e<2.3/IS/"
<-45/36"USA<2-45/36"
<-)7.56"A!lan!i%<2-)7.56"
<73I-$"8.-0<273I-$"
<6$.3"198-<26$.3"
<2-1"
- <-1"
</I/*$"Bla%0 an)el<2/I/*$"
<.3/IS/"Sa/a)e $ose<2.3/IS/"
<-45/36"EU<2-45/36"
<-)7.56"*e)a<2-)7.56"
<73I-$"10.90<273I-$"
<6$.3"1995<26$.3"
<2-1"
- <-1"
</I/*$"1999 &rammy 5ominees<2/I/*$"
<.3/IS/"*any<2.3/IS/"
<-45/36"USA<2-45/36"
<-)7.56"&rammy<2-)7.56"
<73I-$"10.(0<273I-$"
<6$.3"1999<26$.3"
<2-1"
- <-1"
</I/*$"6or !e )ood !imes<2/I/*$"
<.3/IS/"#enny $o)ers<2.3/IS/"
<-45/36"U#<2-45/36"
<-)7.56"*u%i0 *as!er<2-)7.56"
<73I-$"8.-0<273I-$"
<6$.3"1995<26$.3"
<2-1"
- <-1"
</I/*$"Bi) 4illie s!yle<2/I/*$"
<.3/IS/"4ill Smi!<2.3/IS/"
<-45/36"USA<2-45/36"
<-)7.56"Columbia<2-)7.56"
<73I-$"9.90<273I-$"
<6$.3"199-<26$.3"
<2-1"
- <-1"
</I/*$""upelo Honey<2/I/*$"
<.3/IS/"+an *orrison<2.3/IS/"
<-45/36"U#<2-45/36"
<-)7.56"'olydor<2-)7.56"
<73I-$"8.(0<273I-$"
<6$.3"19-1<26$.3"
<2-1"
- <-1"
20
</I/*$"Souls/ille<2/I/*$"
<.3/IS/"7orn Hoel<2.3/IS/"
<-45/36"5or2ay<2-45/36"
<-)7.56"4EA<2-)7.56"
<73I-$"-.90<273I-$"
<6$.3"1993<26$.3"
<2-1"
- <-1"
</I/*$""e /ery bes! o8<2/I/*$"
<.3/IS/"Ca! S!e/ens<2.3/IS/"
<-45/36"U#<2-45/36"
<-)7.56"9sland<2-)7.56"
<73I-$"8.90<273I-$"
<6$.3"1990<26$.3"
<2-1"
- <-1"
</I/*$"S!op<2/I/*$"
<.3/IS/"Sam Bro2n<2.3/IS/"
<-45/36"U#<2-45/36"
<-)7.56"A and *<2-)7.56"
<73I-$"8.90<273I-$"
<6$.3"1988<26$.3"
<2-1"
- <-1"
</I/*$"Brid)e o8 Spies<2/I/*$"
<.3/IS/"":'au<2.3/IS/"
<-45/36"U#<2-45/36"
<-)7.56"Siren<2-)7.56"
<73I-$"-.90<273I-$"
<6$.3"198-<26$.3"
<2-1"
- <-1"
</I/*$"'ri/a!e Dan%er<2/I/*$"
<.3/IS/""ina "urner<2.3/IS/"
<-45/36"U#<2-45/36"
<-)7.56"Capi!ol<2-)7.56"
<73I-$"8.90<273I-$"
<6$.3"1981<26$.3"
<2-1"
- <-1"
</I/*$"*id! om na!!en<2/I/*$"
<.3/IS/"#im ;arsen<2.3/IS/"
<-45/36"EU<2-45/36"
<-)7.56"*edley<2-)7.56"
<73I-$"-.80<273I-$"
<6$.3"1981<26$.3"
<2-1"
- <-1"
</I/*$"'a/aro!!i &ala Con%er!<2/I/*$"
<.3/IS/";u%iano 'a/aro!!i<2.3/IS/"
<-45/36"U#<2-45/36"
<-)7.56"DECCA<2-)7.56"
<73I-$"9.90<273I-$"
<6$.3"1991<26$.3"
<2-1"
- <-1"
</I/*$""e do%0 o8 !e bay<2/I/*$"
<.3/IS/".!is $eddin)<2.3/IS/"
<-45/36"USA<2-45/36"
<-)7.56"A!lan!i%<2-)7.56"
<73I-$"-.90<273I-$"
<6$.3"198-<26$.3"
<2-1"
- <-1"
</I/*$"'i%!ure boo0<2/I/*$"
<.3/IS/"Simply $ed<2.3/IS/"
<-45/36"EU<2-45/36"
<-)7.56"Ele0!ra<2-)7.56"
<73I-$"-.(0<273I-$"
<6$.3"1985<26$.3"
21
<2-1"
- <-1"
</I/*$"$ed<2/I/*$"
<.3/IS/""e Communards<2.3/IS/"
<-45/36"U#<2-45/36"
<-)7.56";ondon<2-)7.56"
<73I-$"-.80<273I-$"
<6$.3"198-<26$.3"
<2-1"
- <-1"
</I/*$"Un%ain my ear!<2/I/*$"
<.3/IS/"7oe Co%0er<2.3/IS/"
<-45/36"USA<2-45/36"
<-)7.56"E*9<2-)7.56"
<73I-$"8.(0<273I-$"
<6$.3"198-<26$.3"
<2-1"
<2-./.*0"
.n ()* +l8n% c8%8log
/'is is 8 +l8n% c8%8log 9rom 8 +l8n% s'o+: s%ored 8s ()* d8%8.
<?xml version="1.0" encoding="IS!8859!1" ?"
! <#!!
$di%ed &i%' ()* S+, v4.2
!!"
- <-./.*0"
- <7*.5/"
<-))5"Bloodroo!<2-))5"
<;/.5I-.*"San)uinaria %anadensis<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"*os!ly Sady<2*I0=/"
<73I-$"=(.<<<273I-$"
<.>.I*.;I*I/6"011599<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Columbine<2-))5"
<;/.5I-.*"Aquile)ia %anadensis<2;/.5I-.*"
<<5$"1<2<5$"
<*I0=/"*os!ly Sady<2*I0=/"
<73I-$"=9.1-<273I-$"
<.>.I*.;I*I/6"010399<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"*ars *ari)old<2-))5"
<;/.5I-.*"Cal!a palus!ris<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"*os!ly Sunny<2*I0=/"
<73I-$"=3.81<273I-$"
<.>.I*.;I*I/6"051-99<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Co2slip<2-))5"
<;/.5I-.*"Cal!a palus!ris<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"*os!ly Sady<2*I0=/"
<73I-$"=9.90<273I-$"
<.>.I*.;I*I/6"010399<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Du!%man:s-Bree%es<2-))5"
<;/.5I-.*"Di%en!ra %u%ullaria<2;/.5I-.*"
<<5$"1<2<5$"
<*I0=/"*os!ly Sady<2*I0=/"
22
<73I-$"=3.<<<273I-$"
<.>.I*.;I*I/6"01(099<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"&in)er> 4ild<2-))5"
<;/.5I-.*"Asarum %anadense<2;/.5I-.*"
<<5$"1<2<5$"
<*I0=/"*os!ly Sady<2*I0=/"
<73I-$"=9.01<273I-$"
<.>.I*.;I*I/6"0<1899<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Hepa!i%a<2-))5"
<;/.5I-.*"Hepa!i%a ameri%ana<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"*os!ly Sady<2*I0=/"
<73I-$"=<.<5<273I-$"
<.>.I*.;I*I/6"01(399<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5";i/erlea8<2-))5"
<;/.5I-.*"Hepa!i%a ameri%ana<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"*os!ly Sady<2*I0=/"
<73I-$"=1.99<273I-$"
<.>.I*.;I*I/6"010(99<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"7a%0-9n-"e-'ulpi!<2-))5"
<;/.5I-.*"Arisaema !ripyllum<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"*os!ly Sady<2*I0=/"
<73I-$"=1.(1<273I-$"
<.>.I*.;I*I/6"0(0199<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"*ayapple<2-))5"
<;/.5I-.*"'odopyllum pel!a!um<2;/.5I-.*"
<<5$"1<2<5$"
<*I0=/"*os!ly Sady<2*I0=/"
<73I-$"=(.98<273I-$"
<.>.I*.;I*I/6"030599<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"'lo?> 4oodland<2-))5"
<;/.5I-.*"'lo? di/ari%a!a<2;/.5I-.*"
<<5$"1<2<5$"
<*I0=/"Sun or Sade<2*I0=/"
<73I-$"=(.80<273I-$"
<.>.I*.;I*I/6"01((99<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"'lo?> Blue<2-))5"
<;/.5I-.*"'lo? di/ari%a!a<2;/.5I-.*"
<<5$"1<2<5$"
<*I0=/"Sun or Sade<2*I0=/"
<73I-$"=5.59<273I-$"
<.>.I*.;I*I/6"0(1399<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Sprin)-Beau!y<2-))5"
<;/.5I-.*"Clay!onia +ir)ini%a<2;/.5I-.*"
<<5$"-<2<5$"
<*I0=/"*os!ly Sady<2*I0=/"
<73I-$"=3.59<273I-$"
<.>.I*.;I*I/6"0(0199<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5""rillium<2-))5"
<;/.5I-.*""rillium )randi8lorum<2;/.5I-.*"
23
<<5$"5<2<5$"
<*I0=/"Sun or Sade<2*I0=/"
<73I-$"=1.90<273I-$"
<.>.I*.;I*I/6"0<(999<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"4a0e $obin<2-))5"
<;/.5I-.*""rillium )randi8lorum<2;/.5I-.*"
<<5$"5<2<5$"
<*I0=/"Sun or Sade<2*I0=/"
<73I-$"=1.(0<273I-$"
<.>.I*.;I*I/6"0((199<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"+iole!> Do)-"oo!<2-))5"
<;/.5I-.*"Ery!ronium ameri%anum<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"Sade<2*I0=/"
<73I-$"=9.0<<273I-$"
<.>.I*.;I*I/6"0(0199<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5""rou! ;ily<2-))5"
<;/.5I-.*"Ery!ronium ameri%anum<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"Sade<2*I0=/"
<73I-$"=3.9<<273I-$"
<.>.I*.;I*I/6"01(<99<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Adder:s-"on)ue<2-))5"
<;/.5I-.*"Ery!ronium ameri%anum<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"Sade<2*I0=/"
<73I-$"=9.58<273I-$"
<.>.I*.;I*I/6"0<1199<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Anemone<2-))5"
<;/.5I-.*"Anemone blanda<2;/.5I-.*"
<<5$"3<2<5$"
<*I0=/"*os!ly Sady<2*I0=/"
<73I-$"=8.83<273I-$"
<.>.I*.;I*I/6"1((398<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"&re%ian 4ind8lo2er<2-))5"
<;/.5I-.*"Anemone blanda<2;/.5I-.*"
<<5$"3<2<5$"
<*I0=/"*os!ly Sady<2*I0=/"
<73I-$"=9.13<273I-$"
<.>.I*.;I*I/6"0-1099<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Bee Balm<2-))5"
<;/.5I-.*"*onarda didyma<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"Sade<2*I0=/"
<73I-$"=<.59<273I-$"
<.>.I*.;I*I/6"050199<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Ber)amo!<2-))5"
<;/.5I-.*"*onarda didyma<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"Sade<2*I0=/"
<73I-$"=-.13<273I-$"
<.>.I*.;I*I/6"0<(-99<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
24
<-))5"Bla%0-Eyed Susan<2-))5"
<;/.5I-.*"$udbe%0ia ir!a<2;/.5I-.*"
<<5$"Annual<2<5$"
<*I0=/"Sunny<2*I0=/"
<73I-$"=9.80<273I-$"
<.>.I*.;I*I/6"031899<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Bu!!er%up<2-))5"
<;/.5I-.*"$anun%ulus<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"Sade<2*I0=/"
<73I-$"=(.5-<273I-$"
<.>.I*.;I*I/6"031099<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Cro28oo!<2-))5"
<;/.5I-.*"$anun%ulus<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"Sade<2*I0=/"
<73I-$"=9.1<<273I-$"
<.>.I*.;I*I/6"0<0199<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Bu!!er8ly 4eed<2-))5"
<;/.5I-.*"As%lepias !uberosa<2;/.5I-.*"
<<5$"Annual<2<5$"
<*I0=/"Sunny<2*I0=/"
<73I-$"=(.-8<273I-$"
<.>.I*.;I*I/6"031099<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Cinque8oil<2-))5"
<;/.5I-.*"'o!en!illa<2;/.5I-.*"
<<5$"Annual<2<5$"
<*I0=/"Sade<2*I0=/"
<73I-$"=-.03<273I-$"
<.>.I*.;I*I/6"05(599<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"'rimrose<2-))5"
<;/.5I-.*".eno!era<2;/.5I-.*"
<<5$"1 - 5<2<5$"
<*I0=/"Sunny<2*I0=/"
<73I-$"=3.53<273I-$"
<.>.I*.;I*I/6"011099<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"&en!ian<2-))5"
<;/.5I-.*"&en!iana<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"Sun or Sade<2*I0=/"
<73I-$"=-.81<273I-$"
<.>.I*.;I*I/6"051899<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Blue &en!ian<2-))5"
<;/.5I-.*"&en!iana<2;/.5I-.*"
<<5$"<<2<5$"
<*I0=/"Sun or Sade<2*I0=/"
<73I-$"=8.53<273I-$"
<.>.I*.;I*I/6"050(99<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"7a%ob:s ;adder<2-))5"
<;/.5I-.*"'olemonium %aeruleum<2;/.5I-.*"
<<5$"Annual<2<5$"
<*I0=/"Sade<2*I0=/"
<73I-$"=9.(3<273I-$"
<.>.I*.;I*I/6"0((199<2.>.I*.;I*I/6"
25
<27*.5/"
- <7*.5/"
<-))5"&ree0 +alerian<2-))5"
<;/.5I-.*"'olemonium %aeruleum<2;/.5I-.*"
<<5$"Annual<2<5$"
<*I0=/"Sade<2*I0=/"
<73I-$"=<.13<273I-$"
<.>.I*.;I*I/6"0-1<99<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Cali8ornia 'oppy<2-))5"
<;/.5I-.*"Es%s%ol,ia %ali8orni%a<2;/.5I-.*"
<<5$"Annual<2<5$"
<*I0=/"Sun<2*I0=/"
<73I-$"=-.89<273I-$"
<.>.I*.;I*I/6"01(-99<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Soo!in) S!ar<2-))5"
<;/.5I-.*"Dode%a!eon<2;/.5I-.*"
<<5$"Annual<2<5$"
<*I0=/"*os!ly Sady<2*I0=/"
<73I-$"=8.30<273I-$"
<.>.I*.;I*I/6"051199<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Sna0eroo!<2-))5"
<;/.5I-.*"Cimi%i8u)a<2;/.5I-.*"
<<5$"Annual<2<5$"
<*I0=/"Sade<2*I0=/"
<73I-$"=5.31<273I-$"
<.>.I*.;I*I/6"0-1199<2.>.I*.;I*I/6"
<27*.5/"
- <7*.5/"
<-))5"Cardinal 6lo2er<2-))5"
<;/.5I-.*";obelia %ardinalis<2;/.5I-.*"
<<5$"(<2<5$"
<*I0=/"Sade<2*I0=/"
<73I-$"=1.0(<273I-$"
<.>.I*.;I*I/6"0(((99<2.>.I*.;I*I/6"
<27*.5/"
<2-./.*0"
> Si$ple 3ood Menu
This is a brea'fast food $enu fro$ a restaurant# stored as XML data%
7=0$l versionE91%29 encodingE9!S6?55B4?19 =8
? 7G??
9dited with 2.3 '-y vB#0
??8
- 7brea'fastK$enu8
- 7food8
7na$e81elgian Waffles7/na$e8
7price8G?.H?7/price8
7description8two of our fa#ous 1elgian Waffles with plenty of real #aple syrup7/description8
7calories8>?A7/calories8
7/food8
+ 7food8
- 7food8
7na$e81erry%1erry 1elgian Waffles7/na$e8
7price8GD.H?7/price8
26
7description8light 1elgian waffles co&ered with an assort#ent of fresh berries and whipped
crea#7/description8
7calories8HAA7/calories8
7/food8
- 7food8
7na$e87rench Toast7/na$e8
7price8GI.?A7/price8
7description8thick slices #ade fro# our ho#e#ade sourdough bread7/description8
7calories8>AA7/calories8
7/food8
- 7food8
7na$e8Ho#estyle 1reakfast7/na$e8
7price8G>.H?7/price8
7description8two eggs- bacon or sausage- toast- and our e&er%popular hash browns7/description8
7calories8H?A7/calories8
7/food8
7/brea'fastK$enu8
Why does XML display like this"
XML docu$ents do not carry infor$ation about how to display the data%
Since XML tags are 9invented9 by the author of the XML docu$ent# browsers do not 'now if a tag
li'e 7table8 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%
$isplaying XML with (SS
With (SS J(ascading Style SheetsK you can add display infor#ation to an XML docu#ent.
$isplaying your XML files with (SS"
!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
Then loo' at this style sheet: The .SS file
27
><T<3(:
C
ba%kgro/nd)%olor= DffffffE
width= "$$FE
G
>D
C
dis-lay= blo%kE
margin)bottom= 4$-tE
margin)left= $E
G
T&T39
C
%olor= D11$$$$E
font)siHe= 0$-tE
G
<RT&'T
C
%olor= D$$$$11E
font)siHe= 0$-tE
G
>(IATR?J@R&>9J?9<RJ>(.@<A?
C
dis-lay= blo%kE
%olor= D$$$$$$E
margin)left= 0$-tE
G
3inally# view: The .* catalog for$atted with the .SS file
Bob *ylan FS> .olu$bia 12%42 145B Bonnie Tyler FQ .BS /ecords
4%42 1455 *olly Harton FS> /.> 4%42 145@ Oary Moore FQ irgin
records 12%@2 1442 &ros /a$a::otti &F BMO 4%42 144J Bee Oees FQ
Holydor 12%42 1445 *r%Hoo' FQ .BS 5%12 14J- /od Stewart FQ
Hic'wic' 5%B2 1442 >ndrea Bocelli &F Holydor 12%52 144I Hercy Sledge
FS> >tlantic 5%J2 145J Savage /ose &F Mega 12%42 144B Many FS>
Ora$$y 12%@2 1444 Qenny /ogers FQ Muci' Master 5%J2 144B ,ill
S$ith FS> .olu$bia 4%42 144J an Morrison FQ Holydor 5%@2 14J1
Jorn Hoel Dorway ,&> J%42 144I .at Stevens FQ !sland 5%42 1442
Sa$ Brown FQ > and M 5%42 1455 TRHau FQ Siren J%42 145J Tina
Turner FQ .apitol 5%42 145- Qi$ Larsen &F Medley J%52 145- Luciano
Havarotti FQ *&..> 4%42 1441 6tis /edding FS> >tlantic J%42 145J
Si$ply /ed &F &le'tra J%@2 145B The .o$$unards FQ London J%52
145J Joe .oc'er FS> &M! 5%@2 145J
Below is a fraction of the XML file% The second line# L"+#l%stylesheet typeM,te+t6css,
hrefM,cdNcatalog.css,"O# lin's the XML file to the .SS file:
<?xml version!"#$! en%oding!&'()**+,)"!?>
28
<?xml)stylesheet ty-e!text/%ss! href!%dK%atalog#%ss!?>
<><T<3(:>
<>D>
<T&T39>9m-ire L/rlesM/e</T&T39>
<<RT&'T>Lob Dylan</<RT&'T>
<>(IATR?>I'<</>(IATR?>
<>(.@<A?>>ol/mbia</>(.@<A?>
<@R&>9>"$#,$</@R&>9>
<?9<R>",*+</?9<R>
</>D>
<>D>
<T&T39>8ide yo/r heart</T&T39>
<<RT&'T>Lonnie Tyler</<RT&'T>
<>(IATR?>IN</>(IATR?>
<>(.@<A?>>L' Re%ords</>(.@<A?>
<@R&>9>,#,$</@R&>9>
<?9<R>",**</?9<R>
</>D>
#
#
#
#
</><T<3(:>
4ote9 3or$atting XML with .SS is D6T the future of how to style XML docu$ents% XML docu$ent
should be styled by using the ,-.As XSL standardG
$isplaying XML with XSL
With XSL you can add display infor#ation to your XML docu#ent.
$isplaying XML with XSL
XSL is the preferred style sheet language of XML.
XSL )the eXtensible Stylesheet Language+ is far $ore sophisticated than .SS% 6ne 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#
7=0$l versionE91%29 encodingE9!S6?55B4?19 =8
? 7G??
9dited with 2.3 '-y vB#0
??8
- 7brea'fastK$enu8
- 7food8
7na$e81elgian Waffles7/na$e8
7price8G?.H?7/price8
29
7description8two of our fa#ous 1elgian Waffles with plenty of real #aple
syrup7/description8
7calories8>?A7/calories8
7/food8
- 7food8
7na$e8Strawberry 1elgian Waffles7/na$e8
7price8GC.H?7/price8
7description8light 1elgian waffles co&ered with strawberries and whipped
crea#7/description8
7calories8HAA7/calories8
7/food8
- 7food8
7na$e81erry%1erry 1elgian Waffles7/na$e8
7price8GD.H?7/price8
7description8light 1elgian waffles co&ered with an assort#ent of fresh berries
and whipped crea#7/description8
7calories8HAA7/calories8
7/food8
- 7food8
7na$e87rench Toast7/na$e8
7price8GI.?A7/price8
7description8thick slices #ade fro# our ho#e#ade sourdough
bread7/description8
7calories8>AA7/calories8
7/food8
- 7food8
7na$e8Ho#estyle 1reakfast7/na$e8
7price8G>.H?7/price8
7description8two eggs- bacon or sausage- toast- and our e&er%popular hash
browns7/description8
7calories8H?A7/calories8
7/food8
7/brea'fastK$enu8
7/ht$l8
%'e (S* s%,le s'ee%:
7=0$l versionE91%29 encodingE9!S6?55B4?19 =8
? 7G??
9dited with 2.3 '-y vB#0
??8
- 7ht$l 0sl:versionE9@.A9 0$lns:0slE9http966www.w'.org6@HHH6XSL6Transfor#9
0$lnsE9http966www.w'.org6@HHH6+ht#l98
- 7body styleE9font%fa#ily9rial-hel&etica-sans%serifPfont%siBe9@2ptP background%
color9Q00000098
- 70sl:for?each selectE9breakfastN#enu6food98
- 7div styleE9background%color9tealPcolor9whitePpadding9Ip+98
- 7span styleE9font%weight9boldPcolor9white98
70sl:value?of selectE9na#e9 /8
30
7/span8
%
70sl:value?of selectE9price9 /8
7/div8
- 7div styleE9#argin%left92Ap+P#argin%botto#9@e#Pfont%siBe9@Apt98
70sl:value?of selectE9description9 /8
- 7span styleE9font%style9italic98
J
70sl:value?of selectE9calories9 /8
calories per ser&ingK
7/span8
7/div8
7/0sl:for?each8
7/body8
7/ht$l8
and iew the result%
Belgian Waffles - $5.95
two of our famous Belgian affles with plent! of real maple s!rup (650 calories per serving)
Strawberry Belgian Waffles - $".95
light Belgian waffles co#ered with straw$erries and whipped cream (900 calories per serving)
Berry-Berry Belgian Waffles - $%.95
light Belgian waffles co#ered with an assortment of fresh $erries and whipped cream (900
calories per serving)
French Toast - $&.5'
thic( slices made from our homemade sourdough $read (600 calories per serving)
Homestyle Breakfast - $).95
two eggs* $acon or sausage* toast* and our e#er-popular hash $rowns (950 calories per serving)
Below is a fraction of the XML file% The second line# L"+#l%stylesheet typeM,te+t6+sl,
hrefM,si#ple.+sl,"O# lin's the XML file to the XSL file:
<?xml version!"#$! en%oding!&'()**+,)"!?>
<?xml)stylesheet ty-e!text/xsl! href!sim-le#xsl!?>
<breakfastKmen/>
<food>
<name>Lelgian 7affles</name>
<-ri%e>O+#,+</-ri%e>
<des%ri-tion>
two of o/r famo/s Lelgian 7affles
</des%ri-tion>
31
<%alories>5+$</%alories>
</food>
</breakfastKmen/>
!f you want to learn $ore about XSL# please visit our XSL tutorial%
XML in $ata Islands
With Internet 0+plorer- XML can be e#bedded into HTML pages in $ata Islands.
XML 0#bedded in HTML
The unofficial L+#lO tag is used to e#bed XML data within HTML.
XML data can be e$bedded directly into an HTML page li'e this:
<xml id!note!>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
</xml>
>nd an e0ternal XML file can be e$bedded li'e this:
<xml id!note! sr%!note#xml!>
</xml>
4ote9 The 70$l8 tag is an HTML ele$ent# not an XML ele$ent%
$ata 1inding
XML $ata Islands can be bound to HTML ele#ents.
!n the e0a$ple below# an XML *ata !sland with an !* of 9cdcat9 is loaded fro$ an e0ternal XML file
)9cdKcatalog%0$l9+% De0t# the HTML table ele$ent is bound to the *ata !sland with the datasrc
attribute# and finally the td ele$ents are bound to the XML data with a datafld attribute inside a
span ele$ent%
<html>
<body>
<xml id!%d%at! sr%!%dK%atalog#xml!></xml>
32
<table border!"! datasr%!D%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>
!f you are running !& B%2 or higher# you can try it yourself% (ou can also view the e0ternal XML file%
7T!TL&8Maggie May7/T!TL&8
7>/T!ST8)od Stewart7/>/T!ST8
7.6FDT/(8/!7/.6FDT/(8
7.6MH>D(83ickwick7/.6MH>D(8
7H/!.&8D.?A7/H/!.&8
7(&>/8@HHA7/(&>/8
7/.*8
- 7.*8
7T!TL&8)o#anBa7/T!TL&8
7>/T!ST8ndrea 1ocelli7/>/T!ST8
7.6FDT/(80/7/.6FDT/(8
7.6MH>D(83olydor7/.6MH>D(8
7H/!.&8@A.DA7/H/!.&8
7(&>/8@HH>7/(&>/8
7/.*8
- 7.*8
7T!TL&8When a #an lo&es a wo#an7/T!TL&8
7>/T!ST83ercy Sledge7/>/T!ST8
7.6FDT/(8/S7/.6FDT/(8
7.6MH>D(8tlantic7/.6MH>D(8
7H/!.&8D.CA7/H/!.&8
7(&>/8@HDC7/(&>/8
7/.*8
- 7.*8
7T!TL&81lack angel7/T!TL&8
7>/T!ST8Sa&age )ose7/>/T!ST8
7.6FDT/(80/7/.6FDT/(8
7.6MH>D(8Mega7/.6MH>D(8
7H/!.&8@A.HA7/H/!.&8
7(&>/8@HH?7/(&>/8
7/.*8
- 7.*8
7T!TL&8@HHH 8ra##y 4o#inees7/T!TL&8
7>/T!ST8Many7/>/T!ST8
7.6FDT/(8/S7/.6FDT/(8
7.6MH>D(88ra##y7/.6MH>D(8
7H/!.&8@A.2A7/H/!.&8
7(&>/8@HHH7/(&>/8
33
7/.*8
- 7.*8
7T!TL&87or the good ti#es7/T!TL&8
7>/T!ST8!enny )ogers7/>/T!ST8
7.6FDT/(8/!7/.6FDT/(8
7.6MH>D(8Mucik Master7/.6MH>D(8
7H/!.&8D.CA7/H/!.&8
7(&>/8@HH?7/(&>/8
7/.*8
- 7.*8
7T!TL&81ig Willie style7/T!TL&8
7>/T!ST8Will S#ith7/>/T!ST8
7.6FDT/(8/S7/.6FDT/(8
7.6MH>D(8(olu#bia7/.6MH>D(8
7H/!.&8H.HA7/H/!.&8
7(&>/8@HHC7/(&>/8
7/.*8
- 7.*8
7T!TL&8Tupelo Honey7/T!TL&8
7>/T!ST8=an Morrison7/>/T!ST8
7.6FDT/(8/!7/.6FDT/(8
7.6MH>D(83olydor7/.6MH>D(8
7H/!.&8D.2A7/H/!.&8
7(&>/8@HC@7/(&>/8
7/.*8
- 7.*8
7T!TL&8Souls&ille7/T!TL&8
7>/T!ST8.orn Hoel7/>/T!ST8
7.6FDT/(84orway7/.6FDT/(8
7.6MH>D(8W07/.6MH>D(8
7H/!.&8C.HA7/H/!.&8
7(&>/8@HH>7/(&>/8
7/.*8
- 7.*8
7T!TL&8The &ery best of7/T!TL&8
7>/T!ST8(at Ste&ens7/>/T!ST8
7.6FDT/(8/!7/.6FDT/(8
7.6MH>D(8Island7/.6MH>D(8
7H/!.&8D.HA7/H/!.&8
7(&>/8@HHA7/(&>/8
7/.*8
- 7.*8
7T!TL&8Stop7/T!TL&8
7>/T!ST8Sa# 1rown7/>/T!ST8
7.6FDT/(8/!7/.6FDT/(8
7.6MH>D(8 and M7/.6MH>D(8
7H/!.&8D.HA7/H/!.&8
7(&>/8@HDD7/(&>/8
7/.*8
34
- 7.*8
7T!TL&81ridge of Spies7/T!TL&8
7>/T!ST8T<3au7/>/T!ST8
7.6FDT/(8/!7/.6FDT/(8
7.6MH>D(8Siren7/.6MH>D(8
7H/!.&8C.HA7/H/!.&8
7(&>/8@HDC7/(&>/8
7/.*8
- 7.*8
7T!TL&83ri&ate $ancer7/T!TL&8
7>/T!ST8Tina Turner7/>/T!ST8
7.6FDT/(8/!7/.6FDT/(8
7.6MH>D(8(apitol7/.6MH>D(8
7H/!.&8D.HA7/H/!.&8
7(&>/8@HD'7/(&>/8
7/.*8
- 7.*8
7T!TL&8Midt o# natten7/T!TL&8
7>/T!ST8!i# Larsen7/>/T!ST8
7.6FDT/(80/7/.6FDT/(8
7.6MH>D(8Medley7/.6MH>D(8
7H/!.&8C.DA7/H/!.&8
7(&>/8@HD'7/(&>/8
7/.*8
- 7.*8
7T!TL&83a&arotti 8ala (oncert7/T!TL&8
7>/T!ST8Luciano 3a&arotti7/>/T!ST8
7.6FDT/(8/!7/.6FDT/(8
7.6MH>D(8$0((7/.6MH>D(8
7H/!.&8H.HA7/H/!.&8
7(&>/8@HH@7/(&>/8
7/.*8
- 7.*8
7T!TL&8The dock of the bay7/T!TL&8
7>/T!ST8*tis )edding7/>/T!ST8
7.6FDT/(8/S7/.6FDT/(8
7.6MH>D(8tlantic7/.6MH>D(8
7H/!.&8C.HA7/H/!.&8
7(&>/8@HDC7/(&>/8
7/.*8
- 7.*8
7T!TL&83icture book7/T!TL&8
7>/T!ST8Si#ply )ed7/>/T!ST8
7.6FDT/(80/7/.6FDT/(8
7.6MH>D(80lektra7/.6MH>D(8
7H/!.&8C.2A7/H/!.&8
7(&>/8@HD?7/(&>/8
7/.*8
- 7.*8
35
7T!TL&8)ed7/T!TL&8
7>/T!ST8The (o##unards7/>/T!ST8
7.6FDT/(8/!7/.6FDT/(8
7.6MH>D(8London7/.6MH>D(8
7H/!.&8C.DA7/H/!.&8
7(&>/8@HDC7/(&>/8
7/.*8
- 7.*8
7T!TL&8/nchain #y heart7/T!TL&8
7>/T!ST8.oe (ocker7/>/T!ST8
7.6FDT/(8/S7/.6FDT/(8
7.6MH>D(80MI7/.6MH>D(8
7H/!.&8D.2A7/H/!.&8
7(&>/8@HDC7/(&>/8
7/.*8
7/.>T>L6O8
>lso try this e0a$ple# de$onstrating 7thead8# 7tbody8# and 7tfoot8%
XML 3arser
To read and #anipulate XML docu#ents- you need an XML parser.
The Microsoft XML parser
MSXML Harser @%B is the XML parser that is shipped with ,indows @222 and !& B%B%
MSXML Harser -%2 is the XML parser that is shipped with !& I%2 and ,indows XH%
6nce you have installed !nternet &0plorer# the parser is available to scripts# both inside HTML
docu$ents and inside >SH files% The MSXML -%2 parser features a language?neutral progra$$ing
$odel that supports:
JavaScript# BScript# Herl# B# Java# .PP and $ore
.o$plete XML support
3ull *6M and Da$espace support
*T* and validation
.o$plete XSLT and XHath support
S>X@
Server?safe HTTH
To create an XML *ocu$ent 6b"ect with JavaScript# use the following code:
var xmlDo%new <%tive2(bPe%tQ!.i%rosoft#2.3D(.!R
36
To create an XML *ocu$ent 6b"ect with BScript# use the following code:
set xmlDo%>reate(bPe%tQ!.i%rosoft#2.3D(.!R
To create an XML *ocu$ent 6b"ect in >SH )with BScript+# use the following code:
set xmlDo%'erver#>reate(bPe%tQ!.i%rosoft#2.3D(.!R
The MoBilla XML 3arser
Hlain XML docu$ents are displayed in a tree?li'e structure in Mo:illa )"ust li'e !&+%
Mo:illa also supports parsing of XML data that resides in a file# using JavaScript% The parsed data
can be displayed as HTML%
To create an XML *ocu$ent ob"ect with JavaScript in Mo:illa# use the following code:
var xmlDo%do%/ment#im-lementation#%reateDo%/mentQ!ns!J!root!Jn/llR
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%
Loading an XML 7ile Into the 3arser
The following code loads an e0isting XML docu$ent )9note%0$l9+ into the XML parser:
<s%ri-t ty-e!text/Pavas%ri-t!>
var xmlDo%new <%tive2(bPe%tQ!.i%rosoft#2.3D(.!R
xmlDo%#asyn%!false!
xmlDo%#loadQ!note#xml!R
###
###
###
</s%ri-t>
The first line of the script creates an instance of the Microsoft XML parser% The third line tells the
parser to load an XML docu$ent called 9note%0$l9% The second line turns off asynchroni:ed loading#
to $a'e sure that the parser will not continue e0ecution of the script before the docu$ent is fully
loaded%
Loading XML Te+t Into the 3arser
The following code loads a te0t string into the XML parser:
<s%ri-t ty-e!text/Pavas%ri-t!>
var txt!<note>!
37
txttxtS!<to>Tove</to><from>Jani</from>!
txttxtS!<heading>Reminder</heading>!
txttxtS!<body>Don't forget me this weekend!</body>!
txttxtS!</note>!
var xmlDo%new <%tive2(bPe%tQ!.i%rosoft#2.3D(.!R
xmlDo%#asyn%!false!
xmlDo%#load2.3QtxtR
###
###
###
</s%ri-t>
Dote that we have used the 9loadXML9 $ethod )instead of the 9load9 $ethod+ to load a te0t string%
$isplaying XML with .a&aScript
To see how XML and HTML co$ple$ent each other this way# first loo' at the XML docu$ent
)note%0$l+# then open the HTML docu$ent )note%ht$+ that contains a JavaScript which reads the
XML file and displays the infor$ation inside the HTML page%
To see how it wor's# try it yourself%
There are $ore e0a$ples of this in our XML *6M tutorial%
XML in )eal Life
So#e real%life e+a#ples of how XML can be used to carry infor#ation.
0+a#ple9 XML 4ews
XML4ews is a specification for e+changing news and other infor#ation.
Fsing 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 e+a#ple XML 4ews docu#ent9
<?xml version!"#$! en%oding!&'()**+,)"!?>
<nitf>
<head>
<title>>olombia 9arthM/ake</title>
</head>
<body>
38
<headline>
<hl">"B4 Dead in >olombia 9arthM/ake</hl">
</headline>
<byline>
<bytag>Ly Jared NotlerJ <sso%iated @ress 7riter</bytag>
</byline>
<dateline>
<lo%ation>LogotaJ >olombia</lo%ation>
<date>.onday Jan/ary 0+ ",,, 6=0* 9T</date>
</dateline>
</body>
</nitf>
XML 4a#espaces
XML 4a#espaces pro&ide a #ethod to a&oid ele#ent na#e conflicts.
4a#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>Lananas</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 7table8 ele$ent with different content and definition%
Sol&ing 4a#e (onflicts using a 3refi+
This XML docu$ent carries infor$ation in a table:
39
<h=table>
<h=tr>
<h=td><--les</h=td>
<h=td>Lananas</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>
Dow there will be no na$e conflict because the two docu$ents use a different na$e for their
7table8 ele$ent )7h:table8 and 7f:table8+%
By using a prefi0# we have created two different types of 7table8 ele$ents%
/sing 4a#espaces
This XML docu$ent carries infor$ation in a table:
<h=table xmlns=h!htt-=//www#w4#org/TR/htmlB/!>
<h=tr>
<h=td><--les</h=td>
<h=td>Lananas</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 7table8 tag to give the
prefi0 a ;ualified na$e associated with a na$espace%
The XML 4a#espace J+#lnsK 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!
40
,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%
Dote 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 uni;ue 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$lC/%
/nifor# )esource Identifier J/)IK
> /nifor# )esource Identifier )F/!+ is a string of characters which identifies an !nternet
/esource% The $ost co$$on F/! is the /nifor# )esource Locator )F/L+ which identifies an
!nternet do$ain address% >nother# not so co$$on type of F/! is the /ni&ersal )esource 4a#e
)F/D+% !n our e0a$ples we will only use F/Ls%
$efault 4a#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/htmlB/!>
<tr>
<td><--les</td>
<td>Lananas</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>
4a#espaces in )eal /se
,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
9http://www%w-%org/1444/XSL/Transfor$9:
41
<?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.
*nly te+t inside a ($T section will be ignored by the parser.
3arsed $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 7na$e8 ele$ent contains two other ele$ents )first and last+:
<name><first>Lill</first><last>:ates</last></name>
and the parser will brea' it up into sub?ele$ents li'e this:
<name>
<first>Lill</first>
<last>:ates</last>
</name>
42
0scape (haracters
Illegal XML characters ha&e to be replaced by entity references.
!f you place a character li'e 979 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 979 character with an entity reference# li'e this:
<message>if salary TltE "$$$ then</message>
There are B predefined entity references in XML:
SltT 7 less than
SgtT 8 greater than
Sa$pT S a$persand
SaposT A apostrophe
S;uotT 9 ;uotation $ar'
4ote9 6nly the characters 979 and 9S9 are strictly illegal in XML% >postrophes# ;uotation $ar's and
greater than signs are legal# but it is a good habit to replace the$%
($T
0&erything inside a ($T section is ignored by the parser.
!f your te0t contains a lot of 979 or 9S9 characters ? as progra$ code often does ? the XML ele$ent
can be defined as a .*>T> section%
> .*>T> section starts with 9LFR($TR9 and ends with 9SSO9:
<s%ri-t>
<!U>D<T<U
f/n%tion mat%hwoQaJbR
C
if Qa < b TT a < $R then
C
ret/rn "
G
else
C
ret/rn $
G
G
VV>
</s%ri-t>
43
!n the e0a$ple above# everything inside the .*>T> section is ignored by the parser%
Notes on CDATA sections:
> .*>T> section cannot contain the string 9UU89# therefore# nested .*>T> sections are not allowed%
>lso $a'e sure there are no spaces or line brea's inside the 9UU89 string
XML 0ncoding
XML docu#ents #ay contain foreign characters- like 4orwegian T U V - or 7rench W X Y.
To let your XML parser understand these characters- you should sa&e your XML
docu#ents as /nicode.
Windows 2AAA 4otepad
,indows @222 Dotepad can save files as Fnicode%
Save the XML file below as Fnicode )note that the docu$ent does not contain any encoding
attribute+:
<?xml version!"#$!?>
<note>
<from>Jani</from>
<to>Tove</to>
<message>Aorwegian= WXY# 1ren%h= Z[\</message>
</note>
The file above# n oteKencodeKnoneKu%0$l will D6T generate an error in !& BP# 3irefo0# or 6pera# but
it ,!LL generate an error in Detscape I%@%
Windows 2AAA 4otepad with 0ncoding
,indows @222 Dotepad files saved as Fnicode use 9FT3?1I9 encoding%
!f you add an encoding attribute to XML files saved as Fnicode# windows encoding values will
generate an error%
The following encoding )open it+# will D6T give an error $essage:
<?xml version!"#$! en%oding!windows)"0+0!?>
The following encoding )open it+# will D6T give an error $essage:
<?xml version!"#$! en%oding!&'()**+,)"!?>
44
The following encoding )open it+# will D6T give an error $essage:
<?xml version!"#$! en%oding!IT1)*!?>
The following encoding )open it+# will D6T generate an error in !& BP# 3irefo0# or 6pera# but it ,!LL
generate an error in Detscape I%@%
<?xml version!"#$! en%oding!IT1)"5!?>
0rror 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% Dor$ally you will get this error $essage if your XML docu$ent contains 9foreign9
characters# and the file was saved with a single?byte encoding editor li'e Dotepad# and no encoding
attribute was specified%
Switch fro# current encoding to specified encoding not supported.
(ou will get this error $essage if your file was saved as Fnicode/FT3?1I but the encoding attribute
specified a single?byte encoding li'e ,indows?1@B@# !S6?55B4?1 or FT3?5% (ou can also get this
error $essage if your docu$ent was saved with single?byte encoding# but the encoding attribute
specified a double?byte encoding li'e FT3?1I%
(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:
Fse an editor that supports encoding
Ma'e sure you 'now what encoding it uses
Fse the sa$e encoding attribute in your XML docu$ents
XML on the Ser&er
XML can be generated on a ser&er without installing any XML controls.
Storing XML on the Ser&er
XML files can be stored on an !nternet server e0actly the sa$e way as HTML files%
45
Start ,indows Dotepad 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 9note%0$l9%
8enerating XML with S3
XML can be generated on a ser&er without any installed XML software.
To generate an XML response fro$ the server ? si$ply write the following code and save it as an
>SH file on the web server:
<F
res-onse#>ontentTy-e!text/xml!
res-onse#7riteQ!<?xml version'"#$' en%oding'&'()**+,)"'?>!R
res-onse#7riteQ!<note>!R
res-onse#7riteQ!<from>Jani</from>!R
res-onse#7riteQ!<to>Tove</to>!R
res-onse#7riteQ!<message>Remember me this weekend</message>!R
res-onse#7riteQ!</note>!R
F>
Dote that the content type of the response $ust be set to 9te0t/0$l9%
See how the >SH file will be returned fro$ the server%
!f you donAt 'now how to write >SH# please visit our >SH tutorial
8etting XML fro# 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 >SH file on the web server:
<F
res-onse#>ontentTy-e !text/xml!
set %onn'erver#>reate(bPe%tQ!<D(DL#>onne%tion!R
%onn#-rovider!.i%rosoft#Jet#(39DL#B#$E!
%onn#o-en server#ma--athQ!/db/database#mdb!R
sMl!sele%t fnameJlname from tbl:/estLook!
set rs>onn#9xe%/teQsMlR
rs#.ove1irstQR
res-onse#writeQ!<?xml version'"#$' en%oding'&'()**+,)"'?>!R
res-onse#writeQ!<g/estbook>!R
46
while Qnot rs#9(1R
res-onse#writeQ!<g/est>!R
res-onse#writeQ!<fname>! T rsQ!fname!R T !</fname>!R
res-onse#writeQ!<lname>! T rsQ!lname!R T !</lname>!R
res-onse#writeQ!</g/est>!R
rs#.oveAextQR
wend
rs#%loseQR
%onn#%loseQR
res-onse#writeQ!</g/estbook>!R
F>
See the real life database output fro$ the >SH file above%
The e0a$ple above uses >SH with >*6% !f you donAt 'now how to use >*6# please visit our >*6
tutorial%
XML pplication
This chapter de#onstrates a s#all fra#ework for an XML application.
The XML 0+a#ple $ocu#ent
Loo' at the following XML docu$ent )9cdKcatalog%0$l9+# that represents a .* catalog:
<?xml version!"#$! en%oding!&'()**+,)"!?>
<><T<3(:>
<>D>
<T&T39>9m-ire L/rlesM/e</T&T39>
<<RT&'T>Lob Dylan</<RT&'T>
<>(IATR?>I'<</>(IATR?>
<>(.@<A?>>ol/mbia</>(.@<A?>
<@R&>9>"$#,$</@R&>9>
<?9<R>",*+</?9<R>
</>D>
#
#
### more ###
#
iew the full 9cdKcatalog%0$l9 file in your browser%
Load the XML docu#ent into a $ata Island
$ata Island can be used to access the XML file.
To get your XML docu$ent 9inside9 an HTML page# add an XML *ata !sland to the HTML page:
47
<xml sr%!%dK%atalog#xml! id!xmldso! asyn%!false!>
</xml>
,ith the e0a$ple code above# the XML file 9cdKcatalog%0$l9 will be loaded into an 9invisible9 *ata
!sland called 90$ldso9% The asyncE9false9 attribute is added to $a'e sure that all the XML data is
loaded before any other HTML processing ta'es place%
1ind the $ata Island to an HTML Table
To $a'e the XML data visible on the HTML page# you $ust 9bind9 the *ata !sland to an HTML
ele$ent%
To bind the XML data to an HTML table# add a datasrc attribute to the table ele$ent# and add
datafld attributes to the span ele$ents inside the table data:
<table datasr%!Dxmldso! width!"$$F! border!"!>
<thead>
<th>Title</th>
<th><rtist</th>
<th>?ear</th>
</thead>
<tr align!left!>
<td><s-an datafld!T&T39!></s-an></td>
<td><s-an datafld!<RT&'T!></s-an></td>
<td><s-an datafld!?9<R!></s-an></td>
</tr>
</table>
!f you have !& B%2 or higher: See how the XML data is displayed inside an HTML table%
1ind the $ata Island to LspanO or Ldi&O ele#ents
LspanO or Ldi&O ele#ents can be used to display XML data.
(ou donAt have to use the HTML table ele$ent to display XML data% *ata fro$ a *ata !sland can be
displayed anywhere on an HTML page%
>ll you have to do is to add so$e 7span8 or 7div8 ele$ents to your page% Fse the datasrc
attribute to bind the ele$ents to the *ata !sland# and the datafld attribute to bind each ele$ent to
an XML ele$ent# li'e this:
<br />Title=
<s-an datasr%!Dxmldso! datafld!T&T39!></s-an>
<br /><rtist=
<s-an datasr%!Dxmldso! datafld!<RT&'T!></s-an>
<br />?ear=
<s-an datasr%!Dxmldso! datafld!?9<R!></s-an>
or li'e this:
48
<br />Title=
<div datasr%!Dxmldso! datafld!T&T39!></div>
<br /><rtist=
<div datasr%!Dxmldso! datafld!<RT&'T!></div>
<br />?ear=
<div datasr%!Dxmldso! datafld!?9<R!></div>
!f you have !& B%2 or higher: See how the XML data is displayed inside the HTML ele$ents%
Dote that if you use an HTML 7div8 ele$ent# the data will be displayed on a new line%
,ith the e0a$ples above# you will only see one line of your XML data% To navigate to the ne0t line
of data# you have to add so$e scripting to your code%
dd a 4a&igation Script
4a&igation has to be perfor#ed by a script.
To add navigation to the XML *ata !sland# create a script that calls the $ovene0t)+ and
$oveprevious)+ $ethods of the *ata !sland%
<s%ri-t ty-e!text/Pavas%ri-t!>
f/n%tion movenextQR
C
xxmldso#re%ordset
if Qx#absol/te-osition < x#re%ord%o/ntR
C
x#movenextQR
G
G
f/n%tion move-revio/sQR
C
xxmldso#re%ordset
if Qx#absol/te-osition > "R
C
x#move-revio/sQR
G
G
</s%ri-t>
!f you have !& B%2 or higher: See how you can navigate through the XML records%
ll Together 4ow
With a little creati&ity you can create a full application.
!f you use what you have learned on this page# and a little i$agination# you can easily develop this
into a full application%
!f you have !& B%2 or higher: See how you can add a little fancy to this application
49
The XMLHttp)e5uest *b:ect
The XMLHttp)e5uest ob:ect is supported in Internet 0+plorer ?.AE- Safari @.2- MoBilla
@.A 6 7irefo+- and 4etscape C.
What is an HTT3 )e5uest"
,ith an HTTH re;uest# a web page can $a'e a re;uest to# and get a response fro$ a web server ?
without reloading the page% The user will stay on the sa$e page# and he or she will not notice that
scripts $ight re;uest pages# or send data to a server in the bac'ground%
1y using the XMLHttp)e5uest ob:ect- a web de&eloper can change a page with data fro#
the ser&er after the page has loaded.
Ooogle Suggest is using the XMLHttp/e;uest ob"ect to create a very dyna$ic web interface: ,hen
you start typing in OoogleAs search bo0# a JavaScript sends the letters off to a server and the server
returns a list of suggestions%
Is the XMLHttp)e5uest *b:ect a W'( Standard"
The XMLHttp/e;uest ob"ect is not a ,-. standard%
The ,-. *6M Level - 9Load and Save9 specification contains so$e si$ilar functionality# but these
are not i$ple$ented in any browsers yet% So# at the $o$ent# if you need to send an HTTH re;uest
fro$ a browser# you will have to use the XMLHttp/e;uest ob"ect%
(reating an XMLHttp)e5uest *b:ect
3or Mo:illa# 3irefo0# Safari# and Detscape:
var xmlhtt-new 2.38tt-ReM/estQR
3or !nternet &0plorer:
var xmlhtt-new <%tive2(bPe%tQ!.i%rosoft#2.38TT@!R
!am"le
<s%ri-t ty-e!text/Pavas%ri-t!>
var xmlhtt-
f/n%tion load2.3Do%Q/rlR
C
// %ode for .oHillaJ et%#
if Qwindow#2.38tt-ReM/estR
C
xmlhtt-new 2.38tt-ReM/estQR
50
xmlhtt-#onreadystate%hangexmlhtt->hange
xmlhtt-#o-enQ!:9T!J/rlJtr/eR
xmlhtt-#sendQn/llR
G
// %ode for &9
else if Qwindow#<%tive2(bPe%tR
C
xmlhtt-new <%tive2(bPe%tQ!.i%rosoft#2.38TT@!R
if Qxmlhtt-R
C
xmlhtt-#onreadystate%hangexmlhtt->hange
xmlhtt-#o-enQ!:9T!J/rlJtr/eR
xmlhtt-#sendQR
G
G
G
f/n%tion xmlhtt->hangeQR
C
// if xmlhtt- shows !loaded!
if Qxmlhtt-#ready'tateBR
C
// if !(N!
if Qxmlhtt-#stat/s0$$R
C
// ###some %ode here###
G
else
C
alertQ!@roblem retrieving 2.3 data!R
G
G
G
</s%ri-t>
Try it yourself using JavaScript
The synta0 is a little bit different in BScript: Try it yourself using BScript
4ote9 >n i$portant property in the e0a$ple above is the onreadystatechange property% This
property is an event handler which is triggered each ti$e the state of the re;uest changes% The
states run fro$ 2 )uninitiali:ed+ to C )co$plete+% By having the function 0$lhttp.hange)+ chec' for
the state changing# we can tell when the process is co$plete and continue only if it has been
successful%
Why are we /sing async in our 0+a#ples"
>ll the e0a$ples here use the async $ode )the third para$eter of open)+ set to true+%
The async para$eter specifies whether the re;uest should be handled asynchronously or not% True
$eans that script continues to run after the send)+ $ethod# without waiting for a response fro$ the
server% false $eans that the script waits for a response before continuing script processing% By
setting this para$eter to false# you run the ris' of having your script hang if there is a networ' or
server proble$# or if the re;uest is long )the F! loc's while the re;uest is being $ade+ a user $ay
even see the 9Dot /esponding9 $essage% !t is safer to send asynchronously and design your code
around the onreadystatechange eventG
51
More 0+a#ples
Load a te0tfile into a div ele$ent with XML HTTH )JavaScript+
Ma'e a H&>* re;uest with XML HTTH )JavaScript+
Ma'e a specified H&>* re;uest with XML HTTH )JavaScript+
The XMLHttp)e5uest *b:ect )eference
#etho$s
Method $escription
abort)+ .ancels the current re;uest
get>ll/esponseHeaders)+ /eturns the co$plete set of http headers as a string
get/esponseHeader)9headerna$e9+ /eturns the value of the specified http header
open)9$ethod9#9F/L9#async#9una$e9#9pswd9+ Specifies the $ethod# F/L# and other optional
attributes of a re;uest
The $ethod para$eter can have a value of 9O&T9#
9H6ST9# or 9HFT9 )use 9O&T9 when re;uesting data
and use 9H6ST9 when sending data )especially if the
length of the data is greater than B1@ bytes%
The F/L para$eter $ay be either a relative or
co$plete F/L%
The async para$eter specifies whether the re;uest
should be handled asynchronously or not% true $eans
that script processing carries on after the send)+
$ethod# without waiting for a response% false $eans
that the script waits for a response before continuing
script processing
send)content+ Sends the re;uest
set/e;uestHeader)9label9#9value9+ >dds a label/value pair to the http header to be sent
%ro"erties
3roperty $escription
onreadystatechange >n event handler for an event that fires at every state change
readyState /eturns the state of the ob"ect:
2 E uninitiali:ed
1 E loading
@ E loaded
- E interactive
C E co$plete
responseTe0t /eturns the response as a string
responseXML /eturns the response as XML% This property returns an XML docu$ent
ob"ect# which can be e0a$ined and parsed using ,-. *6M node tree
$ethods and properties
status /eturns the status as a nu$ber )e%g% C2C for 9Dot 3ound9 or @22 for
52
96Q9+
statusTe0t /eturns the status as a string )e%g% 9Dot 3ound9 or 96Q9+
Sa&e $ata to an XML 7ile
/sually- we sa&e data in databases. Howe&er- if we want to #ake the data #ore portable-
we can store the data in an XML file.
(reate and Sa&e an XML 7ile
Storing data in XML files is useful if the data is to be sent to applications on non?,indows platfor$s%
/e$e$ber that XML is portable across all platfor$s and the data will not need to be convertedG
3irst we will learn how to create and save an XML file% The XML file below will be na$ed 9test%0$l9
and will be stored in the c directory on the server% ,e will use >SH and MicrosoftAs XML*6M ob"ect
to create and save the XML file:
<F
Dim xmlDo%J root9lJ %hild"J %hild0J -
'>reate an 2.3 do%/ment
'et xmlDo% 'erver#>reate(bPe%tQ!.i%rosoft#2.3D(.!R
'>reate a root element and a--end it to the do%/ment
'et root9l xmlDo%#%reate9lementQ!root!R
xmlDo%#a--end>hild root9l
'>reate and a--end %hild elements
'et %hild" xmlDo%#%reate9lementQ!%hild"!R
'et %hild0 xmlDo%#%reate9lementQ!%hild0!R
root9l#a--end>hild %hild"
root9l#a--end>hild %hild0
'<dd an 2.3 -ro%essing instr/%tion
'and insert it before the root element
'et -xmlDo%#%reate@ro%essing&nstr/%tionQ!xml!J!version'"#$'!R
xmlDo%#insertLefore -JxmlDo%#%hildAodesQ$R
''ave the 2.3 file to the % dire%tory
xmlDo%#'ave !%=]test#xml!
F>
!f you open the saved XML file it will loo' so$ething li'e this )9test%0$l9+:
<?xml version!"#$!?>
<root>
<%hild" />
<%hild0 />
</root>
)eal 7or# 0+a#ple
53
Dow# we will loo' at a real HTML for$ e0a$ple%
,e will first loo' at the HTML for$ that will be used in this e0a$ple: The HTML for$ below as's for
the userAs na$e# country# and e?$ail address% This infor$ation will then be written to an XML file for
storage%
9custo$ers%ht$9:
<html>
<body>
<form a%tion!save1orm#as-! method!-ost!>
<-><b>9nter yo/r %onta%t information</b></->
1irst Aame= <in-/t ty-e!text! id!fname! name!fname!><br />
3ast Aame= <in-/t ty-e!text! id!lname! name!lname!><br />
>o/ntry= <in-/t ty-e!text! id!%o/ntry! name!%o/ntry!><br />
9mail= <in-/t ty-e!text! id!email! name!email!><br />
<in-/t ty-e!s/bmit! id!btnKs/b! name!btnKs/b! val/e!'/bmit!>
<in-/t ty-e!reset! id!btnKres! name!btnKres! val/e!Reset!>
</form>
</body>
</html>
The action for the HTML for$ above is set to 9save3or$%asp9% The 9save3or$%asp9 file is an >SH
page that will loop through the for$ fields and store their values in an XML file:
<F
dim xmlDo%
dim root9lJfieldAameJfield^al/eJatt&D
dim -Ji
'Do not sto- if an error o%%/rs
(n 9rror Res/me Aext
'et xmlDo% server#>reate(bPe%tQ!.i%rosoft#2.3D(.!R
xmlDo%#-reserve7hite'-a%etr/e
'>reate a root element and a--end it to the do%/ment
'et root9l xmlDo%#%reate9lementQ!%/stomer!R
xmlDo%#a--end>hild root9l
'3oo- thro/gh the form %olle%tion
for i " To ReM/est#1orm#>o/nt
'9liminate b/tton elementsin the form
if instrQ"JReM/est#1orm#NeyQiRJ!btnK!R$ then
'>reate a field and a val/e elementJ and an id attrib/te
'et fieldAame xmlDo%#%reate9lementQ!field!R
'et field^al/e xmlDo%#%reate9lementQ!val/e!R
'et att&D xmlDo%#%reate<ttrib/teQ!id!R
''et the val/e of the id attrib/te eM/al to the name of
'the %/rrent form field
att&D#Text ReM/est#1orm#NeyQiR
'<--end the id attrib/te to the field element
fieldAame#set<ttrib/teAode att&D
''et the val/e of the val/e element eM/al to
'the val/e of the %/rrent form field
field^al/e#Text ReM/est#1ormQiR
'<--end the field element as a %hild of the root element
root9l#a--end>hild fieldAame
'<--end the val/e element as a %hild of the field element
fieldAame#a--end>hild field^al/e
end if
54
next
'<dd an 2.3 -ro%essing instr/%tion
'and insert it before the root element
'et - xmlDo%#%reate@ro%essing&nstr/%tionQ!xml!J!version'"#$'!R
xmlDo%#insertLefore -JxmlDo%#%hildAodesQ$R
''ave the 2.3 file
xmlDo%#save !%=]>/stomer#xml!
'Release all obPe%t referen%es
set xmlDo%nothing
set root9lnothing
set fieldAamenothing
set field^al/enothing
set att&Dnothing
set -nothing
'Test to see if an error o%%/rred
if err#n/mber<>$ then
res-onse#writeQ!9rror= Ao information saved#!R
else
res-onse#writeQ!?o/r information has been saved#!R
end if
F>
4ote9 !f the XML file na$e specified already e0ists# it will be overwrittenG
The XML file that will be produced by the code above will loo' so$ething li'e this )9.usto$er%0$l9+:
<?xml version!"#$! ?>
<%/stomer>
<field id!firstAame!>
<val/e>8ege</val/e>
</field>
<field id!lastAame!>
<val/e>Refsnes</val/e>
</field>
<field id!%o/ntry!>
<val/e>Aorway</val/e>
</field>
<field id!email!>
<val/e>mymail_myaddress#%om</val/e>
</field>
</%/stomer>
XML $HTML 1eha&iors
Internet 0+plorer ? introduced $HTML beha&iors. 1eha&iors are a way to add $HTML
functionality to HTML ele#ents with the ease of (SS.
1eha&iors % What are They"
55
!&B introduced *HTML behaviors% Behaviors are a way to add *HTML functionality to HTML ele$ents
with the ease of .SS%
How does behaviors wor'= By using XML we can lin' behaviors to any ele$ent in a web page and
$anipulate that ele$ent%
*HTML behaviors do not use a 7script8 tag% !nstead# they are using a .SS attribute called
9behavior9% This 9behavior9 specifies a F/L to an HT. file which contains the actual behavior )The
HT. file is written in XML+%
Synta!
behavior= /rlQsome_filename#ht%R
4ote9 The behavior attribute is only supported by !& B and higher# all other browsers will ignore it%
This $eans that Mo:illa# 3irefo0# Detscape and other browsers will only see the regular content and
!& BP can see the *HTML behaviors%
0+a#ple
The following HTML file has a 7style8 ele$ent that defines a behavior for the 7h18 ele$ent:
<html>
<head>
<style ty-e!text/%ss!>
h" C behavior= /rlQbehave#ht%R G
</style>
</head>
<body>
<h">.o/se over me!!!</h">
</body>
</html>
The XML docu$ent 9behave%htc9 is shown below:
<atta%h for!element! event!onmo/seover! handler!higKlite! />
<atta%h for!element! event!onmo/seo/t! handler!lowKlite! />
<s%ri-t ty-e!text/Pavas%ri-t!>
f/n%tion higKliteQR
C
element#style#%olor'red'
G
f/n%tion lowKliteQR
C
element#style#%olor'bl/e'
G
</s%ri-t>
The behavior file contains a JavaScript and the event handlers for the script%
Try it yourself )$ouse over the te0t in the e0a$ple+%
56
The following HTML file has a 7style8 ele$ent that defines a behavior for ele$ents with an id of
9typing9:
<html>
<head>
<style ty-e!text/%ss!>
Dty-ing
C
behavior=/rlQty-ing#ht%RE
font)family='%o/rier new'E
G
</style>
</head>
<body>
<s-an id!ty-ing! s-eed!"$$!>&9+ introd/%ed D8T.3 behaviors#
Lehaviors are a way to add D8T.3 f/n%tionality to 8T.3 elements
with the ease of >''#<br /><br />8ow does behaviors work?<br />
Ly /sing 2.3 we %an link behaviors to any element in a web -age
and mani-/late that element#</->
</s-an>
</body>
</html>
The XML docu$ent 9typing%htc9 is shown below:
<atta%h for!window! event!onload! handler!beginTy-ing! />
<method name!ty-e! />
<s%ri-t ty-e!text/Pavas%ri-t!>
var iJtext"Jtext0Jtext3engthJt
f/n%tion beginTy-ingQR
C
i$
text"element#innerText
text3engthtext"#length
element#innerText!!
text0!!
twindow#set&ntervalQelement#idS!#ty-eQR!Js-eedR
G
f/n%tion ty-eQR
C
text0text0Stext"#s/bstringQiJiS"R
element#innerTexttext0
iiS"
if Qitext3engthRC%lear&ntervalQtRG
G
</s%ri-t>
Try it yourself
XML )elated Technologies
1elow is a list of XML%related technologies.
57
XHTML J0+tensible HTMLK is a stricter and cleaner version of HTML%
XML $*M JXML $ocu#ent *b:ect ModelK defines a standard way for accessing and
$anipulating XML docu$ents%
XSL J0+tensible Style Sheet LanguageK ? XSL consists of three parts: XSLT ? a language for
transfor$ing XML docu$ents# XHath ? a language for navigating in XML docu$ents# and XSL?36 ? a
language for for$atting XML docu$ents%
XSLT JXSL Transfor#ationsK is used to transfor$ XML docu$ents into other XML for$ats# li'e
XHTML%
X3ath is a language for navigating in XML docu$ents%
XSL%7* J0+tensible Style Sheet Language 7or#atting *b:ectsK is an XML based $ar'up
language describing the for$atting of XML data for output to screen# paper or other $edia%
XLink JXML Linking LanguageK is a language for creating hyperlin's in XML docu$ents%
X3ointer JXML 3ointer LanguageK allows the XLin' hyperlin's to point to $ore specific parts in
the XML docu$ent%
$T$ J$ocu#ent Type $efinitionK is used to define the legal ele$ents in an XML docu$ent%
XS$ JXML Sche#aK is an XML?based alternative to *T*s%
X7or#s JXML 7or#sK uses XML to define for$ data%
X;uery JXML ;uery LanguageK is designed to ;uery XML data%
S*3 JSi#ple *b:ect ccess 3rotocolK is an XML?based protocol to let applications e0change
infor$ation over HTTH%
WS$L JWeb Ser&ices $escription LanguageK is an XML?based language for describing web
services%
)$7 J)esource $escription 7ra#eworkK is an XML?based language for describing web
resources%
)SS J)eally Si#ple SyndicationK is a for$at for syndicating news and the content of news?li'e
sites%
W3 JWireless pplication 3rotocolK was designed to show internet contents on wireless clients#
li'e $obile phones%
SMIL JSynchroniBed Multi#edia Integration LanguageK is a language for describing
audiovisual presentations%
S=8 JScalable =ector 8raphicsK defines graphics in XML for$at%
XML 0ditors
58
If you are serious about XML- you will benefit fro# using a professional XML 0ditor.
XML is Te+t%based
XML is a te0t?based $ar'up language%
6ne great thing about XML is that XML files can be created and edited using a si$ple te0t?editor li'e
Dotepad%
However# when you start wor'ing with XML# you will soon find that it is better to edit XML
docu$ents using a professional XML editor%
Why 4ot 4otepad"
Many web developers use Dotepad to edit both HTML and XML docu$ents because Dotepad is
included with the $ost co$$on 6S and it is si$ple to use% Hersonally ! often use Dotepad for ;uic'
editing of si$ple HTML# .SS# and XML files%
But# if you use Dotepad for XML editing# you will soon run into proble$s%
Dotepad does not 'now that you are writing XML# so it will not be able to assist you%
Why an XML 0ditor"
Today XML is an i$portant technology# and develop$ent pro"ects use XML?based technologies li'e:
XML Sche$a to define XML structures and data types
XSLT to transfor$ XML data
S6>H to e0change XML data between applications
,S*L to describe web services
/*3 to describe web resources
XHath and X<uery to access XML data
SM!L to define graphics
To be able to write error?free XML docu$ents# you will need an intelligent XML editorG
XML 0ditors
Hrofessional XML editors will help you to write error?free XML docu$ents# validate your XML against
a *T* or a sche$a# and force you to stic' to a valid XML structure%
>n XML editor should be able to:
>dd closing tags to your opening tags auto$atically
59
3orce you to write valid XML
erify your XML against a *T*
erify your XML against a Sche$a
.olor code your XML synta0
lto&a<s XMLS3Y
>t ,-Schools we have been using XMLSH( for $any years% XMLSH( is our favorite XML editor%
These are so$e of the features we especially li'e:
&asy to use
Synta0 coloring
>uto$atic tag co$pletion
>uto$atic well?for$ed chec'
&asy switching between te0t view and grid view
Built in *T* and / or Sche$a validation
Built in graphical XML Sche$a designer
Howerful conversion utilities
*atabase i$port and e0port
Built in te$plates for $ost XML docu$ent types
Built in XHath analy:er
3ull S6>H and ,S*L capabilities
Howerful pro"ect $anage$ent
/ead $ore about XMLSH(
60

Das könnte Ihnen auch gefallen