Sie sind auf Seite 1von 73

XML

(karROX Release Year 2007 Verison 1.0)


Table of Contents
Chapter 1 : Introduction to XML
1.1 XML GOAL and prerequisite
1.2The main difference between XML and HTML
1.3 How can XML be Used?
Chapter 2 : XML Syntax
2.1 The syntax rules of XML
2.2 XML lements
2.3 XML !ttributes
Chapter 3 : XML DTD and Schema
3.1 "#ell $ormed" XML documents
3.2 %ntroduction to XML &T& and schema
3.3 &i''in' dee( into &T&
3.) &i''in' dee(er into schema
Chapter 4 : CSS
).1 &is(layin' xml usin' css
).2 *++ +yntax
).3 *++ How To...
Chapter 5 : XSL
,.1 %ntroduction to X+L
,.2 X+L -rowsers
,.3 X+L . Transformation /X+LT0
,.) X+L 1 Tem(lates
,., X+L . 2n the *lient
,.3 X+L +ort
,.4 X+L $ilter 5uery
,.6 X+L *onditional %$
,.7 X+L *onditional *hoose
Chapter 1
Introduction to XML
1.1 XML GOAL and prerequisite
XML was desi'ned to describe data8 and to focus on what data is.
HTML was desi'ned to dis(lay data8 and to focus on how data loo9s.
What you should already know
Before you continue you should have soe !asic understandin" of the follo#in"$
%%%& '()* and the !asics of !uildin" %e! +a"es
%e! scri+tin" lan"ua"es like ,ava-cri+t or VB-cri+t
What is XML?
X)* stands for .Xtensi!le Marku+ Lan"ua"e
X)* is a markup language uch like '()*.
X)* #as desi"ned to describe data.
X)* ta"s are not +redefined in X)*. You ust define your own tags.
X)* uses a /(/ (Document Type Definition) to descri!e the data.
X)* #ith a /(/ is desi"ned to !e self-descriptie.
!"# The main difference between XML and $TML
XML was designed to carry data"
X)* is not a re+laceent for '()*.
X)* and '()* #ere desi"ned #ith different "oals$
X)* #as desi"ned to descri!e data and to focus on #hat data is.
'()* #as desi"ned to dis+lay data and to focus on ho# data looks.
'()* is a!out dis+layin" inforation& X)* is a!out descri!in" inforation.
XML does not D% anything
XML was not designed to D% anything"
)ay!e it is a little hard to understand& !ut X)* does not /O anythin". X)* #as not ade to /O
anythin". X)* is created as a #ay to structure& store and send inforation.
(he follo#in" e0a+le is a note to (ove fro ,ani& stored as X)*$
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
(he note has a header& and a essa"e !ody. 1t also has sender and receiver inforation. But still&
this X)* docuent does not /O anythin". 1t is 2ust +ure inforation #ra++ed in X)* ta"s. -oeone
ust #rite a +iece of soft#are to send it& receive it or dis+lay it.
XML is free and e&tensible
XML tags are not predefined" 'ou must (inent( your own tags"
(he ta"s used to arku+ '()* docuents and the structure of '()* docuents are +redefined.
(he author of '()* docuents can only use ta"s that are defined in the '()* standard (like 3+4
and 3h14.....).
X)* allo#s the author to define his o#n ta"s and his o#n docuent structure
(he ta"s in the e0a+le a!ove (like 3to4 and 3fro4)& are not defined in any X)* standard. (hese
ta"s are 5invented5 !y the author of the X)* docuent.
XML is a complement to $TML
XML is not a replacement for $TML"
1t is i+ortant to understand that X)* is not a re+laceent for '()*. 1n future %e! develo+ent it
is ost likely that X)* #ill !e used to descri!e the data& #hile '()* #ill !e used to forat and
dis+lay the sae data.
)y !est descri+tion of X)* is as a cross6+latfor& soft#are and hard#are inde+endent tool for
transmitting information.
XML in future Web deelopment
XML is going to be eerywhere"
%e have !een +artici+atin" in X)* develo+ent since its creation. 1t has !een aa7in" to see ho#
8uickly the X)* standard has !een develo+ed& and ho# 8uickly a lar"e nu!er of soft#are vendors
have ado+ted the standard.
%e stron"ly !elieve that X)* #ill !e as i+ortant to the future of the %e! as '()* has !een to the
foundation of the %e!& and that X)* #ill !e the ost coon tool for all data ani+ulation and
data transission.
!")$ow can XML be *sed?
+t is important to know that XML was designed to store, carry and e&change data" +t was
not designed to display data"
XML can -eparate Data from $TML
With XML, your data is stored outside your $TML"
%hen '()* is used to dis+lay data& the data is stored inside your '()*. %ith X)*& data can !e
stored in se+arate X)* files. (his #ay you can concentrate on usin" '()* for data layout and
dis+lay& and !e sure that chan"es in the underlyin" data #ill not re8uire any chan"es to your '()*.
X)* data can also !e stored inside '()* +a"es as 5/ata 1slands5. You can still concentrate on usin"
'()* only for forattin" and dis+layin" the data.
XML is used to .&change Data
With XML, data can be e&changed between incompatible systems"
1n the real #orld& co+uter systes and data!ases contain data in inco+ati!le forats. One of the
ost tie6consuin" challen"es for develo+ers has !een to e0chan"e data !et#een such systes
over the 1nternet.
9onvertin" the data to X)* can "reatly reduce this co+le0ity and create data that can !e read !y
any different ty+es of a++lications.
XML and /#/
With XML, financial information can be e&changed oer the +nternet"
.0+ect to see a lot a!out X)* and B2B (Business (o Business) in the near future.
X)* is "oin" to !e the ain lan"ua"e for e0chan"in" financial inforation !et#een !usinesses over
the 1nternet. : lot of interestin" B2B a++lications are under develo+ent.
XML can be used to -hare Data
With XML, plain te&t files can be used to share data"
-ince X)* data is stored in +lain te0t forat& X)* +rovides a soft#are6 and hard#are6inde+endent
#ay of sharin" data.
(his akes it uch easier to create data that different a++lications can #ork #ith. 1t also akes it
easier to e0+and or u+"rade a syste to ne# o+eratin" systes& servers& a++lications& and ne#
!ro#sers.
XML can be used to -tore Data
With XML, plain te&t files can be used to store data"
X)* can also !e used to store data in files or in data!ases. :++lications can !e #ritten to store and
retrieve inforation fro the store& and "eneric a++lications can !e used to dis+lay the data.
XML can make your Data more *seful
With XML, your data is aailable to more users"
-ince X)* is inde+endent of hard#are& soft#are and a++lication& you can ake your data availa!le
to ore than only standard '()* !ro#sers.
Other clients and a++lications can access your X)* files as data sources& like they are accessin"
data!ases. Your data can !e ade availa!le to all kinds of 5readin" achines5 (a"ents)& and it is
easier to ake your data availa!le for !lind +eo+le& or +eo+le #ith other disa!ilities.
XML can be used to Create new Languages
XML is the mother of W01 and WML"
(he %ireless )arku+ *an"ua"e (%)*)& used to arku+ 1nternet a++lications for handheld devices
like o!ile +hones& is #ritten in X)*.
+f Deelopers hae -ense
+f they D% hae sense, all future applications will e&change their data in XML"
(he future i"ht "ive us #ord +rocessors& s+readsheet a++lications and data!ases that can read
each other;s data in a +ure te0t forat& #ithout any conversion utilities in !et#een.
%e can only +ray that )icrosoft and all the other soft#are vendors #ill a"ree.
FAQ
1. What does XML stand for?
eXtra Modern Link
eXtensible Markup Language
XMarkup Language
!"a#ple Markup Language

$. %here is a &a' of des(ribing XML data) ho&?
XML uses a des(ription node to des(ribe data
XML uses X*L to des(ribe data
XML uses a +%+ to des(ribe the data
,. XML-s goal is to repla(e .%ML
/alse
%rue

0 %t is im(ortant to 9now that XML was desi'ned to store8 carry and
exchan'e data. %t was not desi'ned to dis(lay data
%rue
/alse
1 XML also ma9es it easier to ex(and or u('rade a system to new
o(eratin' systems8 ser:ers8 a((lications8 and new browsers.
%rue
/alse
Chapter 2
XML Syntax
XML Syntax
2! The synta& rules of XML
The "yntax ru#e" o$ XML are %ery "imp#e and %ery "trict The ru#e" are %ery ea"y to
#earn& and %ery ea"y to u"e
/ecause of this, creating software that can read and manipulate XML is ery easy to do"
0n e&ample XML document
XML documents use a self-describing and simple synta&"
<?xml version!"#$!?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
(he first line in the docuent 6 the X)* declaration 6 defines the X)* version of the docuent. 1n
this case the docuent confors to the 1.0 s+ecification of X)*.
(he ne0t line descri!es the root eleent of the docuent (like it #as sayin"$ 5this docuent is a
note5)$
<note>
(he ne0t < lines descri!e < child eleents of the root (to& fro& headin"& and !ody)$
<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 eleent$
</note>
9an you detect fro this e0a+le that the X)* docuent contains a =ote to (ove fro ,ani> /on;t
you a"ree that X)* is +retty self6descri+tive>
0ll XML elements must hae a closing tag
With XML, it is illegal to omit the closing tag"
1n '()* soe eleents do not have to have a closin" ta". (he follo#in" code is le"al in '()*$
<%>This is a %aragra%h
<%>This is another %aragra%h
1n X)* all eleents ust have a closin" ta" like this$
<%>This is a %aragra%h</%>
<%>This is another %aragra%h</%>
2ote$ You i"ht have noticed fro the +revious e0a+le that the X)* declaration did not have a
closin" ta". (his is not an error. (he declaration is not a +art of the X)* docuent itself. 1t is not an
X)* eleent& and it should not have a closin" ta".
XML tags are case sensitie
*nlike $TML, XML tags are case sensitie"
%ith X)*& the ta" 3*etter4 is different fro the ta" 3letter4.
O+enin" and closin" ta"s ust therefore !e #ritten #ith the sae case$
<&essage>This is in'orre't</message>
<message>This is 'orre't</message>
0ll XML elements must be properly nested
+mproper nesting of tags makes no sense to XML"
1n '()* soe eleents can !e i+ro+erly nested #ithin each other like this$
<b><i>This text is bold and itali'</b></i>
1n X)* all eleents ust !e +ro+erly nested #ithin each other like this$
<b><i>This text is bold and itali'</i></b>
0ll XML documents must hae a root tag
The first tag in an XML document is the root tag"
:ll X)* docuents ust contain a sin"le ta" +air to define the root eleent. :ll other eleents
ust !e nested #ithin the root eleent.
:ll eleents can have su! eleents (children). -u! eleents ust !e correctly nested #ithin their
+arent eleent$
<root>
<'hild>
<s(b'hild>#####</s(b'hild>
</'hild>
</root>
0ttribute alues must always be 3uoted
With XML, it is illegal to omit 3uotation marks around attribute alues"
X)* eleents can have attri!utes in nae?value +airs 2ust like in '()*. 1n X)* the attri!ute value
ust al#ays !e 8uoted. -tudy the t#o X)* docuents !elo#. (he first one is incorrect& the second
is correct$
<?xml version!"#$!?>
<note date")/""/**>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<?xml version!"#$!?>
<note date!")/""/**!>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
(he error in the first docuent is that the date attri!ute in the note eleent is not 8uoted.
(his is correct$ date@512?11?AA5. (his is incorrect$ date@12?11?AA.
With XML, White -pace is 1resered
With XML, the white space in your document is not truncated.
(his is unlike '()*. %ith '()*& a sentence like this$ 'ello y nae is (ove& #ill !e
dis+layed like this$ 'ello y nae is (ove& !ecause '()* stri+s off the #hite s+ace.
With XML, C4 5 L6 is Conerted to L6
With XML, a new line is always stored as L6.
/o you kno# #hat a ty+e#riter is>. %ell& a ty+e#riter is a ty+e of echanical device they used in
the +revious century $6)
:fter you have ty+ed one line of te0t on a ty+e#riter& you have to anually return the +rintin"
carria"e to the left ar"in +osition and anually feed the +a+er u+ one line.
1n %indo#s a++lications& a ne# line in the te0t is norally stored as a +air of 9R *B (carria"e
return& line feed) characters. 1n Cni0 a++lications& a ne# line is norally stored as a *B character.
-oe a++lications use only a 9R character to store a ne# line.
Comments in XML
(he synta0 for #ritin" coents in X)* is siilar to that of '()*.
3D66 (his is a coent 664
There is 2othing -pecial about XML
(here is nothin" s+ecial a!out X)*. 1t is 2ust +lain te0t #ith the addition of soe X)* ta"s enclosed
in an"le !rackets.
-oft#are that can handle +lain te0t can also handle X)*. 1n a si+le te0t editor& the X)* ta"s #ill
!e visi!le and #ill not !e handled s+ecially.
1n an X)*6a#are a++lication ho#ever& the X)* ta"s can !e handled s+ecially. (he ta"s ay or ay
not !e visi!le& or have a functional eanin"& de+endin" on the nature of the a++lication.
#"# XML .lements
XML .lements are e&tensible and they hae relationships"
XML .lements hae simple naming rules"
XML .lements are .&tensible
XML documents can be e&tended to carry more information"
*ook at the follo#in" X)* =O(. e0a+le$
<note>
<to>Tove</to>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>
*et;s ia"ine that #e created an a++lication that e0tracted the 3to4& 3fro4& and 3!ody4
eleents fro the X)* docuent to +roduce this out+ut$
M.--07.
To8 (ove
6rom8 ,ani
/on;t for"et e this #eekendD
1a"ine that the author of the X)* docuent added soe e0tra inforation to it$
<note>
<date>"***+$,+$"</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
-hould the a++lication !reak or crash>
=o. (he a++lication should still !e a!le to find the 3to4& 3fro4& and 3!ody4 eleents in the X)*
docuent and +roduce the sae out+ut.
XML documents are .&tensible"
XML .lements hae 4elationships
.lements are related as parents and children"
(o understand X)* terinolo"y& you have to kno# ho# relationshi+s !et#een X)* eleents are
naed& and ho# eleent content is descri!ed.
1a"ine that this is a descri+tion of a !ook$
/ook Title8 My 6irst XML
9ha+ter 1$ 1ntroduction to X)*
%hat is '()*
%hat is X)*
9ha+ter 2$ X)* -ynta0
.leents ust have a closin" ta"
.leents ust !e +ro+erly nested
1a"ine that this X)* docuent descri!es the !ook$
<book>
<title>&y -irst .&/</title>
<%rod id!00+123! media!%a%er!></%rod>
<'ha%ter>4ntrod('tion to .&/
<%ara>5hat is 6T&/</%ara>
<%ara>5hat is .&/</%ara>
</'ha%ter>
<'ha%ter>.&/ 7yntax
<%ara>8lements m(st have a 'losing tag</%ara>
<%ara>8lements m(st be %ro%erly nested</%ara>
</'ha%ter>
</book>
Book is the root element. (itle& +rod& and cha+ter are child elements of !ook. Book is the parent
element of title& +rod& and cha+ter. (itle& +rod& and cha+ter are siblings (or sister elements)
!ecause they have the sae +arent.
.lements hae Content
.lements can hae different content types"
:n XML element is everythin" fro (includin") the eleent;s start ta" to (includin") the eleent;s
end ta".
:n eleent can have element content& mi&ed content& simple content& or empty content. :n
eleent can also have attributes.
1n the e0a+le a!ove& !ook has element content& !ecause it contains other eleents. 9ha+ter
has mi&ed content !ecause it contains !oth te0t and other eleents. Eara has simple content (or
te&t content) !ecause it contains only te0t. Erod has empty content& !ecause it carries no
inforation.
1n the e0a+le a!ove only the +rod eleent has attributes. (he attribute naed id has the
alue 5FF6GH75. (he attribute naed edia has the alue 5+a+er5.
.lement 2aming
XML elements must follow these naming rules8
=aes can contain letters& nu!ers& and other characters
=aes ust not start #ith a nu!er or +unctuation character
=aes ust not start #ith the letters 0l (or X)* or Xl ..)
=aes cannot contain s+aces
(ake care #hen you 5invent5 eleent naes and follo# these si+le rules$
:ny nae can !e used& no #ords are reserved& !ut the idea is to ake naes descri+tive. =aes
#ith an underscore se+arator are nice.
.0a+les$ 3firstInae4& 3lastInae4.
:void 565 and 5.5 in naes. 1t could !e a ess if your soft#are tried to su!tract nae fro first
(first6nae) or think that 5nae5 is a +ro+erty of the o!2ect 5first5 (first.nae).
.leent naes can !e as lon" as you like& !ut don;t e0a""erate. =aes should !e short and
si+le& like this$ 3!ookItitle4 not like this$ 3theItitleIofItheI!ook4.
X)* docuents often have a corres+ondin" data!ase& in #hich fields e0ist corres+ondin" to
eleents in the X)* docuent. : "ood +ractice is to use the nain" rules of your data!ase for the
eleents in the X)* docuents.
=on6.n"lish letters like JKL are +erfectly le"al in X)* eleent naes& !ut #atch out for +ro!les if
your soft#are vendor doesn;t su++ort the.
(he 5$5 should not !e used in eleent naes !ecause it is reserved to !e used for soethin" called
naes+aces (ore later).
#") XML 0ttributes
XML elements can hae attributes in the start tag, 9ust like $TML"
0ttributes are used to proide additional information about elements"
XML 0ttributes
XML elements can hae attributes"
Bro '()* you #ill ree!er this$ 31)M -R9@5co+uter."if54. (he -R9 attri!ute +rovides
additional inforation a!out the 1)M eleent.
1n '()* (and in X)*) attri!utes +rovide additional inforation a!out eleents$
<img sr'!'om%(ter#gif!>
<a href!demo#as%!>
:ttri!utes often +rovide inforation that is not a +art of the data. 1n the e0a+le !elo#& the file
ty+e is irrelevant to the data& !ut i+ortant to the soft#are that #ants to ani+ulate the eleent$
<file ty%e!gif!>'om%(ter#gif</file>
:uote -tyles, (female( or ;female;?
:ttri!ute values ust al#ays !e enclosed in 8uotes& !ut either sin"le or dou!le 8uotes can !e used.
Bor a +erson;s se0& the +erson ta" can !e #ritten like this$
<%erson sex!female!>
or like this$
<%erson sex'female'>
/ou!le 8uotes are the ost coon& !ut soeties (if the attri!ute value itself contains 8uotes) it
is necessary to use sin"le 8uotes& like in this e0a+le$
<gangster name'9eorge !7hotg(n! :iegler'>

*se of .lements s" 0ttributes
Data can be stored in child elements or in attributes"
(ake a look at these e0a+les$
<%erson sex!female!>
<firstname>;nna</firstname>
<lastname>7mith</lastname>
</%erson>
<%erson>
<sex>female</sex>
<firstname>;nna</firstname>
<lastname>7mith</lastname>
</%erson>
1n the first e0a+le se0 is an attri!ute. 1n the last& se0 is a child eleent. Both e0a+les +rovide
the sae inforation.
(here are no rules a!out #hen to use attri!utes& and #hen to use child eleents. )y e0+erience is
that attri!utes are handy in '()*& !ut in X)* you should try to avoid the. Cse child eleents if
the inforation feels like data.
My 6aorite Way
+ like to store data in child elements"
(he follo#in" three X)* docuents contain e0actly the sae inforation$
: date attri!ute is used in the first e0a+le$
<note date!")/""/**!>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
: date eleent is used in the second e0a+le$
<note>
<date>")/""/**</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
:n e0+anded date eleent is used in the third$ (('1- 1- )Y B:VOR1(.)$
<note>
<date>
<day>")</day>
<month>""</month>
<year>**</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
0oid using attributes?
-hould you aoid using attributes?
'ere are soe of the +ro!les usin" attri!utes$
attri!utes cannot contain ulti+le values (child eleents can)
attri!utes are not easily e0+anda!le (for future chan"es)
attri!utes cannot descri!e structures (child eleents can)
attri!utes are ore difficult to ani+ulate !y +ro"ra code
attri!ute values are not easy to test a"ainst a /(/
1f you use attri!utes as containers for data& you end u+ #ith docuents that are difficult to read and
aintain. (ry to use elements to descri!e data. Cse attri!utes only to +rovide inforation that is
not relevant to the data.
/on;t end u+ like this ( if you think this looks like X)*& you have not understood the +oint)$
<note day!")! month!""! year!**!
to!Tove! from!Jani! heading!Reminder!
body!Don't forget me this weekend!!>
</note>
0n .&ception to my 0ttribute rule
4ules always hae e&ceptions"
)y rule a!out attri!utes has one e0ce+tion$
-oeties 1 assi"n 1/ references to eleents. (hese 1/ references can !e used to access X)*
eleents in uch the sae #ay as the =:). or 1/ attri!utes in '()*. (his e0a+le deonstrates
this$
<messages>
<note 4D!2$"!>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note 4D!2$)!>
<to>Jani</to>
<from>Tove</from>
<heading>Re< Reminder</heading>
<body>4 will not!</body>
</note>
</messages>
(he 1/ in these e0a+les is 2ust a counter& or a uni8ue identifier& to identify the different notes in
the X)* file& and not a +art of the note data.
%hat 1 a tryin" to say here is that etadata (data a!out data) should !e stored as attri!utes& and
that data itself should !e stored as eleents.
FAQ
1. %hat is the correct synta0 of the declaration #hich defines the X)* version>
a)30l version@51.05 ?4
!)3>0l version@51.05 ?4
c)3>0l version@51.05>4

2. %hich stateent is true>
a) :ll the stateents are true
!) :ll X)* docuents ust have a /(/
c) :ll X)* eleents ust !e lo#er case
d) :ll X)* eleents ust have a closin" ta"
F. %hich stateent is true>
a) :ll the stateents are true
!) X)* docuents ust have a root ta"
c) X)* eleents ust !e +ro+erly nested
d) X)* ta"s are case sensitive
<. X)* +reserve #hite s+aces synta
a) Balse
!) (rue

H. 1s this a correct X)* docuent>
3>0l version@51.05>4
3note4
3to a"e@52A54(ove3?to4
3fro4,ani3?fro4
3?note4
a) =o
!) Yes
Chapter '
XML DTD and Schema
XML DTD and -chema
)"!(Well 6ormed( XML documents
XML with correct synta& is Well 6ormed XML"
XML alidated against a DTD is <alid XML"
(Well 6ormed( XML documents
0 (Well 6ormed( XML document has correct XML synta&"
: 5%ell Bored5 X)* docuent is a docuent that confors to the X)* synta0 rules that #ere
descri!ed in the +revious cha+ters$
<?xml version!"#$!?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
(<alid( XML documents
0 (<alid( XML document also conforms to a DTD"
: 5Valid5 X)* docuent is a 5%ell Bored5 X)* docuent& #hich also confors to the rules of a
/ocuent (y+e /efinition (/(/)$
<?xml version!"#$!?>
<!D=>T?@8 note 7?7T8& !4nternalAote#dtd!>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
)"# introduction to XML DTD and schema
0 DTD defines the legal elements of an XML document"
(he +ur+ose of a /(/ is to define the le"al !uildin" !locks of an X)* docuent. 1t defines the
docuent structure #ith a list of le"al eleents. You can read ore a!out /(/& and ho# to validate
your X)* docuents in
XML -chema
X-chema is an XML based alternatie to DTD"
%F9 su++orts an alternative to /(/ called X)* -chea. You can read ore a!out X)* -chea in
.rrors will -top you
.rrors in XML documents will stop the XML program"
(he %F9 X)* s+ecification states that a +ro"ra should not continue to +rocess an X)* docuent
if it finds a validation error. (he reason is that X)* soft#are should !e easy to #rite& and that all
X)* docuents should !e co+ati!le.
%ith '()* it #as +ossi!le to create docuents #ith lots of errors (like #hen you for"et an end ta").
One of the ain reasons that '()* !ro#sers are so !i" and inco+ati!le& is that they have their
o#n #ays to fi"ure out #hat a docuent should look like #hen they encounter an '()* error.
%ith X)* this should not !e +ossi!le.
''Di((in( deep into DTD
The purpose of a Document Type Definition is to define the legal building blocks of an
XML document" +t defines the document structure with a list of legal elements"
0 DTD can be declared inline in your XML document, or as an e&ternal reference"
+nternal D%CT'1. declaration
1f the /(/ is included in your X)* source file& it should !e #ra++ed in a /O9(YE. definition #ith the
follo#in" synta0$
<!D=>T?@8 root+element Belement+de'larationsC>
.0a+le X)* docuent #ith a /(/$ (O+en it in 1.H& and select vie# source)$
<?xml version!"#$!?>
<!D=>T?@8 note B
<!8/8&8AT note DtoEfromEheadingEbodyF>
<!8/8&8AT to DG@>D;T;F>
<!8/8&8AT from DG@>D;T;F>
<!8/8&8AT heading DG@>D;T;F>
<!8/8&8AT body DG@>D;T;F>
C>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
(he /(/ a!ove is inter+reted like this$
=D%CT'1. note (in line 2) defines that this is a docuent of the ty+e note"
=.L.M.2T note (in line F) defines the note eleent as havin" four eleents$
5to&fro&headin"&!ody5.
=.L.M.2T to (in line <) defines the to eleent to !e of the ty+e 5NE9/:(:5.
=.L.M.2T from (in line H) defines the from eleent to !e of the ty+e 5NE9/:(:5
and so on.....
.&ternal D%CT'1. declaration
1f the /(/ is e0ternal to your X)* source file& it should !e #ra++ed in a /O9(YE. definition #ith the
follo#in" synta0$
<!D=>T?@8 root+element 7?7T8& !filename!>
(his is the sae X)* docuent as a!ove& !ut #ith an e0ternal /(/$ (O+en it in 1.H& and select
vie# source)
<?xml version!"#$!?>
<!D=>T?@8 note 7?7T8& !note#dtd!>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
:nd this is a co+y of the file 5note.dtd5 containin" the /(/$
<!8/8&8AT note DtoEfromEheadingEbodyF>
<!8/8&8AT to DG@>D;T;F>
<!8/8&8AT from DG@>D;T;F>
<!8/8&8AT heading DG@>D;T;F>
<!8/8&8AT body DG@>D;T;F>
Why use a DTD?
%ith /(/& each of your X)* files can carry a descri+tion of its o#n forat #ith it.
%ith a /(/& inde+endent "rou+s of +eo+le can a"ree to use a coon /(/ for interchan"in" data.
Your a++lication can use a standard /(/ to verify that the data you receive fro the outside #orld
is valid.
You can also use a /(/ to verify your o#n data.
DTD - XML building blocks
The main building blocks of both XML and $TML documents are tagged elements like
>body?"""">5body?"
The building blocks of XML documents
-een fro a /(/ +oint of vie#& all X)* docuents (and '()* docuents) are ade u+ !y the
follo#in" si+le !uildin" !locks$
.leents
(a"s
:ttri!utes
.ntities
E9/:(:
9/:(:
(he follo#in" is a !rief e0+lanation of each of the !uildin" !locks$
.lements
.leents are the main building blocks of !oth X)* and '()* docuents.
.0a+les of '()* eleents are 5!ody5 and 5ta!le5. .0a+les of X)* eleents could !e 5note5 and
5essa"e5. .leents can contain te0t& other eleents& or !e e+ty. .0a+les of e+ty '()*
eleents are 5hr5& 5!r5 and 5i"5.
Tags
(a"s are used to markup elements.
: startin" ta" like 3eleentInae4 arks u+ the !e"innin" of an eleent& and an endin" ta" like
3?eleentInae4 arks u+ the end of an eleent.
.0a+les$
body element marked (% with body tags<
<body>body text in between</body>#
message element marked (% with message tags<
<message>some message in between</message>
0ttributes
:ttri!utes +rovide e&tra information about elements.
:ttri!utes are al#ays +laced inside the startin" ta" of an eleent. :ttri!utes al#ays coe in
nae?value +airs. (he follo#in" 5i"5 eleent has additional inforation a!out a source file$
<img sr'!'om%(ter#gif! />
(he nae of the eleent is 5i"5. (he nae of the attri!ute is 5src5. (he value of the attri!ute is
5co+uter."if5. -ince the eleent itself is e+ty it is closed !y a 5 ?5.
.ntities
.ntities are varia!les used to define common te&t. .ntity references are references to entities.
)ost of you #ill kno# the '()* entity reference$ 5On!s+P5. (his 5no6!reakin"6s+ace5 entity is used
in '()* to insert an e0tra s+ace in a docuent. .ntities are e0+anded #hen a docuent is +arsed
!y an X)* +arser.
(he follo#in" entities are +redefined in X)*$
.ntity 4eferences Character
OltP 3
O"tP 4
Oa+P O
O8uotP 5
Oa+osP ;

1CD0T0
E9/:(: eans +arsed character data.
(hink of character data as the te0t found !et#een the start ta" and the end ta" of an X)* eleent.
1CD0T0 is te&t that will be parsed by a parser. (a"s inside the te0t #ill !e treated as arku+
and entities #ill !e e0+anded.
CD0T0
9/:(: also eans character data.
CD0T0 is te&t that will 2%T be parsed by a parser. (a"s inside the te0t #ill =O( !e treated as
arku+ and entities #ill not !e e0+anded.
+n a DTD, XML elements are declared with a DTD element declaration"
Declaring an .lement
1n the /(/& X)* eleents are declared #ith an eleent declaration. :n eleent declaration has the
follo#in" synta0$
<!8/8&8AT element+name 'ategory>
or
<!8/8&8AT element+name Delement+'ontentF>
.mpty elements
.+ty eleents are declared #ith the cate"ory key#ord .)E(Y$
<!8/8&8AT element+name 8&@T?>
DTD exam%le<
<!8/8&8AT br 8&@T?>
.&/ exam%le<
<br />
.lements with only character data
.leents #ith only character data are declared #ith NE9/:(: inside +arentheses$
<!8/8&8AT element+name DG@>D;T;F>
exam%le<
<!8/8&8AT from DG@>D;T;F>
.lements with any contents
.leents declared #ith the cate"ory key#ord :=Y& can contain any co!ination of +arsa!le data$
<!8/8&8AT element+name ;A?>
exam%le<
<!8/8&8AT note ;A?>
.lements with children @se3uencesA
.leents #ith one or ore children are defined #ith the nae of the children eleents inside
+arentheses$
<!8/8&8AT element+name
D'hild+element+nameF>
or
<!8/8&8AT element+name
D'hild+element+nameE'hild+element+nameE#####F>
exam%le<
<!8/8&8AT note DtoEfromEheadingEbodyF>
%hen children are declared in a se8uence se+arated !y coas& the children ust a++ear in the
sae se8uence in the docuent. 1n a full declaration& the children ust also !e declared& and the
children can also have children. (he full declaration of the note docuent #ill !e$
<!8/8&8AT note DtoEfromEheadingEbodyF>
<!8/8&8AT to DG@>D;T;F>
<!8/8&8AT from DG@>D;T;F>
<!8/8&8AT heading DG@>D;T;F>
<!8/8&8AT body DG@>D;T;F>
Declaring only one occurrence of the same element
<!8/8&8AT element+name D'hild+nameF>
exam%le<
<!8/8&8AT note DmessageF>
(he e0a+le declaration a!ove declares that the child eleent essa"e can only occur one tie
inside the note eleent.
Declaring minimum one occurrence of the same element
<!8/8&8AT element+name D'hild+nameHF>
exam%le<
<!8/8&8AT note DmessageHF>
(he Q si"n in the e0a+le a!ove declares that the child eleent essa"e ust occur one or ore
ties inside the note eleent.
Declaring Bero or more occurrences of the same element
<!8/8&8AT element+name D'hild+nameIF>
exam%le<
<!8/8&8AT note DmessageIF>
(he R si"n in the e0a+le a!ove declares that the child eleent essa"e can occur 7ero or ore
ties inside the note eleent.
Declaring Bero or one occurrences of the same element
<!8/8&8AT element+name D'hild+name?F>
exam%le<
<!8/8&8AT note Dmessage?F>
(he > si"n in the e0a+le a!ove declares that the child eleent essa"e can occur 7ero or one
ties inside the note eleent.
Declaring either5or content
exam%le<
<!8/8&8AT note DtoEfromEheaderEDmessageJbodyFF>
(he e0a+le a!ove declares that the eleent note ust contain a to eleent& a fro eleent& a
header eleent& and either a essa"e eleent or a !ody eleent.
Declaring mi&ed content
exam%le<
<!8/8&8AT note DG@>D;T;JtoJfromJheaderJmessageFI>
(he e0a+le a!ove declares that the eleent note can contain +arsed character data or any
nu!er of to& fro& header& essa"e eleents.
DTD - 0ttributes
+n a DTD, 0ttributes are declared with an 0TTL+-T declaration"
Declaring 0ttributes
:n attri!ute declaration has the follo#in" synta0$
<!;TT/47T element+name attrib(te+name
attrib(te+ty%e defa(lt+val(e>
exam%le<
DTD exam%le<
<!;TT/47T %ayment ty%e >D;T; !'he'k!>
.&/ exam%le<
<%ayment ty%e!'he'k! />
(he attribute-type can have the follo#in" values$
<alue .&planation
9/:(: (he value is character data
(en1Sen2S..) (he value ust !e one fro an enuerated list
1/ (he value is a uni8ue id
1/R.B (he value is the id of another eleent
1/R.B- (he value is a list of other ids
=)(OT.= (he value is a valid X)* nae
=)(OT.=- (he value is a list of valid X)* naes
.=(1(Y (he value is an entity
.=(1(1.- (he value is a list of entities
=O(:(1O= (he value is a nae of a notation
0l$ (he value is a +redefined 0l value
(he default-alue can have the follo#in" values$
<alue .&planation
value (he attri!utes default value
N/.B:C*( value (he attri!utes default value
NR.UC1R./ (he attri!ute value ust !e included in the eleent
N1)E*1./ (he attri!ute does not have to !e included
NB1X./ value (he attri!ute value is fi0ed
0ttribute declaration e&ample
DTD exam%le<
<!8/8&8AT sK(are 8&@T?>
<!;TT/47T sK(are width >D;T; !$!>
.&/ exam%le<
<sK(are width!"$$! />
1n the e0a+le a!ove& the eleent s8uare is defined to !e an e+ty eleent #ith a #idth attri!ute
of ty+e 9/:(:. 1f no #idth attri!ute is "iven& the #idth attri!ute has a default value of 0.
Default attribute alue
7yntax<
<!;TT/47T element+name attrib(te+name
attrib(te+ty%e !defa(lt+val(e!>
DTD exam%le<
<!;TT/47T %ayment ty%e >D;T; !'he'k!>
.&/ exam%le<
<%ayment ty%e!'he'k! />
-+ecifyin" a default value for an attri!ute ensures that the attri!ute #ill "et a value even if the
author of the X)* docuent didn;t include it.
+mplied attribute
7yntax<
<!;TT/47T element+name attrib(te+name
attrib(te+ty%e G4&@/48D>
DTD exam%le<
<!;TT/47T 'onta't fax >D;T; G4&@/48D>
.&/ exam%le<
<'onta't fax!222+1133,,! />
Cse an i+lied attri!ute if you don;t #ant to force the author to include an attri!ute& and you don;t
have an o+tion for a default value.
4e3uired attribute
7yntax<
<!;TT/47T element+name attrib(teLname
attrib(te+ty%e GR8MN4R8D>
DTD exam%le<
<!;TT/47T %erson n(mber >D;T; GR8MN4R8D>
.&/ exam%le<
<%erson n(mber!2133! />
Cse a re8uired attri!ute if you don;t have an o+tion for a default value& !ut still #ant to force the
attri!ute to !e +resent.
6i&ed attribute alue
7yntax<
<!;TT/47T element+name attrib(te+name
attrib(te+ty%e G-4.8D !val(e!>
DTD exam%le<
<!;TT/47T sender 'om%any >D;T; G-4.8D !&i'rosoft!>
.&/ exam%le<
<sender 'om%any!&i'rosoft! />
Cse a fi0ed attri!ute value #hen you #ant an attri!ute to have a fi0ed value #ithout allo#in" the
author to chan"e it. 1f an author includes another value& the X)* +arser #ill return an error.
.numerated attribute alues
7yntax<
<!;TT/47T element+name
attrib(te+name Den"Jen)J##F defa(lt+val(e>
DTD exam%le<
<!;TT/47T %ayment ty%e D'he'kJ'ashF !'ash!>
.&/ exam%le<
<%ayment ty%e!'he'k! />
or
<%ayment ty%e!'ash! />
Cse enuerated attri!ute values #hen you #ant the attri!ute values to !e one of a fi0ed set of le"al
values.
DTD - .ntities
.ntities are ariables used to define shortcuts to common te&t"
- .ntity references are references to entities"
- .ntities can be declared internal, or e&ternal
+nternal .ntity Declaration
7yntax<
<!8AT4T? entity+name !entity+val(e!>
DTD 8xam%le<
<!8AT4T? writer !Donald D('k#!>
<!8AT4T? 'o%yright !>o%yright karrox#'om#!>
.&/ exam%le<
<a(thor>OwriterPO'o%yrightP</a(thor>

.&ternal .ntity Declaration
7yntax<
<!8AT4T? entity+name 7?7T8& !NR4/NR/!>
DTD 8xam%le<
<!8AT4T? writer
7?7T8& !htt%<//www#karrox#'om/entities/entities#xml!>
<!8AT4T? 'o%yright
7?7T8& !htt%<//www#karrox#'om/entities/entities#dtd!>
.&/ exam%le<
<a(thor>OwriterPO'o%yrightP</a(thor>

3.4 Digging deeper into schea
XML -chema is an XML based alternatie to DTD"
0n XML schema describes the structure of an XML document"
What is an XML -chema?
(he +ur+ose of an X)* -chea is to define the le"al !uildin" !locks of an X)* docuent& 2ust like a
/(/.
:n X)* -chea$
defines eleents that can a++ear in a docuent
defines attri!utes that can a++ear in a docuent
defines #hich eleents are child eleents
defines the se8uence in #hich the child eleents can a++ear
defines the nu!er of child eleents
defines #hether an eleent is e+ty or can include te0t
defines data ty+es for eleents and attri!utes
defines default values for eleents and attri!utes
XML -chemas are the -uccessors of DTDs
%e think that very soon X)* -cheas #ill !e used in ost %e! a++lications as a re+laceent for
/(/s. 'ere are soe reasons$
X)* -cheas are easier to learn than /(/
X)* -cheas are e0tensi!le to future additions
X)* -cheas are richer and ore useful than /(/s
X)* -cheas are #ritten in X)*
X)* -cheas su++ort data ty+es
X)* -cheas su++ort naes+aces
XML -chema - Why?
There are a number of reasons why XML -chema is better than DTD"
XML -chema has -upport for Data Types
One of the "reatest stren"th of X)* -cheas is the su++ort for data ty+es.
%ith the su++ort for data ty+es$
1t is easier to descri!e +erissi!le docuent content
1t is easier to validate the correctness of data
1t is easier to #ork #ith data fro a data!ase
1t is easier to define data facets (restrictions on data)
1t is easier to define data +atterns (data forats)
1t is easier to convert data !et#een different data ty+es
XML -chemas use XML -ynta&
:nother "reat stren"th a!out X)* -cheas is that they are #ritten in X)*.
Because X)* -cheas are #ritten in X)*$
You don;t have to learn another lan"ua"e
You can use your X)* editor to edit your -chea files
You can use your X)* +arser to +arse your -chea files
You can ani+ulate your -chea #ith the X)* /O)
You can transfor your -chea #ith X-*(
XML -chemas -ecure Data Communication
%hen data is sent fro a sender to a receiver it is essential that !oth +arts have the sae
5e0+ectations5 a!out the content.
%ith X)* -cheas& the sender can descri!e the data in a #ay that the receiver #ill understand.
: date like 1AAA60F611 i"ht (in soe countries) !e inter+reted as F.=ove!er or (in soe other
countries) as 11.)arch& !ut an X)* eleent #ith a data ty+e like this$
3date ty+e@5date541AAA60F6113?date4
ensures a utual understandin" of the content !ecause the X)* data ty+e date re8uires the forat
99YY6))6//.
XML -chemas are .&tensible
X)* -cheas are e0tensi!le& 2ust like X)*& !ecause they are #ritten in X)*.
%ith an e0tensi!le -chea definition you can$
Reuse your -chea in other -cheas
9reate your o#n data ty+es derived fro standard ty+es
Reference ulti+le scheas fro the sae docuent
Well-6ormed is not .nough
: #ell6fored X)* docuent is a docuent that confors to the X)* synta0 rules$
ust !e"in #ith the X)* declaration
ust have one uni8ue root eleent
all start ta"s ust atch end6ta"s
X)* ta"s are case sensitive
all eleents ust !e closed
all eleents ust !e +ro+erly nested
all attri!ute values ust !e 8uoted
X)* entities ust !e used for s+ecial characters
.ven if docuents are %ell6Bored they can still contain errors& and those errors can have serious
conse8uences. (hink of this situation$ you order H "ross of laser +rinters& instead of H laser +rinters.
%ith X)* -cheas& ost of these errors can !e cau"ht !y your validatin" soft#are.
XML documents can hae a reference to a DTD or an XML -chema"
0 -imple XML Document
(his si+le X)* docuent (note.0l) is co+ied fro our X)* -chool.
<?xml version!"#$!?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
1n this e0a+le& the note eleent is said to !e a comple& eleent !ecause it contains other
eleents. :ll the other eleents are said to !e simple eleents !ecause they do not contain other
eleents.
0 -imple DTD
(his si+le /(/ file (note.dtd fro /(/ -chool) defines the eleents of the X)* docuent a!ove$
<!8/8&8AT note DtoE fromE headingE bodyF>
<!8/8&8AT to DG@>D;T;F>
<!8/8&8AT from DG@>D;T;F>
<!8/8&8AT heading DG@>D;T;F>
<!8/8&8AT body DG@>D;T;F>
*ine 1 in the /(/ a!ove defines the note eleent as havin" four eleents$ 5to& fro& headin"&
!ody5.
*ine 26H defines the to eleent to !e of the ty+e 5NE9/:(:5& the from eleent to !e of the ty+e
5NE9/:(:5& and so on....
0 -imple XML -chema
(his si+le X)* -chea (note.0sd) also defines the eleents of the X)* file a!ove$
<?xml version!"#$!?>
<xs<s'hema xmlns<xs!htt%<//www#w0#org/)$$"/.&/7'hema!>
<xs<element name!note!>
<xs<'om%lexTy%e>
<xs<seK(en'e>
<element name!to! ty%e!xs<string!/>
<element name!from! ty%e!xs<string!/>
<element name!heading! ty%e!xs<string!/>
<element name!body! ty%e!xs<string!/>
</xs<seK(en'e>
</xs<'om%lexTy%e>
</xs<element>
</xs<s'hema>
(he -chea defines the eleent note to !e a comple& element containin" a se8uence of other
simple elements.
0 4eference to the DTD
(his X)* docuent has a reference to the /(/$
<?xml version!"#$!?>
<!D=>T?@8 note 7?7T8&
!htt%<//www#karrox#'om/dtd/note#dtd!>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
0 4eference to the XML -chema
(his X)* docuent has a reference to the X)* -chea$
<?xml version!"#$!?>
<note xmlns<xsi
!htt%<//www#w0#org/)$$"/.&/7'hema+instan'e!
xsi<noAames%a'e7'hema/o'ation
!htt%<//www#karrox#'om/s'hema/note#xsd!>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML -chemas define the elements of your XML files"
-imple and Comple& .lements
<?xml version!"#$!?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
1n this e0a+le& the note eleent is said to !e a comple& eleent !ecause it contains other
eleents. :ll the other eleents are said to !e simple eleents !ecause they do not contain other
eleents.
Defining -imple .lements
-i+le eleents are defined like this$
7yntax<
<xs<element name!name! ty%e!ty%e!/>
8xam%les<
<xs<element name!to! ty%e!xs<string!/>
<xs<element name!from! ty%e!xs<string!/>
<xs<element name!heading! ty%e!xs<string!/>
<xs<element name!body! ty%e!xs<string!/>
Defining Comple& .lements
9o+le0 eleents are defined like this$
7yntax<
<xs<element name!name!>
<xs<'om%lexTy%e>
#
# element 'ontent
#
</xs<'om%lexTy%e>
</xs<element>
8xam%le<
<xs<element name!note!>
<xs<'om%lexTy%e>
<xs<seK(en'e>
<element name!to! ty%e!xs<string!/>
<element name!from! ty%e!xs<string!/>
<element name!heading! ty%e!xs<string!/>
<element name!body! ty%e!xs<string!/>
</xs<seK(en'e>
</xs<'om%lexTy%e>
</xs<element>
Describing the 2ote Document
(his X)* -chea defines the note docuent$
<?xml version!"#$!?>
<xs<s'hema xmlns<xs!htt%<//www#w0#org/)$$"/.&/7'hema!>
<xs<element name!note!>
<xs<'om%lexTy%e>
<xs<seK(en'e>
<element name!to! ty%e!xs<string!/>
<element name!from! ty%e!xs<string!/>
<element name!heading! ty%e!xs<string!/>
<element name!body! ty%e!xs<string!/>
</xs<seK(en'e>
</xs<'om%lexTy%e>
</xs<element>
</xs<s'hema>
(he -chea a!ove defines note eleent to !e a comple& eleent containin" a se8uence of other
simple eleents.
4eferring other .lements
(his X)* -chea defines e0actly the sae as the one a!ove$
<?xml version!"#$!?>
<xs<s'hema xmlns<xs!htt%<//www#w0#org/)$$"/.&/7'hema!>
<xs<element name!note!>
<xs<'om%lexTy%e>
<xs<seK(en'e>
<xs<element ref!to!/>
<xs<element ref!from!/>
<xs<element ref!heading!/>
<xs<element ref!body!/>
</xs<seK(en'e>
</xs<'om%lexTy%e>
</xs<element>
<xs<element name!to! ty%e!xs<string!/>
<xs<element name!from! ty%e!xs<string!/>
<xs<element name!heading! ty%e!xs<string!/>
<xs<element name!body! ty%e!xs<string!/>
</xs<s'hema>
=ote that the note eleent is referrin" to its su! eleents #ith the ref attri!ute.
XML -chema8 -imple Type .lements
0 simple type element can contain only te&t"
What is a -imple Type .lement?
: si+le ty+e eleent is an X)* eleent that can contain only te0t. 1t cannot contain any other
eleents or attri!utes.
'o#ever& the 5only te0t5 restriction is 8uite isleadin". (he te0t can !e of any different ty+es. 1t
can !e one of the ty+es that are included in the X)* -chea definition& or it can !e of a custo
ty+e that you can define yourself.
You can also add restrictions (facets) to a data ty+e in order to liit its content& and you can re8uire
the data to atch a defined +attern.
$ow to Define a -imple Type .lement
(he synta0 for definin" a si+le ty+e eleent is$
<xs<element name!xxx! ty%e!yyy!/>
#here 000 is the nae of the eleent and yyy is the ty+e.
'ere are soe X)* eleents$
<lastname>Refsnes</lastname>
<age>0Q</age>
<dateborn>"*,,+$0+)3</dateborn>
:nd here are soe corres+ondin" si+le ty+e eleent definitions$
<xs<element name!lastname! ty%e!xs<string!/>
<xs<element name!age! ty%e!xs<n(mber!/>
<xs<element name!dateborn! ty%e!xs<date!/>
Common XML -chema Data Types
X)* -chea has a lot of !uilt6in data ty+es. 'ere is a list of the ost coon ty+es$
0s$strin"
0s$decial
0s$inte"er
0s$!oolean
0s$date
0s$tie
Bor a list of !uilt6in X)* -chea data ty+es& +lease "o to the references cha+ters of -chea -chool.
4estrictions on Type of Content
%hen X)* eleents have defined ty+es& this also +uts a restriction on the eleents content.
1f an X)* eleent of the ty+e 0s$date contains a strin" like 5'ello )other5& the eleent #ill create a
validation error.
%ith X)* -chea definitions& you can add your o#n restrictions to your X)* eleents. (hese
restrictions are called facets. You can read ore a!out facets in the ne0t cha+ter.
XML -chema 6acets
4estrictions on XML elements are called facets"
4estrictions on <alues
(his e0a+le defines an eleent called a"e #ith restriction on values$
<xs<element name!age!>
<xs<sim%leTy%e>
<xs<restri'tion base!xs<integer!>
<xs<min4n'l(sive val(e!"1!/>
<xs<max4n'l(sive val(e!0Q!/>
</xs<restri'tion>
</xs<sim%leTy%e>
</xs<element>
4estrictions on .numerated <alues
-oeties you #ant to liit the content of your X)* eleents to a set of fi0ed values.
(his e0a+le defines an eleent called car$
<xs<element name!'ar!>
<xs<sim%leTy%e>
<xs<restri'tion base!xs<string!>
<xs<en(meration val(e!;(di!/>
<xs<en(meration val(e!&er'edes!/>
<xs<en(meration val(e!Rolvo!/>
</xs<restri'tion>
</xs<sim%leTy%e>
</xs<element>
1n the e0a+le a!ove& the eleent called car is defined to !e of a si+le ty+e #ith a derived
restriction !ased on the X)* -chea data ty+e strin"& and also liited to the set of acce+ta!le
values$ :udi& )ercedes& Volvo.
(he e0a+le could also have !een #ritten like this$.
<xs<element name!'ar! ty%e!'arTy%e!/>
<xs<sim%leTy%e name!'arTy%e!>
<xs<restri'tion base!xs<string!>
<xs<en(meration val(e!;(di!/>
<xs<en(meration val(e!&er'edes!/>
<xs<en(meration val(e!Rolvo!/>
</xs<restri'tion>
</xs<sim%leTy%e>
1n this case the ty+e 5car(y+e5 can !e used !y other eleents !ecause it is not a +art of the 5car5
eleent.
0n XML -chema .&ample
.&ample XML -hipping %rder
Ree!er the 9/ catalo" fro X)* -chool> (his is a 9/ order$
<?xml version!"#$!?>
<shi%=rder>
<shi%To>
<name>Tove 7vendson</name>
<street>Ragnhildvei )</street>
<address>Q$$$ 7tavanger</address>
<'o(ntry>Aorway</'o(ntry>
</shi%To>
<items>
<item>
<title>8m%ire S(rlesK(e</title>
<K(antity>"</K(antity>
<%ri'e>"$#*$</%ri'e>
</item>
<item>
<title>6ide yo(r heart</title>
<K(antity>"</K(antity>
<%ri'e>*#*$</%ri'e>
</item>
</items>
</shi%=rder>
(he order a!ove consists of a root eleent 3shi+Order4& #ith t#o child eleents 3shi+(o4 and
3ites4. (he 3ites4 eleent contains 3ite4 eleents. :n 3ite4 eleent contains
3title4&38uantity4& and 3+rice4 eleents.
.&ample XML -chema
(his is the X)* -chea that defines the a!ove order$
<xs<s'hema xmlns<xs!htt%<//www#w0#org/)$$"/.&/7'hema!>
<xs<element name!shi%=rder! ty%e!order!/>
<xs<'om%lexTy%e name!order!>
<xs<seK(en'e>
<xs<element name!shi%To! ty%e!shi%;ddress!/>
<xs<element name!items! ty%e!'d4tems!/>
</xs<seK(en'e>
</xs<'om%lexTy%e>
<xs<'om%lexTy%e name!shi%;ddress!>
<xs<seK(en'e>
<xs<element name!name! ty%e!xs<string!/>
<xs<element name!street! ty%e!xs<string!/>
<xs<element name!address! ty%e!xs<string!/>
<xs<element name!'o(ntry! ty%e!xs<string!/>
</xs<seK(en'e>
</xs<'om%lexTy%e>
<xs<'om%lexTy%e name!'d4tems!>
<xs<seK(en'e>
<xs<element name!item! ty%e!'d4tem!
max=''(rs!(nbo(nded!/>
</xs<seK(en'e>
</xs<'om%lexTy%e>
<xs<'om%lexTy%e name!'d4tem!>
<xs<seK(en'e>
<xs<element name!title! ty%e!xs<string!/>
<xs<element name!K(antity! ty%e!xs<integer!/>
<xs<element name!%ri'e! ty%e!xs<de'imal!/>
</xs<seK(en'e>
</xs<'om%lexTy%e>
</xs<s'hema>
(he X)* -chea a!ove defines the eleent 3shi+Order4 to !e of the ty+e order. Order is a
co+le0 ty+e eleent consistin" of the eleents 3shi+(o4 and 3ites4. (he 3shi+(o4 eleent is
of the ty+e shi+:ddress 6 a co+le0 ty+e eleent consistin" of the eleents 3nae4& 3street4&
3address4& and 3country4. (he 3ites4 eleent is of the ty+e cd1tes 6 a co+le0 ty+e eleent
consistin" of 3ite4 eleents. (he 3ite4 eleent is of the ty+e cd1te 6 a co+le0 ty+e eleent
consistin" of 3title4& 38uantity4& and 3+rice4 eleents. (he 3title4 eleent is a noral eleent
of the ty+e strin".
!A"
1. 1s this a correct X)* docuent>
3>0l version@51.05>4
3note4
3to a"e@2A4(ove3?to4
3fro4,ani3?fro4
3?note4
a) =o
!)Yes


2. X)* eleents cannot !e e+ty
a) (rue
!) Balse
F. %hich is not a correct nae for an X)* eleent>
a) 31dollar4
c) 3h14
d) :ll F naes are incorrect
<. %hich is not a correct nae for an X)* eleent>
a) 3first nae4
!) 3a"e4
c) 3=:).4
d) :ll F naes are incorrect
H. %hich is not a correct nae for an X)* eleent>
a) 30ldocuent4
!) :ll F naes are incorrect
c) 3+hone nu!er4
d) 37eleven4
G. :ttri!ute values ust al#ays !e enclosed !y dou!le 8uotes
a) Balse
!) (rue
7. (he +ur+ose of a /ocuent (y+e /efinition is to define the le"al !uildin" !locks of an X)*
docuent. 1t defines the docuent structure #ith a list of le"al eleents.

a) (rue
!) false
234ou (an use 'our XML editor to edit 'our *(he#a files
a) (rue
!) false
534ou (an use 'our XML parser to parse 'our *(he#a files
a) (rue
!) false
1634ou (an #anipulate 'our *(he#a &ith the XML +OM
a) (rue
!) false
1134ou (an transfor# 'our *(he#a &ith X*L%
a) (rue
!) false
ChapterCD
Disp#a$ing %# using css
Dispalying Xml with C--
D"! What is C--?
C-- stands for Cascadin" -tyle -heets
-tyles define how to display '()* eleents
-tyles are norally stored in -tyle -heets
-tyles #ere added to '()* <.0 to sole a problem
.&ternal -tyle -heets can save you a lot of #ork
.0ternal -tyle -heets are stored in C-- files
)ulti+le style definitions #ill cascade into one
-tyles -ole a Common 1roblem
'()* ta"s #ere ori"inally desi"ned to define the content of a docuent. (hey #ere su++osed to say
5(his is a header5& 5(his is a +ara"ra+h5& 5(his is a ta!le5& !y usin" ta"s like 3h14& 3+4& 3ta!le4&
and so on. (he layout of the docuent #as su++osed to !e taken care of !y the !ro#ser& #ithout
usin" any forattin" ta"s.
:s the t#o a2or !ro#sers 6 =etsca+e and 1nternet .0+lorer 6 continued to add ne# '()* ta"s and
attri!utes (like the 3font4 ta" and the color attri!ute) to the ori"inal '()* s+ecification& it !ecae
ore and ore difficult to create %e! sites #here the content of '()* docuents #as clearly
se+arated fro the docuent;s +resentation layout.
(o solve this +ro!le& the %orld %ide %e! 9onsortiu (%F9) 6 the non +rofit& standard settin"
consortiu res+onsi!le for standardi7in" '()* 6 created -(Y*.- in addition to '()* <.0.
Both =etsca+e <.0 and 1nternet .0+lorer <.0 su++ort 9ascadin" -tyle -heets.
-tyle -heets Can -ae a Lot of Work
-tyles in '()* <.0 define ho# '()* eleents are dis+layed& 2ust like the font ta" and the color
attri!ute in '()* F.2. -tyles are norally saved in files e0ternal to your '()* docuents. .0ternal
style sheets ena!le you to chan"e the a++earance and layout of all the +a"es in your %e!& 2ust !y
editin" a sin"le 9-- docuent. 1f you have ever tried to chan"e the font or color of all the headin"s
in all your %e! +a"es& you #ill understand ho# 9-- can save you a lot of #ork.
9-- is a !reakthrou"h in %e! desi"n !ecause it allo#s develo+ers to control the style and layout of
ulti+le %e! +a"es all at once. :s a %e! develo+er you can define a style for each '()* eleent
and a++ly it to as any %e! +a"es as you #ant. (o ake a "lo!al chan"e& si+ly chan"e the style&
and all eleents in the %e! are u+dated autoatically.
Multiple -tyles Will Cascade +nto %ne
-tyle -heets allo# style inforation to !e s+ecified in any #ays. -tyles can !e s+ecified inside a
sin"le '()* eleent& inside the 3head4 eleent of an '()* +a"e& or in an e0ternal 9-- file. .ven
ulti+le e0ternal -tyle -heets can !e referenced inside a sin"le '()* docuent.
Cascading %rder
What style will be used when there is more than one style specified for an $TML element?
Menerally s+eakin" #e can say that all the styles #ill 5cascade5 into a ne# 5virtual5 -tyle -heet !y
the follo#in" rules& #here nu!er four has the hi"hest +riority$
1. Bro#ser default
2. .0ternal -tyle -heet
F. 1nternal -tyle -heet (inside the 3head4 ta")
<. 1nline -tyle (inside '()* eleent)
-o& an inline style (inside an '()* eleent) has the hi"hest +riority& #hich eans that it #ill
override every style declared inside the 3head4 ta"& in an e0ternal style sheet& and in a !ro#ser (a
default value).
D"#C-- -ynta&
-ynta&
(he 9-- synta0 is ade u+ of three +arts$ a selector& a +ro+erty and a value$
sele'tor T%ro%erty< val(eU
(he selector is norally the eleent?ta" you #ish to define& the +ro+erty is the attri!ute you #ish
to chan"e& and each +ro+erty can take a value. (he +ro+erty and value are se+arated !y a colon
and surrounded !y curly !races$
body T'olor< bla'kU
1f the value is ulti+le #ords& +ut 8uotes around the value$
% Tfont+family< !sans serif!U
2ote8 1f you #ish to s+ecify ore than one +ro+erty& you should se+arate each +ro+erty #ith a
semi-colon. (he e0a+le !elo# sho#s ho# to define a center ali"ned +ara"ra+h& #ith a red te0t
color$
% Ttext+align< 'enterP 'olor< redU
(o ake the style definitions ore reada!le& you can descri!e one +ro+erty on each line& like this$
@
T
text+align< 'enterP
'olor< bla'kP
font+family< arial
U
7rouping
You can "rou+ selectors. -e+arate each selector #ith a coa. 1n the e0a+le !elo# #e have
"rou+ed all the header eleents. .ach header eleent #ill !e "reen$
h"E h)E h0E hQE h2E h1
T
'olor< green
U
The class 0ttribute
%ith the class attri!ute you can define different styles for the sae eleent. -ay that you #ould
like to have t#o ty+es of +ara"ra+hs in your docuent$ one ri"ht6ali"ned +ara"ra+h& and one
center6ali"ned +ara"ra+h. 'ere is ho# you can do it #ith styles$
%#right Ttext+align< rightU
%#'enter Ttext+align< 'enterU
You have to use the class attri!ute in your '()* docuent$
<% 'lass!right!>
This %aragra%h will be right+aligned#
</%>
<% 'lass!'enter!>
This %aragra%h will be 'enter+aligned#
</%>
You can also omit the ta" nae in the selector to define a style that can !e used !y any
eleents$
#'enter Ttext+align< 'enterU
1n the code !elo# both the h1 eleent and the + eleent are classed #ith 5center5. (his eans
that !oth of the eleents #ill follo# the rules in the 5.center5 selector$
<h" 'lass!'enter!>
This heading will be 'enter+aligned
</h">
<% 'lass!'enter!>
This %aragra%h will also be 'enter+aligned#
</%>
The id 0ttribute
(he id attri!ute has to !e uni3ue on the +a"e. (here can only !e one eleent #ith a "iven id in a
docuent.
(hey are arked in the '()* docuent #ith id instead of class$
<% id!intro!>
This %aragra%h will be right+aligned#
</%>
(he id attri!ute can !e defined in t#o #ays. 1t can !e defined to atch all eleents #ith a
+articular id& or to atch only one eleent #ith a +articular id.
1n this e0a+le the id attri!ute #ill atch all eleents #ith id@5intro5$
Gintro
T
font+siVe<""$WP
font+weight<boldP
'olor<G$$$$ffP
ba'kgro(nd+'olor<trans%arent
U
1n this e0a+le the id attri!ute #ill atch only + eleents #ith id@5intro5$
%Gintro
T
font+siVe<""$WP
font+weight<boldP
'olor<G$$$$ffP
ba'kgro(nd+'olor<trans%arent
U
C-- Comments
You can insert coents in 9-- to e0+lain your code& #hich can hel+ you #hen you edit the source
code at a later date. : coent #ill !e i"nored !y the !ro#ser. : 9-- coent !e"ins #ith 5?R5&
and ends #ith 5R?5& like this$
/I This is a 'omment I/
%
T
text+align< 'enterP
/I This is another 'omment I/
'olor< bla'kP
font+family< arial
U
D") C-- $ow To"""
$ow to +nsert a -tyle -heet
%hen a !ro#ser reads a style sheet& it #ill forat the docuent accordin" to it. (here are three
#ays of insertin" a style sheet$
.&ternal -tyle -heet
:n e0ternal style sheet is ideal #hen the style is a++lied to any +a"es. %ith an e0ternal style
sheet& you can chan"e the look of an entire %e! site !y chan"in" one file. .ach +a"e ust link to
the style sheet usin" the 3link4 ta". (he 3link4 ta" "oes inside the head section$
<head>
<link rel!stylesheet! ty%e!text/'ss!
href!mystyle#'ss! />
</head>
(he !ro#ser #ill read the style definitions fro the file ystyle.css& and forat the docuent
accordin" to it.
:n e0ternal style sheet can !e #ritten in any te0t editor. (he file should not contain any htl ta"s.
Your style sheet should !e saved #ith a .css e0tension. :n e0a+le of a style sheet file is sho#n
!elo#$
hr T'olor< siennaU
% Tmargin+left< )$%xU
body Tba'kgro(nd+image< (rlD!images/ba'kQ$#gif!FU
+nternal -tyle -heet
:n internal style sheet should !e used #hen a sin"le docuent has a uni8ue style. You define
internal styles in the head section !y usin" the 3style4 ta"& like this$
<head>
<style ty%e!text/'ss!>
hr T'olor< siennaU
% Tmargin+left< )$%xU
body Tba'kgro(nd+image< (rlD!images/ba'kQ$#gif!FU
</style>
</head>
(he !ro#ser #ill no# read the style definitions& and forat the docuent accordin" to it.
2ote8 : !ro#ser norally i"nores unkno#n ta"s. (his eans that an old !ro#ser that does not
su++ort styles& #ill i"nore the 3style4 ta"& !ut the content of the 3style4 ta" #ill !e dis+layed on
the +a"e. 1t is +ossi!le to +revent an old !ro#ser fro dis+layin" the content !y hidin" it in the
'()* coent eleent$
<head>
<style ty%e!text/'ss!>
<!++
hr T'olor< siennaU
% Tmargin+left< )$%xU
body Tba'kgro(nd+image< (rlD!images/ba'kQ$#gif!FU
++>
</style>
</head>
+nline -tyles
:n inline style loses any of the advanta"es of style sheets !y i0in" content #ith +resentation.
Cse this ethod s+arin"ly& such as #hen a style is to !e a++lied to a sin"le occurrence of an
eleent.
(o use inline styles you use the style attri!ute in the relevant ta". (he style attri!ute can contain
any 9-- +ro+erty. (he e0a+le sho#s ho# to chan"e the color and the left ar"in of a +ara"ra+h$
<% style!'olor< siennaP margin+left< )$%x!>
This is a %aragra%h
</%>
Multiple -tyle -heets
1f soe +ro+erties have !een set for the sae selector in different style sheets& the values #ill !e
inherited fro the ore s+ecific style sheet.
Bor e0a+le& an e0ternal style sheet has these +ro+erties for the hF selector$
h0
T
'olor< redP
text+align< leftP
font+siVe< ,%t
U
:nd an internal style sheet has these +ro+erties for the hF selector$
h0
T
text+align< rightP
font+siVe< )$%t
U
1f the +a"e #ith the internal style sheet also links to the e0ternal style sheet the +ro+erties for hF
#ill !e$
'olor< redP
text+align< rightP
font+siVe< )$%t
(he color is inherited fro the e0ternal style sheet and the te0t6ali"nent and the font6si7e is
re+laced !y the internal style sheet.
See the following example
Example 4.1
<html>
<head>
<link rel="stylesheet"
type="text/css" href="ex1.css" />
</head>
<ody>
<h1>!his header is "# pt</h1>
<h$>!his header is l%e</h$>
<p>!his paragraph has a left
margin of &' pixels</p>
</ody>
</html>
!his is the style sheet (le )ex1.css*+

9ss to set !ack"round
.0a+le <.2
3htl4
3head4
3style ty+e@5te0t?css54
!ody V!ack"round6color$ yello#W
h1 V!ack"round6color$ N00ff00W
h2 V!ack"round6color$ trans+arentW
+ V!ack"round6color$ r"!(2H0&0&2HH)W
3?style4
3?head4
3!ody4
3h14(his is header 13?h14
3h24(his is header 23?h24
3+4(his is a +ara"ra+h3?+4
3?!ody4
3?htl4
.0a+le <.F
css to set te0t color
3htl4
3head4
3style ty+e@5te0t?css54
h1 Vcolor$ N00ff00W
h2 Vcolor$ Ndda0ddW
+ Vcolor$ r"!(0&0&2HH)W
3?style4
3?head4
3!ody4
3h14(his is header 13?h14
3h24(his is header 23?h24
3+4(his is a +ara"ra+h3?+4
3?!ody4
3?htl4
.0a+le <.<
css to set Bonts si7e
3htl4
3head4
3style ty+e@5te0t?css54
hF Vfont6faily$ tiesW
+ Vfont6faily$ courierW
+.sansserif Vfont6faily$ 5sans6serif5W
3?style4
3?head4
3!ody4
3hF4(his is header F3?hF4
3+4
(his is a +ara"ra+h3?+4
3+ class@5sansserif54
(his is a +ara"ra+h3?+4
3?!ody4
3?htl4
.0a+le <.H
css to set !order
3htl4
3head4
3style ty+e@5te0t?css54
+.dotted V!order6style$ dottedW
+.dashed V!order6style$ dashedW
+.solid V!order6style$ solidW
+.dou!le V!order6style$ dou!leW
+."roove V!order6style$ "rooveW
+.rid"e V!order6style$ rid"eW
+.inset V!order6style$ insetW
+.outset V!order6style$ outsetW
3?style4
3?head4
3!ody4
3+4(he 5!order6style5 +ro+erty is not reco"ni7ed !y =etsca+e <.3?+4
3+4=etsca+e G su++orts all !order styles.3?+4
3+41nternet .0+lorer H.H su++orts all !order styles. 1nternet .0+lorer <.0 6 H.0 does not su++ort
the 5dotted5 and 5dashed5 values3?+4
3+ class@5dotted54
: dotted !order3?+4
3!r4
3+ class@5dashed54
: dashed !order3?+4
3!r4
3+ class@5solid54
: solid !order3?+4
3!r4
3+ class@5dou!le54
: dou!le !order3?+4
3!r4
3+ class@5"roove54
: "roove !order3?+4
3!r4
3+ class@5rid"e54
: rid"e !order3?+4
3!r4
3+ class@5inset54
:n inset !order3?+4
3!r4
3+ class@5outset54
:n outset !order3?+4
3?!ody4
3?htl4
.0a+le <.G
css to set ar"in
3htl4
3head4
3style ty+e@5te0t?css54
+.ar"in Var"in6left$ 2cW
3?style4
3?head4
3!ody4
3+4
(his is a +ara"ra+h (his is a +ara"ra+h
(his is a +ara"ra+h (his is a +ara"ra+h
3?+4
3+ class@5ar"in54
(his is a +ara"ra+h #ith a left ar"in
(his is a +ara"ra+h #ith a left ar"in
3?+4
3?!ody4
3?htl4
.0a+le <.7
css to set +addin"
3htl4
3head4
3style ty+e@5te0t?css54
td V+addin"6left$ 2cW
3?style4
3?head4
3!ody4
3ta!le !order@5154
3tr4
3td4
(his is a ta!lecell #ith a left +addin"
3?td4
3?tr4
3?ta!le4
3?!ody4
3?htl4
.0a+le <.X
css to set list order
3htl4
3head4
3style ty+e@5te0t?css54
ul.disc
V
list6style6ty+e$ disc
W
ul.circle
V
list6style6ty+e$ circle
W
ul.s8uare
V
list6style6ty+e$ s8uare
W
3?style4
3?head4
3!ody4
3ul class@5disc54
3li49offee3?li4
3li4(ea3?li4
3li49oca 9ola3?li4
3?ul4
3ul class@5circle54
3li49offee3?li4
3li4(ea3?li4
3li49oca 9ola3?li4
3?ul4
3ul class@5s8uare54
3li49offee3?li4
3li4(ea3?li4
3li49oca 9ola3?li4
3?ul4
3?!ody4
3?htl4
.0a+le <.A
last listin" sho#s ho# to use css to dis+alay 0l usin" css
3>0l version@51.05>4
39:(:*OM4
3(1(*.4Mreatest 'its3?(1(*4
3:R(1-(4/olly Earton3?:R(1-(4
39OC=(RY4C-:3?9OC=(RY4
39O)E:=Y4R9:3?9O)E:=Y4
3ER19.4A.A03?ER19.4
3Y.:R41AX23?Y.:R4
3?9/4
39/4
3(1(*.4-till "ot the !lues3?(1(*.4
3:R(1-(4Mary )oore3?:R(1-(4
39OC=(RY4CT3?9OC=(RY4
39O)E:=Y4Vir"in records3?9O)E:=Y4
3ER19.410.203?ER19.4
3Y.:R41AA03?Y.:R4
3?9/4
39/4
3(1(*.4Brid"e of -+ies3?(1(*.4
3:R(1-(4(;Eau3?:R(1-(4
39OC=(RY4CT3?9OC=(RY4
39O)E:=Y4-iren3?9O)E:=Y4
3ER19.47.A03?ER19.4
3Y.:R41AX73?Y.:R4
3?9/4
39/4
3(1(*.4Erivate /ancer3?(1(*.4
3:R(1-(4(ina (urner3?:R(1-(4
39OC=(RY4CT3?9OC=(RY4
39O)E:=Y49a+itol3?9O)E:=Y4
3ER19.4X.A03?ER19.4
3Y.:R41AXF3?Y.:R4
3?9/4
3(1(*.4(he dock of the !ay3?(1(*.4
3:R(1-(4Otis Reddin"3?:R(1-(4
39OC=(RY4C-:3?9OC=(RY4
39O)E:=Y4:tlantic3?9O)E:=Y4
3ER19.47.A03?ER19.4
3Y.:R41AX73?Y.:R4
3?9/4
39/4
3(1(*.4Eicture !ook3?(1(*.4
3:R(1-(4-i+ly Red3?:R(1-(4
39OC=(RY4.C3?9OC=(RY4
39O)E:=Y4.lektra3?9O)E:=Y4
3ER19.47.203?ER19.4
3Y.:R41AXH3?Y.:R4
3?9/4
3?9:(:*OM4
save this file as cdcatalo".0l
>;T;/=9
T
ba'kgro(nd+'olor< GffffffP
width< "$$WP
U
>D
T
dis%lay< blo'kP
margin+bottom< 0$%tP
margin+left< $P
U
T4T/8
T
'olor< G--$$$$P
font+siVe< )$%tP
U
;RT47T
T
'olor< G$$$$--P
font+siVe< )$%tP
U
>=NATR?E@R4>8E?8;RE>=&@;A?
T
Dis%lay< blo'kP
'olor< G$$$$$$P
margin+left< )$%tP
U
sa,e this as cd-catalog.css
Binally& vie#$ (he 9/ 9atalo" foratted #ith the 9-- file
3>0l version@51.05>4
3>0l6stylesheet ty+e@5te0t?css5 href@5cdIcatalo".css5>4
3>0l version@51.05>4
39:(:*OM4
3(1(*.4Mreatest 'its3?(1(*4
3:R(1-(4/olly Earton3?:R(1-(4
39OC=(RY4C-:3?9OC=(RY4
39O)E:=Y4R9:3?9O)E:=Y4
3ER19.4A.A03?ER19.4
3Y.:R41AX23?Y.:R4
3?9/4
39/4
3(1(*.4-till "ot the !lues3?(1(*.4
3:R(1-(4Mary )oore3?:R(1-(4
39OC=(RY4CT3?9OC=(RY4
39O)E:=Y4Vir"in records3?9O)E:=Y4
3ER19.410.203?ER19.4
3Y.:R41AA03?Y.:R4
3?9/4
39/4
3(1(*.4Brid"e of -+ies3?(1(*.4
3:R(1-(4(;Eau3?:R(1-(4
39OC=(RY4CT3?9OC=(RY4
39O)E:=Y4-iren3?9O)E:=Y4
3ER19.47.A03?ER19.4
3Y.:R41AX73?Y.:R4
3?9/4
39/4
3(1(*.4Erivate /ancer3?(1(*.4
3:R(1-(4(ina (urner3?:R(1-(4
39OC=(RY4CT3?9OC=(RY4
39O)E:=Y49a+itol3?9O)E:=Y4
3ER19.4X.A03?ER19.4
3Y.:R41AXF3?Y.:R4
3?9/4
3(1(*.4(he dock of the !ay3?(1(*.4
3:R(1-(4Otis Reddin"3?:R(1-(4
39OC=(RY4C-:3?9OC=(RY4
39O)E:=Y4:tlantic3?9O)E:=Y4
3ER19.47.A03?ER19.4
3Y.:R41AX73?Y.:R4
3?9/4
39/4
3(1(*.4Eicture !ook3?(1(*.4
3:R(1-(4-i+ly Red3?:R(1-(4
39OC=(RY4.C3?9OC=(RY4
39O)E:=Y4.lektra3?9O)E:=Y4
3ER19.47.203?ER19.4
3Y.:R41AXH3?Y.:R4
3?9/4
3?9:(:*OM4

save this file cdIcata#ith9--.0l
./0
1. 1here in the 2!34 doc%ment is the correct place to refer to an
external style sheet5
a* /t the top of the doc%ment
* 6n the <ody> section
c*6n the <head> section
d* /t the end of the doc%ment
$. 1hich 2!34 tag is %sed to de(ne an internal style sheet5
a* <style>
* <script>
c* <css>

". 1hat is the correct 2!34 for refering to an external style sheet5
a*<stylesheet>mystyle.css</stylesheet>
*<link rel="stylesheet" type="text/css" href="mystyle.css">
c*<style src="mystyle.css">
4. 1hich 2!34 attri%te is %sed to de(ne inline styles5
a* styles
* font
c* class
d* style
& 1hich is the correct 7SS syntax5
a* ody+color=lack
* 8ody9color+lack:
c* ody 8color+ lack:
d* 8ody+color=lack)ody:
# 2ow to insert a comment in yo%r 7SS (le5
a* /; this is a comment ;/
* // this is a comment
c* // this is a comment //
d* < this is a comment
=. 1hich property is %sed to change the ackgro%nd color5
a*color+
* ackgro%nd>color+
c* gcolor+
?. 2ow to add a ackgro%nd color for all yo%r <h1> elements5
a* all.h1 8ackgro%nd>color+@......:
* h1.all 8ackgro%nd>color+@......:
c* h1 8ackgro%nd>color+@......:
A 2ow to change text color of an element5
a* text>color=
* fgcolor+
c* color+
d* text>color+
1'*1hat is the correct syntax of the declaration which de(nes
the B34 ,ersion5
a* <xml ,ersion="1.'" />
* <5xml ,ersion="1.'" />
c* <5xml ,ersion="1.'"5>

Chapter 5
X+L
X+L
,.1 %ntroduction to X+L
X-L, the XML style sheet language is far more sophisticated than C--"
C-- - The -tyle -heet of $TML
Because '()* uses predefined tags& the eanin"s of these ta"s are well understood$ (he >+?
eleent defines a +ara"ra+h and the >h1? eleent defines a headin"& and the !ro#ser kno#s how
to display these eleents.
:ddin" dis+lay style characteristics to '()* eleents #ith 9-- is a si+le +rocess. (ellin" the
!ro#ser to dis+lay each eleent usin" a s+ecial font or color& is easy to do and easy for a !ro#ser
to understand.
X-L - The -tyle -heet of XML
Because X)* does not use predefined tags (#e can use any ta"s #e #ant)& the eanin"s of
these ta"s are not understood$ 3ta!le4 could ean an '()* ta!le or ay!e a +iece of furniture.
Because of the nature of X)*& the !ro#ser does not know how to display an X)* docuent.
1n order to dis+lay X)* docuents& it is necessary to have a echanis to descri!e ho# the
docuent should !e dis+layed. One of these echaniss is 9--& !ut X-* (the eXtensi!le
-tylesheet Lan"ua"e) is the +referred style sheet lan"ua"e of X)*& and X-* is far ore
so+histicated than the 9-- used !y '()*.
X-L - More than a -tyle -heet
X-* consists of three +arts$
a ethod for transforming XML documents
a ethod for defining XML parts and patterns
a ethod for formatting XML documents
1f you don;t understand the eanin" of this& think of X-* as a lan"ua"e that can transform X)*
into '()*& a lan"ua"e that can filter and sort X)* data& a lan"ua"e that can address parts of an
X)* docuent& a lan"ua"e that can format X)* data !ased on the data value& like dis+layin"
ne"ative nu!ers in red& and a lan"ua"e that can output X)* data to different devices& like screen&
+a+er or voice.
X-L actually consists of three languages" The most important is X-LT"
X-L is Three Languages
X-* actually consists of three lan"ua"es$
X-*( is a lan"ua"e to transform XML
XEath is a lan"ua"e to define XML parts or patterns
X-* Borattin" O!2ects is a lan"ua"e to define XML display
X-*( is a lan"ua"e for transforin" X)* docuents into other ty+es of docuents& or into other
X)* docuents.
XEath is a lan"ua"e for addressin" +arts of an X)* docuent. XEath #as desi"ned to !e used !y
X-*(.
Borattin" is the +rocess of turnin" the result of an X-* transforation into a suita!le out+ut for
for a reader or listener.
X-*( and XEath #ere released as t#o se+arate %F9 Recoendations 1G. =ove!er 1AAA. =o
se+arate %F9 docuent e0ists for X-* Borattin" O!2ects& !ut a descri+tion can !e found inside
the X-* 1.0 Recoendation.
You can read ore a!out the %F9 X-* activities in our %F9 X-* section.
X-LT - X-L Transformations
X-*( is the ost i+ortant +art of the X-* -tandard. 1t is the +art of X-* that is used to transfor
an X)* docuent into another X)* docuent& or another ty+e of docuent.
X-*( can !e used to transfor an X)* docuent into a forat that is reco"ni7a!le to a !ro#ser.
One such forat is '()*. =orally X-*( does this !y transforin" each X)* eleent into an '()*
eleent.
X-*( can also add co+letely ne# eleents into the out+ut file& or reove eleents. 1t can
rearran"e and sort the eleents& and test and ake decisions a!out #hich eleents to dis+lay& and
a lot ore.
: coon #ay to descri!e the transforation +rocess is to say that X-* uses X-*( to transfor an
X)* source tree into an X)* result tree (or an X)* source document into an X)* result
document)
$ow does it work?
1n the transforation +rocess& X-*( uses XEath to define +arts of the source docuent that match
one or ore +redefined templates. %hen a atch is found& X-*( #ill transform the atchin" +art
of the source docuent into the result docuent. (he +arts of the source docuent that do not
atch a te+late #ill (as a "eneral rule) end u+ unodified in the result.
This chapter will focus on X-LT and X1ath
)ost of the cha+ters in this tutorial #ill focus on X-*( and XEath. %e #ill use X-*( to define X)*
transforations and XEath to define the atchin" +atterns for the transforations.
.ven thou"h X-* consists of three different +arts #ith three different naes& #e #ill use the "eneral
ter X-* in this tutorial.
E"#X-L /rowsers
6ew +nternet browsers hae full support for X-L at the moment"
We will use +nternet .&plorer to demonstrate X-L in this tutorial"
We will use +nternet .&plorer
1n order to +rocess an X)* docuent usin" X-*& you #ill need an X)* +arser #ith an X-* .n"ine.
-ince 1nternet .0+lorer is the ost coonly used !ro#ser on the arket today& #e #ill use it to
deonstrate X-* in this tutorial.
+nternet .&plorer E is /ad
X-* in 1nternet .0+lorer H is not co+ati!le #ith the official %F9 X-* Recoendation.
%hen 1nternet .0+lorer H.0 #as released in )arch 1AAA& the X-* standard #as still a %F9 %orkin"
/raft.
-ince the final %F9 X-* Recoendation is different fro the %orkin" /raft& the su++ort for X-* in
1. H is not 100Y co+ati!le #ith the official X-* standard.
(his restriction a++lies for !oth 1nternet .0+lorer H.0 and H.H.
9lick here to read ore a!out the X-* activities at the %F9.
+nternet .&plorer F is /etter
1nternet .0+lorer G fully su++orts the official %F9 X-* Recoendation.
(he X)* Earser F.0 6 !uilt onto 1nternet .0+lorer G.0 and %indo#s XE 6 is !ased on !oth the %F9
X-*( 1.0 and the %F9 XEath 1.0 Recoendations.
:t the oent& the e0a+le code in this tutorial #ill only #ork in 1nternet .0+lorer H or later. )any
of the e0a+les #ill only #ork in 1nternet .0+lorer G.0.
1f you are serious a!out learnin" X-* you should u+"rade to 1nternet .0+lorer G.0.
E") X-L - Transformation @X-LTA
Correct -tyle -heet Declaration
(his is the correct #ay to declare an X-* style sheet accordin" to the %F9 X-* Recoendation$
<xsl<stylesheet version!"#$!
xmlns<xsl!htt%<//www#w0#org/"***/.7//Transform!>
1f you are usin" 1nternet .0+lorer G.0 you should use the code a!ove.
+ncorrect -tyle -heet Declaration
(his is the correct #ay to declare an X-* style sheet accordin" to the (outdated) %F9 X-* %orkin"
/raft$
<xsl<stylesheet
xmlns<xsl!htt%<//www#w0#org/TR/5D+xsl!>
1f you are usin" 1nternet .0+lorer H.0 you should use the (incorrect) code a!ove.
.&ample study8 $ow to transform XML into $TML using X-L"
The details of this e&ample will be e&plained in the ne&t chapter"
-tart with your XML Document
Birst you start #ith the X)* docuent that you #ant to transform into '()*$
<?xml version!"#$!?>
<'atalog>
<'d>
<title>8m%ire S(rlesK(e</title>
<artist>Sob Dylan</artist>
<'o(ntry>N7;</'o(ntry>
<'om%any>>ol(mbia</'om%any>
<%ri'e>"$#*$</%ri'e>
<year>"*,2</year>
</'d>
#
#
#
</'atalog>
Create an X-L -tyle -heet Document
(hen you create an X-* -tyle -heet #ith a transforation template8
<?xml version!"#$!?>
<xsl<stylesheet version!"#$!
xmlns<xsl!htt%<//www#w0#org/"***/.7//Transform!>
<xsl<tem%late mat'h!/!>
<html>
<body>
<table border!)! bg'olor!yellow!>
<tr>
<th>Title</th>
<th>;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>
Link the -tyle -heet to the XML document
(hen you add an X-* -tyle -heet reference to your X)* docuent$
<?xml version!"#$!?>
<?xml+stylesheet ty%e!text/xsl! href!'d'atalog#xsl!?>
<'atalog>
<'d>
<title>8m%ire S(rlesK(e</title>
<artist>Sob Dylan</artist>
<'o(ntry>N7;</'o(ntry>
<'om%any>>ol(mbia</'om%any>
<%ri'e>"$#*$</%ri'e>
<year>"*,2</year>
</'d>
#
#
#
</'atalog>
1f you have an X-* co+liant !ro#ser& like 1nternet .0+lorer H.0 or later& your !ro#ser #ill nicely
transform your X)* into '()*.
E"D X-L - Templates
C-- uses 4ules
1f you have studied our 9-- -chool& you #ill kno# that 9-- uses one or ore rules to define the
out+ut of '()* eleents. : selector is used to associate the rule #ith an '()* element.
(he + selector in this 9-- rule tells that a 3+4 eleent should !e dis+layed usin" a font naed
arial$
% T font+family< arial U
X-L uses Templates
X-* uses one or ore templates to define ho# to transform X)* eleents.
: match attribute is used to associate the te+late #ith an XML element. (he atch attri!ute
can also !e used to define a te+late for a #hole !ranch of the X)* docuent& like #hen #e use
atch@5?5 to define the #hole docuent.
*ook at the follo#in" X-L -tyle -heet that contains a template to out+ut the X)* 9/ 9atalo"
fro the +revious cha+ter$
<?xml version!"#$!?>
<xsl<stylesheet version!"#$!
xmlns<xsl!htt%<//www#w0#org/"***/.7//Transform!>
<xsl<tem%late mat'h!/!>
<html>
<body>
<table border!"!>
<tr>
<th>Title</th>
<th>;rtist</th>
</tr>
<tr>
<td>#</td>
<td>#</td>
</tr>
</table>
</body>
</html>
</xsl<tem%late>
</xsl<stylesheet>
-ince the style sheet is an X)* docuent itself& the docuent !e"ins #ith an 0l declaration$ >?
&ml ersionG;!"H;??
(he &sl8stylesheet ta" in the second line defines the start of the stylesheet.
(he &sl8template ta" in the third line defines the start of a te+late. (he te+late attri!ute
matchG(5( associates (atches) the te+late to the root (?) of the X)* source docuent.
(he rest of the docuent contains the te+late itself& e0ce+t for the last t#o lines that defines the
end of the te+late and the end of the style sheet.
(he result of the transforation #ill look (a little disa++ointin") like this$
Title 0rtist
. .
The >&sl8alue-of? .lement
(he result fro the +revious sa+le #as a little disa++ointin"& !ecause no data #as co+ied fro the
X)* docuent to the out+ut.
(he X-* >&sl8alue-of? eleent can !e used to select X)* eleents into the out+ut strea of
the X-* transforation$
<?xml version!"#$!?>
<xsl<stylesheet version!"#$!
xmlns<xsl!htt%<//www#w0#org/"***/.7//Transform!>
<xsl<tem%late mat'h!/!>
<html>
<body>
<table border!"!>
<tr>
<th>Title</th>
<th>;rtist</th>
</tr>
<tr>
<td><xsl<val(e+of sele't!'atalog/'d/title!/></td>
<td><xsl<val(e+of sele't!'atalog/'d/artist!/></td>
</tr>
</table>
</body>
</html>
</xsl<tem%late>
</xsl<stylesheet>
2ote8 the synta0 for the select attri!ute value is called an X-L 1attern. 1t #orks like navi"atin" a
file syste #here a for#ard slash (?) selects su!directories.
(he result of the transforation #ill look like this$
Title 0rtist
.+ire Burles8ue Bo! /ylan
(he result fro this e0a+le #as also a little disa++ointin"& !ecause only one line of data #as
co+ied fro the X)* docuent to the out+ut.
1n the ne0t cha+ter you #ill learn ho# to use the >&sl8for-each? eleent to select every X)*
eleent into the out+ut strea of the transforation.
X-L 6or .ach
The >&sl8for-each? is used to select all elements into the output stream"
The >&sl8for-each? .lement
(he result fro the +revious e0a+le #as a little disa++ointin"& !ecause only one line of data #as
co+ied fro the X)* docuent to the out+ut.
(he X-* >&sl8for-each? eleent can !e used to select every X)* eleent into the out+ut strea
of the X-* transforation$
<?xml version!"#$!?>
<xsl<stylesheet version!"#$!
xmlns<xsl!htt%<//www#w0#org/"***/.7//Transform!>
<xsl<tem%late mat'h!/!>
<html>
<body>
<table border!"!>
<tr>
<th>Title</th>
<th>;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>
(he &sl8for-each eleent locates eleents in the X)* docuent and re+eats a +art of the
te+late for each one.
The 4esult
(he result of the transforation #ill look like this$
Title 0rtist
.+ire Burles8ue Bo! /ylan
'ide your heart Bonnie (yler
Mreatest 'its /olly Earton
-till "ot the !lues Mary )ore
.ros .ros Raa77otti
One ni"ht only Bee Mees
-ylvias )other /r.'ook
)a""ie )ay Rod -te#art
Roan7a :ndrea Bocelli
%hen a an loves a #oan Eercy -led"e
Black an"el -ava"e Rose
1AAA Mray =oinees )any
Bor the "ood ties Tenny Ro"ers
Bi" %illie style %ill -ith
(u+elo 'oney Van )orrison
-oulsville ,orn 'oel
(he very !est of 9at -tevens
-to+ -a Bro#n
Brid"e of -+ies (ZEau
Erivate /ancer (ina (urner
)idt o natten Ti *arsen
Eavarotti Mala 9oncert *uciano Eavarotti
(he dock of the !ay Otis Reddin"
Eicture !ook -i+ly Red
Red (he 9ounards
Cnchain y heart ,oe 9ocker
E"E X-L - %n the Client
+f your browser supports XML, X-L can be used to transform the document to $TML in
your browser"
0 Iaa-cript -olution
1n the +revious cha+ter #e e0+lained ho# X-* can !e used to transfor a docuent fro X)* to
'()*. (he trick #as to add an X-* stylesheet to the X)* file& and to let the !ro#ser do the
transforation.
.ven if this #orks fine& it is not al#ays desira!le to include a stylesheet reference in the X)* file&
and the solution #ill not #ork in a non X-* a#are !ro#ser.
: ore versatile solution #ould !e to use a ,ava-cri+t to do the X)* to '()* transforation.
By usin" ,ava-cri+t #e are ore o+en for these +ossi!ilities$
:llo#in" the ,ava-cri+t to do !ro#ser s+ecific testin"
Csin" different style sheets accordin" to !ro#ser and?or user needs
(hat;s the !eauty of X-*. One of the desi"n "oals for X-* #as to ake it +ossi!le to transfor data
fro one forat to another& su++ortin" different !ro#sers and different user needs.
X-* transforation on the client side is !ound to !e a a2or +art of the !ro#sers #ork tasks in the
future& as #e #ill see a "ro#th in the s+eciali7ed !ro#ser arket (think$ Braille& -+eakin" %e!& %e!
Erinters& 'andheld E9s& )o!ile Ehones .....).
The XML file and the X-L file
(ake a ne# look at the X)* docuent that you sa# in the +revious cha+ter$
<?xml version!"#$!?>
<'atalog>
<'d>
<title>8m%ire S(rlesK(e</title>
<artist>Sob Dylan</artist>
<'o(ntry>N7;</'o(ntry>
<'om%any>>ol(mbia</'om%any>
<%ri'e>"$#*$</%ri'e>
<year>"*,2</year>
</'d>
#
#
#
</'atalog>
:nd the acco+anyin" X-* stylesheet$
<?xml version!"#$!?>
<xsl<stylesheet version!"#$!
xmlns<xsl!htt%<//www#w0#org/"***/.7//Transform!>
<xsl<tem%late mat'h!/!>
<html>
<body>
<table border!)! bg'olor!yellow!>
<tr>
<th>Title</th>
<th>;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>
(he synta0 of the a!ove X-* docuent #as e0+lained in the +revious cha+ter& so it #ill not !e
e0+lained here. But !e sure to notice that the X)* file does not have a reference to the X-* file& and
the X-* file does not have a reference to the X)* file.
1)EOR(:=($ (he a!ove sentence indicates that an X)* file could !e transfored usin" any
different X-* files.
Transforming XML to $TML in your /rowser
'ere is the si+le source code needed to transfor the X)* file to '()* on the client$
<html>
<body>
<s'ri%t ty%e!text/Xavas'ri%t!>
// /oad .&/
var xml new ;'tive.=bXe'tD!&i'rosoft#.&/D=&!F
xml#asyn' false
xml#loadD!'d'atalog#xml!F
// /oad the .7/
var xsl new ;'tive.=bXe'tD!&i'rosoft#.&/D=&!F
xsl#asyn' false
xsl#loadD!'d'atalog#xsl!F
// Transform
do'(ment#writeDxml#transformAodeDxslFF
</s'ri%t>
</body>
</html>
(he first !lock of code creates an instance of the )icrosoft X)* +arser (X)*/O))& and loads the
X)* docuent into eory. (he second !lock of code creates another instance of the +arser and
loads the X-* docuent into eory. (he last line of code transfors the X)* docuent usin" the
X-* docuent& and #rites the result to the '()* docuent.
=ice and si+le.
E"FX-L -ort
X-L can be used to sort an XML document"
Where to put the -ort +nformation
(ake a ne# look at the X)* docuent that you have seen in any cha+ters$
<?xml version!"#$!?>
<'atalog>
<'d>
<title>8m%ire S(rlesK(e</title>
<artist>Sob Dylan</artist>
<'o(ntry>N7;</'o(ntry>
<'om%any>>ol(mbia</'om%any>
<%ri'e>"$#*$</%ri'e>
<year>"*,2</year>
</'d>
#
#
#
</'atalog>
(o out+ut this X)* file as an ordinary '()* file& and sort it at the sae tie& si+ly add a sort
eleent inside your for6each eleent in your X-* file like this$
>&sl8sort selectG(artist(5?
=o# take a look at your sli"htly ad2usted X-* stylesheet$
<?xml version!"#$!?>
<xsl<stylesheet version!"#$!
xmlns<xsl!htt%<//www#w0#org/"***/.7//Transform!>
<xsl<tem%late mat'h!/!>
<html>
<body>
<table border!)! bg'olor!yellow!>
<tr>
<th>Title</th>
<th>;rtist</th>
</tr>
<xsl<for+ea'h sele't!'atalog/'d!>
<xsl<sort sele't!artist!/>
<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>
Transforming it in your /rowser
'ere is the si+le source code needed to transfor the X)* file to '()* in your !ro#ser$
<html>
<body>
<s'ri%t ty%e!text/Xavas'ri%t!>
// /oad .&/
var xml new ;'tive.=bXe'tD!&i'rosoft#.&/D=&!F
xml#asyn' false
xml#loadD!'d'atalog#xml!F
// /oad the .7/
var xsl new ;'tive.=bXe'tD!&i'rosoft#.&/D=&!F
xsl#asyn' false
xsl#loadD!'d'atalogLsort#xsl!F
// Transform
do'(ment#writeDxml#transformAodeDxslFF
</s'ri%t>
</body>
</html>
The 4esult
1f you don;t have 1nternet .0+lorer G.0 to vie# the result& this is the result of the out+ut$
Title 0rtist
Roan7a :ndrea Bocelli
One ni"ht only Bee Mees
.+ire Burles8ue Bo! /ylan
'ide your heart Bonnie (yler
(he very !est of 9at -tevens
Mreatest 'its /olly Earton
-ylvias )other /r.'ook
.ros .ros Raa77otti
-till "ot the !lues Mary )oore
Cnchain y heart ,oe 9ocker
-oulsville ,orn 'oel
Bor the "ood ties Tenny Ro"ers
)idt o natten Ti *arsen
Eavarotti Mala 9oncert *uciano Eavarotti
1AAA Mray =oinees )any
(he dock of the !ay Otis Reddin"
%hen a an loves a #oan Eercy -led"e
)a""ie )ay Rod -te#art
-to+ -a Bro#n
Black an"el -ava"e Rose
Eicture !ook -i+ly Red
Brid"e of -+ies (ZEau
Red (he 9ounards
Erivate /ancer (ina (urner
(u+elo 'oney Van )orrison
Bi" %illie style %ill -ith
E"JX-L 6ilter :uery
X-L can be used to filter an XML file"
Where to put the 6ilter +nformation
(ake a ne# look at the X)* docuent that you have seen so any ties$
<?xml version!"#$!?>
<'atalog>
<'d>
<title>8m%ire S(rlesK(e</title>
<artist>Sob Dylan</artist>
<'o(ntry>N7;</'o(ntry>
<'om%any>>ol(mbia</'om%any>
<%ri'e>"$#*$</%ri'e>
<year>"*,2</year>
</'d>
#
#
#
</'atalog>
(o filter the X)* file& si+ly add a filter to the select attri!ute in your for6each eleent in your X-*
file like this$
>&sl8for-each selectG(catalog5cdKartistG;/ob Dylan;L(?
*e"al filter o+erators are$
@ (e8ual)
D@ (not e8ual)
OltP less than
O"tP "reater than
=o# take a look at your sli"htly ad2usted X-* stylesheet$
<?xml version!"#$!?>
<xsl<stylesheet version!"#$!
xmlns<xsl!htt%<//www#w0#org/"***/.7//Transform!>
<xsl<tem%late mat'h!/!>
<html>
<body>
<table border!)! bg'olor!yellow!>
<tr>
<th>Title</th>
<th>;rtist</th>
</tr>
<xsl<for+ea'h sele't!'atalog/'dBartist'Sob Dylan'C!>
<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>
Transforming it in your /rowser
'ere is the si+le source code needed to transfor the X)* file to '()* in your !ro#ser. 1nsert the
source code !elo# into an .ht or .htl file$
<html>
<body>
<s'ri%t ty%e!text/Xavas'ri%t!>
// /oad .&/
var xml new ;'tive.=bXe'tD!&i'rosoft#.&/D=&!F
xml#asyn' false
xml#loadD!'d'atalog#xml!F
// /oad the .7/
var xsl new ;'tive.=bXe'tD!&i'rosoft#.&/D=&!F
xsl#asyn' false
xsl#loadD!'d'atalogLfilter#xsl!F
// Transform
do'(ment#writeDxml#transformAodeDxslFF
</s'ri%t>
</body>
</html>
The 4esult
1f you don;t have 1nternet .0+lorer G.0 to vie# the result& this is the result of the out+ut$
Title 0rtist
.+ire Burles8ue Bo! /ylan
E"M X-L Conditional +6
X-L can use an +6 test to filter information from an XML document"
Where to put the +6 condition
(ake a ne# look at the X)* docuent that you have seen so any ties$
<?xml version!"#$!?>
<'atalog>
<'d>
<title>8m%ire S(rlesK(e</title>
<artist>Sob Dylan</artist>
<'o(ntry>N7;</'o(ntry>
<'om%any>>ol(mbia</'om%any>
<%ri'e>"$#*$</%ri'e>
<year>"*,2</year>
</'d>
#
#
#
</'atalog>
1f you use 1nternet .0+lorer H.0 or later& you can click here to vie# the X)* file
(o +ut a conditional if test a"ainst the content of the file& si+ly add an 0sl$if eleent to your X-*
docuent like this$
>&sl8if testG(artistG;/ob Dylan;(?
""" some output """
>5&sl8if?
=o# take a look at your sli"htly ad2usted X-* stylesheet$
<?xml version!"#$!?>
<xsl<stylesheet version!"#$!
xmlns<xsl!htt%<//www#w0#org/"***/.7//Transform!>
<xsl<tem%late mat'h!/!>
<html>
<body>
<table border!)! bg'olor!yellow!>
<tr>
<th>Title</th>
<th>;rtist</th>
</tr>
<xsl<for+ea'h sele't!'atalog/'d!>
<xsl<if test!artist'Sob Dylan'!>
<tr>
<td><xsl<val(e+of sele't!title!/></td>
<td><xsl<val(e+of sele't!artist!/></td>
</tr>
</xsl<if>
</xsl<for+ea'h>
</table>
</body>
</html>
</xsl<tem%late>
</xsl<stylesheet>
1f you use 1nternet .0+lorer H.0 or later& you can click here to vie# the X-* file.
Transforming it in your /rowser
'ere is the si+le source code needed transfor the X)* file to '()* in your !ro#ser$
<html>
<body>
<s'ri%t ty%e!text/Xavas'ri%t!>
// /oad .&/
var xml new ;'tive.=bXe'tD!&i'rosoft#.&/D=&!F
xml#asyn' false
xml#loadD!'d'atalog#xml!F
// /oad the .7/
var xsl new ;'tive.=bXe'tD!&i'rosoft#.&/D=&!F
xsl#asyn' false
xsl#loadD!'d'atalogLif#xsl!F
// Transform
do'(ment#writeDxml#transformAodeDxslFF
</s'ri%t>
</body>
</html>
The 4esult
1f you don;t have 1nternet .0+lorer G.0 to vie# the result& this is the result of the out+ut$
Title 0rtist
.+ire Burles8ue Bo! /ylan
E"N X-L Conditional Choose
X-L can use a conditional choose to filter an XML document"
Where to put the Choose Condition
(ake a ne# look at the X)* docuent that you have seen in alost every cha+ter$
<?xml version!"#$!?>
<'atalog>
<'d>
<title>8m%ire S(rlesK(e</title>
<artist>Sob Dylan</artist>
<'o(ntry>N7;</'o(ntry>
<'om%any>>ol(mbia</'om%any>
<%ri'e>"$#*$</%ri'e>
<year>"*,2</year>
</'d>
#
#
#
</'atalog>
(o insert a conditional choose test a"ainst the content of the file& si+ly add the 0sl$choose&
0sl$#hen and 0sl$other#ise eleents to your X-* docuent like this$
>&sl8choose?
>&sl8when testG(artistG;/ob Dylan;(?
""" some code """
>5&sl8when?
>&sl8otherwise?
""" some code """"
>5&sl8otherwise?
>5&sl8choose?
=o# take a look at your sli"htly ad2usted X-* stylesheet$
<?xml version!"#$!?>
<xsl<stylesheet version!"#$!
xmlns<xsl!htt%<//www#w0#org/"***/.7//Transform!>
<xsl<tem%late mat'h!/!>
<html>
<body>
<table border!)! bg'olor!yellow!>
<tr>
<th>Title</th>
<th>;rtist</th>
</tr>
<xsl<for+ea'h sele't!'atalog/'d!>
<tr>
<td><xsl<val(e+of sele't!title!/></td>
<xsl<'hoose>
<xsl<when test!artist'Sob Dylan'!>
<td bg'olor!Gff$$$$!>
<xsl<val(e+of sele't!artist!/>
</td>
</xsl<when>
<xsl<otherwise>
<td><xsl<val(e+of sele't!artist!/></td>
</xsl<otherwise>
</xsl<'hoose>
</tr>
</xsl<for+ea'h>
</table>
</body>
</html>
</xsl<tem%late>
</xsl<stylesheet>
Transforming it in your /rowser
'ere is the si+le source code needed to transfor the X)* file to '()* in your !ro#ser$
<html>
<body>
<s'ri%t ty%e!text/Xavas'ri%t!>
// /oad .&/
var xml new ;'tive.=bXe'tD!&i'rosoft#.&/D=&!F
xml#asyn' false
xml#loadD!'d'atalog#xml!F
// /oad the .7/
var xsl new ;'tive.=bXe'tD!&i'rosoft#.&/D=&!F
xsl#asyn' false
xsl#loadD!'d'atalogL'hoose#xsl!F
// Transform
do'(ment#writeDxml#transformAodeDxslFF
</s'ri%t>
</body>
</html>
The 4esult
Title 0rtist
.+ire Burles8ue Bo! /ylan
'ide your heart Bonnie (yler
Mreatest 'its /olly Earton
-till "ot the !lues Mary )oore
.ros .ros Raa77otti
One ni"ht only Bee Mees
-ylvias )other /r.'ook
)a""ie )ay Rod -te#art
Roan7a :ndrea Bocelli
%hen a an loves a #oan Eercy -led"e
Black an"el -ava"e Rose
1AAA Mray =oinees )any
Bor the "ood ties Tenny Ro"ers
Bi" %illie style %ill -ith
(u+elo 'oney Van )orrison
-oulsville ,orn 'oel
(he very !est of 9at -tevens
-to+ -a Bro#n
Brid"e of -+ies (ZEau
Erivate /ancer (ina (urner
)idt o natten Ti *arsen
Eavarotti Mala 9oncert *uciano Eavarotti
(he dock of the !ay Otis Reddin"
Eicture !ook -i+ly Red
B:U
10;;;;;; is a lan'ua'e for transformin' XML documents into other ty(es of
documents8 or into other XML documents.
a0 X+LT
b0 X(ath
c0 X+L
d0 <one of abo:e
20;;;;;; is a lan'ua'e for addressin' (arts of an XML document. X=ath was
desi'ned to be used by X+LT.
a0 X(ath
b0 X+LT
c0 X+L
d0 <one of abo:e
30X+L uses;;;;;;;; to describe how to transform XML.
a0 TM=L!T+
b0 X+LT
c0 <one of abo:e
)0X+L uses one or more tem(lates to define how to transform XML elements.
a0True
b0false
,0The X+L >xsl?:alue.of@ element can be used to select XML elements into the
out(ut stream of the X+L transformation?
a0True
b0false

Das könnte Ihnen auch gefallen