xml_karox
TRANSCRIPT
-
8/11/2019 XML_Karox
1/73
XML
(karROX Release Year 2007 Verison 1.0)
-
8/11/2019 XML_Karox
2/73
Table of Contents
Chapter 1 : Introduction to XML
1.1 XML GOAL and prerequisite
1.2The main difference between XML and HTML1.3 How can XML be Used?
Chapter 2 : XML Syntax
2.1 The syntax rules of XML2.2 XML lements2.3 XML !ttributes
Chapter 3 : XML DTD and Schema
3.1 "#ell $ormed" XML documents3.2 %ntroduction to XML &T& and schema3.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 Tem(lates,., X+L n the *lient,. X+L +ort
,.4 X+L $ilter 5uery,.6 X+L *onditional %$,.7 X+L *onditional *hoose
-
8/11/2019 XML_Karox
3/73
Chapter 1
Introduction to XML
-
8/11/2019 XML_Karox
4/73
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 languageuch 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 /Oanythin". X* is created as a #ay to structure& store and send inforation.he follo#in" ea+le is a note to ove fro ,ani& stored as X*$
ToveJaniReminderDon't forget me this weekend!
he note has a header& and a essa"e !ody. t also has sender and receiver inforation. But still&
this X* docuent does not /O anythin". t is ust +ure inforation #ra++ed in X* ta"s. -oeoneust #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+4and 3h14.....).
-
8/11/2019 XML_Karox
5/73
X* allo#s the author to define his o#n ta"s and his o#n docuent structurehe ta"s in the ea+le a!ove (like 3to4 and 3fro4)& are not defined in any X* standard. heseta"s are 5invented5 !y the author of the X* docuent.
XML is a complement to $TML
XML is not a replacement for $TML"
t is i+ortant to understand that X* is not a re+laceent for '*. n future %e! develo+ent itis ost likely that X* #ill !e used to descri!e the data& #hile '* #ill !e used to forat anddis+lay the sae data.y !est descri+tion of X* is as a cross6+latfor& soft#are and hard#are inde+endent tool fortransmitting information.
XML in future Web deelopment
XML is going to be eerywhere"
%e have !een +artici+atin" in X* develo+ent since its creation. t has !een aain" to see ho#8uickly the X* standard has !een develo+ed& and ho# 8uickly a lar"e nu!er of soft#are vendorshave 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 thefoundation of the %e!& and that X* #ill !e the ost coon tool for all data ani+ulation anddata transission.
!")$ow can XML be *sed?
+t is important to know that XML was designed to store, carry and e&change data" +t wasnot 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 !estored in se+arate X* files. his #ay you can concentrate on usin" '* for data layout anddis+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 slands5. 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"
n the real #orld& co+uter systes and data!ases contain data in inco+ati!le forats. One of theost tie6consuin" challen"es for develo+ers has !een to echan"e data !et#een such systesover the nternet.9onvertin" the data to X* can "reatly reduce this co+leity and create data that can !e read !yany different ty+es of a++lications.
XML and /#/
-
8/11/2019 XML_Karox
6/73
With XML, financial information can be e&changed oer the +nternet"+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 echan"in" financial inforation !et#een !usinesses overthe nternet. : 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 tet 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. t also akes iteasier to e+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 andretrieve 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!leto 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 iseasier 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+ nternet a++lications for handheld deviceslike 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 readeach other;s data in a +ure tet forat& #ithout any conversion utilities in !et#een.%e can only +ray that icrosoft and all the other soft#are vendors #ill a"ree.
-
8/11/2019 XML_Karox
7/73
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 andexchan'e data. %t was not desi'ned to dis(lay data
%rue
/alse
XML also ma9es it easier to ex(and or u('rade a system to newo(eratin' systems8 ser:ers8 a((lications8 and new browsers.
%rue/alse
-
8/11/2019 XML_Karox
8/73
Chapter 2
XML Syntax
-
8/11/2019 XML_Karox
9/73
XML Syntax
2!The synta& rules of XMLThe "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&le XML document
XML documents use a self-describing and simple synta&"
0ll XML elements must hae a closing tag
With XML, it is illegal to omit the closing tag"
n '* soe eleents do not have to have a closin" ta". he follo#in" code is le"al in '*$
This is a %aragra%hThis is another %aragra%h
n X* all eleents ust have a closin" ta" like this$
This is a %aragra%hThis is another %aragra%h
2ote$ You i"ht have noticed fro the +revious ea+le that the X* declaration did not have aclosin" ta". his is not an error. he declaration is not a +art of the X* docuent itself. t is not anX* 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$
-
8/11/2019 XML_Karox
10/73
This is inorret
This is orret
0ll XML elements must be properly nested
+mproper nesting of tags makes no sense to XML"
n '* soe eleents can !e i+ro+erly nested #ithin each other like this$
This text is bold and itali
n X* all eleents ust !e +ro+erly nested #ithin each other like this$
This text is bold and itali
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$
#####
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 ust like in '*. n X* the attri!ute valueust al#ays !e 8uoted. -tudy the t#o X* docuents !elo#. he first one is incorrect& the secondis correct$
ToveJaniReminderDon't forget me this weekend!
Tove
JaniReminderDon't forget me this weekend!
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
-
8/11/2019 XML_Karox
11/73
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 !edis+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 inthe +revious century $6):fter you have ty+ed one line of tet 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.n %indo#s a++lications& a ne# line in the tet is norally stored as a +air of 9R * (carria"ereturn& line feed) characters. n Cni a++lications& a ne# line is norally stored as a * character.-oe a++lications use only a 9R character to store a ne# line.
Comments in XML
he synta 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*. t is ust +lain tet #ith the addition of soe X* ta"s enclosed
in an"le !rackets.-oft#are that can handle +lain tet can also handle X*. n a si+le tet editor& the X* ta"s #ill!e visi!le and #ill not !e handled s+ecially.n an X*6a#are a++lication ho#ever& the X* ta"s can !e handled s+ecially. he ta"s ay or aynot !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 ea+le$
ToveJani
Don't forget me this weekend!
*et;s ia"ine that #e created an a++lication that etracted the 3to4& 3fro4& and 3!ody4eleents fro the X* docuent to +roduce this out+ut$
M.07.
To8ove6rom8,ani
-
8/11/2019 XML_Karox
12/73
/on;t for"et e this #eekendD
a"ine that the author of the X* docuent added soe etra inforation to it$
"***+$,+$"Tove
JaniReminderDon't forget me this weekend!
-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.a"ine that this is a descri+tion of a !ook$
/ook Title8 My 6irst XML
9ha+ter 1$ ntroduction to X*
%hat is '*
%hat is X*
9ha+ter 2$ X* -ynta leents ust have a closin" ta"
leents ust !e +ro+erly nested
a"ine that this X* docuent descri!es the !ook$
&y -irst .&4ntrod(tion to .&5hat is 6T&5hat is .&
.& 7yntax8lements m(st have a losing tag8lements m(st be %ro%erly nested
Book is the root element. itle& +rod& and cha+ter are child elementsof !ook. Book is the parentelementof 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 elementis everythin" fro (includin") the eleent;s start ta" to (includin") the eleent;send ta".
-
8/11/2019 XML_Karox
13/73
:n eleent can have elementcontent& mi&edcontent& simplecontent& or emptycontent. :neleent can also have attributes.n the ea+le a!ove& !ook has element content& !ecause it contains other eleents. 9ha+terhas mi&ed content!ecause it contains !oth tet and other eleents. Eara has simple content(orte&t content) !ecause it contains only tet. Erod has empty content& !ecause it carries noinforation.n the ea+le a!ove only the +rod eleent has attributes. he attributenaed id has the
alue5FF6GH75. he attributenaed edia has the alue5+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 l (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.a+les$ 3firstInae4& 3lastInae4.
:void 565 and 5.5 in naes. t 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!ect 5first5 (first.nae).leent naes can !e as lon" as you like& !ut don;t ea""erate. =aes should !e short andsi+le& like this$ 3!ookItitle4 not like this$ 3theItitleIofItheI!ook4.X* docuents often have a corres+ondin" data!ase& in #hich fields eist corres+ondin" toeleents in the X* docuent. : "ood +ractice is to use the nain" rules of your data!ase for theeleents in the X* docuents.=on6n"lish letters like JKL are +erfectly le"al in X* eleent naes& !ut #atch out for +ro!les ifyour 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" callednaes+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"
ro '* you #ill ree!er this$ 3M -R9@5co+uter."if54. he -R9 attri!ute +rovidesadditional inforation a!out the M eleent.n '* (and in X*) attri!utes +rovide additional inforation a!out eleents$
:ttri!utes often +rovide inforation that is not a +art of the data. n the ea+le !elo#& the filety+e is irrelevant to the data& !ut i+ortant to the soft#are that #ants to ani+ulate the eleent$
-
8/11/2019 XML_Karox
14/73
om%(ter#gif
: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.or a +erson;s se& the +erson ta" can !e #ritten like this$
or like this$
/ou!le 8uotes are the ost coon& !ut soeties (if the attri!ute value itself contains 8uotes) itis necessary to use sin"le 8uotes& like in this ea+le$
*se of .lements s" 0ttributes
Data can be stored in child elements or in attributes"
ake a look at these ea+les$
;nna 7mith
female ;nna 7mith
n the first ea+le se is an attri!ute. n the last& se is a child eleent. Both ea+les +rovidethe sae inforation.here are no rules a!out #hen to use attri!utes& and #hen to use child eleents. y e+erience isthat attri!utes are handy in '*& !ut in X* you should try to avoid the. Cse child eleents ifthe inforation feels like data.
My 6aorite Way
+ like to store data in child elements"
he follo#in" three X* docuents contain eactly the sae inforation$: date attri!ute is used in the first ea+le$
-
8/11/2019 XML_Karox
15/73
:n e+anded date eleent is used in the third$ ('- - Y :VOR)$
") "" **
ToveJaniReminderDon't forget me this weekend!
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 e+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 //
f you use attri!utes as containers for data& you end u+ #ith docuents that are difficult to read andaintain. ry to use elementsto descri!e data. Cse attri!utes only to +rovide inforation that isnot relevant to the data./on;t end u+ like this ( if you think this looks like X*& you have not understood the +oint)$
Tove Jani
Reminder Don't forget me this weekend!
Jani Tove Re Reminder 4 will not!
-
8/11/2019 XML_Karox
16/73
he / in these ea+les is ust a counter& or a uni8ue identifier& to identify the different notes inthe X* file& and not a +art of the note data.%hat a tryin" to say here is that etadata (data a!out data) should !e stored as attri!utes& andthat data itself should !e stored as eleents.
FAQ
1. %hat is the correct synta of the declaration #hich defines the X* version>
a)3l [email protected] ?4 !)3>l [email protected] ?4 c)3>l [email protected]>42. %hich stateent is true>
a) :ll the stateents are true!) :ll X* docuents ust have a //c) :ll X* eleents ust !e lo#er cased) :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 nestedd) X* ta"s are case sensitive
3>l [email protected]>43note43to a"e@52A54ove3?to43fro4,ani3?fro43?note4
a) =o!) Yes
-
8/11/2019 XML_Karox
17/73
Chapter '
XML DTD and Schema
-
8/11/2019 XML_Karox
18/73
-
8/11/2019 XML_Karox
19/73
%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* docuentif it finds a validation error. he reason is that X* soft#are should !e easy to #rite& and that allX* 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 theiro#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 anXML 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
f the // is included in your X* source file& it should !e #ra++ed in a /O9YE definition #ith thefollo#in" synta$
a+le X* docuent #ith a //$ (O+en it in H& and select vie# source)$
.&ternal D%CT'1. declaration
http://www.w3schools.com/dtd/note_in_dtd.xmlhttp://www.w3schools.com/dtd/note_in_dtd.xml -
8/11/2019 XML_Karox
20/73
f the // is eternal to your X* source file& it should !e #ra++ed in a /O9YE definition #ith thefollo#in" synta$
his is the sae X* docuent as a!ove& !ut #ith an eternal //$ (O+en it in H&and selectvie# source)
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 #orldis 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 thefollo#in" si+le !uildin" !locks$
leents
a"s
:ttri!utes
ntities
E9/::
9/::
he follo#in" is a !rief e+lanation of each of the !uildin" !locks$
.lements
leents are the main building blocksof !oth X* and '* docuents.a+les of '* eleents are 5!ody5 and 5ta!le5. a+les of X* eleents could !e 5note5 and5essa"e5. leents can contain tet& other eleents& or !e e+ty. a+les of e+ty '*eleents are 5hr5& 5!r5 and 5i"5.
Tags
http://www.w3schools.com/dtd/note_ex_dtd.xmlhttp://www.w3schools.com/dtd/note_ex_dtd.xmlhttp://www.w3schools.com/dtd/note_ex_dtd.xml -
8/11/2019 XML_Karox
21/73
a"s are used to markup elements.: startin" ta" like 3eleentInae4 arks u+ the !e"innin" of an eleent& and an endin" ta" like3?eleentInae4 arks u+ the end of an eleent.a+les$
body element marked (% with body tagsbody text in between#
message element marked (% with message tagssome message in between
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 innae?value +airs. he follo#in" 5i"5 eleent has additional inforation a!out a source file$
he nae of the eleent is 5i"5. he nae of the attri!ute is 5src5. he value of the attri!ute is5co+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$ 5n!s+P5. his 5no6!reakin"6s+ace5 entity is usedin '* to insert an etra s+ace in a docuent. ntities are e+anded #hen a docuent is +arsed!y an X* +arser.he follo#in" entities are +redefined in X*$
.ntity 4eferences Character
ltP 3
"tP 4
a+P
8uotP 5
a+osP ;
1CD0T0
E9/:: eans +arsed character data.hink of character data as the tet 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 tet #ill !e treated as arku+and entities #ill !e e+anded.
CD0T0
9/:: also eans character data.CD0T0 is te&t that will 2%T be parsed by a parser. a"s inside the tet #ill =O !e treated as
arku+ and entities #ill not !e e+anded.+n a DTD, XML elements are declared with a DTD element declaration"
Declaring an .lement
n the //& X* eleents are declared #ith an eleent declaration. :n eleent declaration has thefollo#in" synta$
or
-
8/11/2019 XML_Karox
22/73
.mpty elements
+ty eleents are declared #ith the cate"ory key#ord EY$
DTD exam%le.& exam%le
.lements with only character data
leents #ith only character data are declared #ith NE9/:: inside +arentheses$
exam%le
.lements with any contents
leents declared #ith the cate"ory key#ord :=Y& can contain any co!ination of +arsa!le data$
exam%le
.lements with children @se3uencesA
leents #ith one or ore children are defined #ith the nae of the children eleents inside+arentheses$
orexam%le
%hen children are declared in a se8uence se+arated !y coas& the children ust a++ear in thesae se8uence in the docuent. n a full declaration& the children ust also !e declared& and thechildren can also have children. he full declaration of the note docuent #ill !e$
Declaring only one occurrence of the same element
-
8/11/2019 XML_Karox
23/73
exam%le
he ea+le declaration a!ove declares that the child eleent essa"e can only occur one tieinside the note eleent.
Declaring minimum one occurrence of the same element
exam%le
he Q si"n in the ea+le a!ove declares that the child eleent essa"e ust occur one or oreties inside the note eleent.
Declaring Bero or more occurrences of the same element
exam%le
he si"n in the ea+le a!ove declares that the child eleent essa"e can occur ero or oreties inside the note eleent.
Declaring Bero or one occurrences of the same element
exam%le
he > si"n in the ea+le a!ove declares that the child eleent essa"e can occur ero or oneties inside the note eleent.
Declaring either5or content
exam%le
he ea+le a!ove declares that the eleent note ust contain a to eleent& a fro eleent& aheader eleent& and either a essa"e eleent or a !ody eleent.
Declaring mi&ed content
exam%le
he ea+le a!ove declares that the eleent note can contain +arsed character data or anynu!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" synta$
-
8/11/2019 XML_Karox
24/73
exam%leDTD exam%le
.& exam%le
he attribute-typecan have the follo#in" values$
-
8/11/2019 XML_Karox
25/73
0ttribute declaration e&le
DTD exam%le
.& exam%le
DTD exam%le
.& exam%le
-+ecifyin" a default value for an attri!ute ensures that the attri!ute #ill "et a value even if theauthor of the X* docuent didn;t include it.
+mplied attribute
7yntaxDTD exam%le
.& exam%le
Cse an i+lied attri!ute if you don;t #ant to force the author to include an attri!ute& and you don;thave an o+tion for a default value.
4e3uired attribute
7yntaxDTD exam%le
.& exam%le
Cse a re8uired attri!ute if you don;t have an o+tion for a default value& !ut still #ant to force theattri!ute to !e +resent.
6i&ed attribute alue
7yntax
-
8/11/2019 XML_Karox
26/73
attrib(te+ty%e G-4.8D val(e>DTD exam%le
.& exam%le
Cse a fied attri!ute value #hen you #ant an attri!ute to have a fied value #ithout allo#in" theauthor to chan"e it. f an author includes another value& the X* +arser #ill return an error.
.numerated attribute alues
7yntax
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
DTD 8xam%le.& exam%leOwriterPOo%yrightP
-
8/11/2019 XML_Karox
27/73
.&ternal .ntity Declaration
7yntax
DTD 8xam%le
77T8& htt%//www#karrox#om/entities/entities#xml>.& exam%leOwriterPOo%yrightP
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& ust 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 tet
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 etensi!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
-
8/11/2019 XML_Karox
28/73
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$
t is easier to descri!e +erissi!le docuent content
t is easier to validate the correctness of data
t is easier to #ork #ith data fro a data!ase
t is easier to define data facets (restrictions on data)
t is easier to define data +atterns (data forats)
t 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 sae5e+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 othercountries) as 11.arch& !ut an X* eleent #ith a data ty+e like this$3date ty+e@5date541AAA60F6113?date4ensures a utual understandin" of the content !ecause the X* data ty+e date re8uires the forat99YY66//.
XML chemas are .&tensible
X* -cheas are etensi!le& ust like X*& !ecause they are #ritten in X*.%ith an etensi!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* synta rules$
ust !e"in #ith the X* declaration
ust have one uni8ue root eleent
all start ta"s ust atch end6ta"s
-
8/11/2019 XML_Karox
29/73
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 %ell6ored they can still contain errors& and those errors can have seriousconse8uences. 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.l) is co+ied fro our X* -chool.
-
8/11/2019 XML_Karox
30/73
he -chea defines the eleent noteto !e a comple& elementcontainin" a se8uence of other
simple elements.
0 4eference to the DTD
his X* docuent has a reference to the //$
-
8/11/2019 XML_Karox
31/73
Defining imple .lements
-i+le eleents are defined like this$
7yntax8xam%les
Defining Comple& .lements
9o+le eleents are defined like this$
7yntax # # element ontent
# 8xam%le
Describing the 2ote Document
his X* -chea defines the note docuent$
he -chea a!ove definesnoteeleent to !e a comple&eleent containin" a se8uence of othersimpleeleents.
4eferring other .lements
-
8/11/2019 XML_Karox
32/73
his X* -chea defines eactly the sae as the one a!ove$
=ote that the noteeleent is referrin" to its su! eleents #ith the refattri!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 tet. t cannot contain any othereleents or attri!utes.'o#ever& the 5only tet5 restriction is 8uite isleadin". he tet can !e of any different ty+es. tcan !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 re8uirethe data to atch a defined +attern.
$ow to Define a imple Type .lement
he synta for definin" a si+le ty+e eleent is$
#here is the nae of the eleent and yyy is the ty+e.'ere are soe X* eleents$
Refsnes0Q
"*,,+$0+)3:nd here are soe corres+ondin" si+le ty+e eleent definitions$
Common XML chema Data Types
-
8/11/2019 XML_Karox
33/73
X* -chea has a lot of !uilt6in data ty+es. 'ere is a list of the ost coon ty+es$ s$strin"
s$decial
s$inte"er
s$!oolean
s$date
s$tie
or 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.f an X* eleent of the ty+e s$date contains a strin" like 5'ello other5& the eleent #ill create avalidation error.%ith X* -chea definitions& you can add your o#n restrictions to your X* eleents. heserestrictions are called facets. You can read ore a!out facets in the net cha+ter.
XML chema 6acets
4estrictions on XML elements are called facets"
4estrictions on
4estrictions on .numerated
-
8/11/2019 XML_Karox
34/73
n this case the ty+e 5cary+e5 can !e used !y other eleents !ecause it is not a +art of the 5car5eleent.
0n XML chema .&le
.&le XML hipping %rder
Ree!er the 9/ catalo" fro X* -chool> his is a 9/ order$
-
8/11/2019 XML_Karox
35/73
he X* -chea a!ove defines the eleent 3shi+Order4 to !e of the ty+e order. Order is aco+le ty+e eleent consistin" of the eleents 3shi+o4 and 3ites4. he 3shi+o4 eleent isof the ty+e shi+:ddress 6 a co+le ty+e eleent consistin" of the eleents 3nae4& 3street4&3address4& and 3country4. he 3ites4 eleent is of the ty+e cdtes 6 a co+le ty+e eleentconsistin" of 3ite4 eleents. he 3ite4 eleent is of the ty+e cdte 6 a co+le ty+e eleentconsistin" of 3title4& 38uantity4& and 3+rice4 eleents. he 3title4 eleent is a noral eleentof the ty+e strin".
!A"
1. s this a correct X* docuent>
3>l [email protected]>43note43to a"e@2A4ove3?to43fro4,ani3?fro43?note4
a) =o
!)Yes
2. X* eleents cannot !e e+ty
a) rue!) alse
F. %hich is not a correct nae for an X* eleent>
-
8/11/2019 XML_Karox
36/73
a) 31dollar4c) 3h14d) :ll F naes are incorrect
a) 3first nae4!) 3a"e4c) 3=:4d) :ll F naes are incorrect
H. %hich is not a correct nae for an X* eleent>
a) 3ldocuent4!) :ll F naes are incorrectc) 3+hone nu!er4d) 37eleven4
G. :ttri!ute values ust al#ays !e enclosed !y dou!le 8uotes
a) alse
!) rue
7. he +ur+ose of a /ocuent y+e /efinition is to define the le"al !uildin" !locks of an X*
docuent. t 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 filesa) rue!) false
534ou (an use 'our XML parser to parse 'our *(he#a filesa) 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
-
8/11/2019 XML_Karox
37/73
-
8/11/2019 XML_Karox
38/73
Chapter
Disp#a$ing %# using css
Dispalying Xml with C
"! What is C?
Cstands for Cascadin" tyle heets
-tyles define how to display'* eleents
-tyles are norally stored in tyle heets
-tyles #ere added to '*
-
8/11/2019 XML_Karox
39/73
:s the t#o aor !ro#sers 6 =etsca+e and nternet +lorer 6 continued to add ne# '* ta"s andattri!utes (like the 3font4 ta" and the color attri!ute) to the ori"inal '* s+ecification& it !ecaeore and ore difficult to create %e! sites #here the content of '* docuents #as clearlyse+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 standardiin" '* 6 created -Y*- in addition to '*
-
8/11/2019 XML_Karox
40/73
% font+family sans serifU
2ote8f you #ish to s+ecify ore than one+ro+erty& you should se+arate each +ro+erty #ith asemi-colon. he ea+le !elo# sho#s ho# to define a center ali"ned +ara"ra+h& #ith a red tetcolor$
% text+align enterP olor redU
o ake the style definitions ore reada!le& you can descri!e one +ro+erty on each line& like this$
@text+align enterPolor blakPfont+family arialU
7rouping
You can "rou+ selectors. -e+arate each selector #ith a coa. n the ea+le !elo# #e have"rou+ed all the header eleents. ach header eleent #ill !e "reen$
h"E h)E h0E hQE h2E h1olor greenU
The class 0ttribute
%ith the class attri!ute you can define different styles for the sae eleent. -ay that you #ouldlike to have t#o ty+es of +ara"ra+hs in your docuent$ one ri"ht6ali"ned +ara"ra+h& and onecenter6ali"ned +ara"ra+h. 'ere is ho# you can do it #ith styles$
%#right text+align rightU%#enter text+align enterU
You have to use the class attri!ute in your '* docuent$This %aragra%h will be right+aligned#
This %aragra%h will be enter+aligned#
You can also omitthe ta" nae in the selector to define a style that can !e used !y anyeleents$
#enter text+align enterU
n the code !elo# boththe h1 eleent and the + eleent are classed #ith 5center5. his eansthat !oth of the eleents #ill follo# the rules in the 5.center5 selector$
This %aragra%h will also be enter+aligned#
The id 0ttribute
-
8/11/2019 XML_Karox
41/73
he id attri!ute has to !e uni3ueon the +a"e. here can only !e oneeleent #ith a "iven id in adocuent.hey are arked in the '* docuent #ith id instead of class$
This %aragra%h will be right+aligned#
he id attri!ute can !e defined in t#o #ays. t can !e defined to atch all eleents #ith a+articular id& or to atch only one eleent #ith a +articular id.n this ea+le the id attri!ute #ill atch all eleents #ith id@5intro5$
Gintrofont+siVe""$WPfont+weightboldPolorG$$$$ffPbakgro(nd+olortrans%arentU
n this ea+le the id attri!ute #ill atch only + eleents #ith id@5intro5$
%Gintrofont+siVe""$WP
font+weightboldPolorG$$$$ffPbakgro(nd+olortrans%arentU
C Comments
You can insert coents in 9-- to e+lain your code& #hich can hel+ you #hen you edit the sourcecode at a later date. : coent #ill !e i"nored !y the !ro#ser. : 9-- coent !e"ins #ith 5?5&and ends #ith 5?5& like this$
/I This is a omment I/%
text+align enterP/I This is another omment I/olor blakPfont+family arialU
") 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
-
8/11/2019 XML_Karox
42/73
:n eternal style sheet is ideal #hen the style is a++lied to any +a"es. %ith an eternal stylesheet& you can chan"e the look of an entire %e! site !y chan"in" one file. ach +a"e ust link tothe style sheet usin" the 3link4 ta". he 3link4 ta" "oes inside the head section$
he !ro#ser #ill read the style definitions fro the file ystyle.css& and forat the docuentaccordin" to it.:n eternal style sheet can !e #ritten in any tet editor. he file should not contain any htl ta"s.Your style sheet should !e saved #ith a .css etension. :n ea+le of a style sheet file is sho#n!elo#$
hr olor siennaU% margin+left )$%xUbody bakgro(nd+image (rlimages/bakQ$#gifFU
+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$hr olor siennaU% margin+left )$%xUbody bakgro(nd+image (rlimages/bakQ$#gifFU
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 notsu++ort styles& #ill i"nore the 3style4 ta"& !ut the content of the 3style4 ta" #ill !e dis+layed onthe +a"e. t is +ossi!le to +revent an old !ro#ser fro dis+layin" the content !y hidin" it in the'* coent eleent$
+nline tyles
:n inline style loses any of the advanta"es of style sheets !y iin" 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 containany 9-- +ro+erty. he ea+le sho#s ho# to chan"e the color and the left ar"in of a +ara"ra+h$
This is a %aragra%h
Multiple tyle heets
-
8/11/2019 XML_Karox
43/73
f soe +ro+erties have !een set for the sae selector in different style sheets& the values #ill !einherited fro the ore s+ecific style sheet.or ea+le& an eternal style sheet has these +ro+erties for the hF selector$
h0olor redPtext+align leftP
font+siVe ,%tU
:nd an internal style sheet has these +ro+erties for the hF selector$
h0text+align rightPfont+siVe )$%tU
f the +a"e #ith the internal style sheet also links to the eternal style sheet the +ro+erties for hF#ill !e$
olor redPtext+align rightPfont+siVe )$%t
he color is inherited fro the eternal style sheet and the tet6ali"nent and the font6sie isre+laced !y the internal style sheet.
See the following exampleExample 4.1
!his header is # pt!his header is l%e
!his paragraph has a leftmargin of &' pixels
!his is the style sheet (le )ex1.css*+
-
8/11/2019 XML_Karox
44/73
9ss to set !ack"round
a+le
-
8/11/2019 XML_Karox
45/73
3style ty+e@5tet?css54
hF font6faily$ tiesW
+ font6faily$ courierW
+.sansserif font6faily$ 5sans6serif5W
3?style4
3?head4
3!ody4
3hF4his is header F3?hF4
3+4
his is a +ara"ra+h3?+4
3+ class@5sansserif54
his is a +ara"ra+h3?+4
3?!ody43?htl4
a+le
-
8/11/2019 XML_Karox
46/73
3+ class@5solid54: solid !order3?+43!r4
3+ class@5dou!le54: dou!le !order3?+43!r4
3+ class@5"roove54: "roove !order3?+43!r4
3+ class@5rid"e54: rid"e !order3?+43!r4
3+ class@5inset54:n inset !order3?+43!r4
3+ class@5outset54:n outset !order3?+4
3?!ody43?htl4
a+le
-
8/11/2019 XML_Karox
47/73
3?head4
3!ody4
3ta!le !order@51543tr4
3td4his is a ta!lecell #ith a left +addin"3?td43?tr43?ta!le4
3?!ody43?htl4
a+le
-
8/11/2019 XML_Karox
48/73
3?!ody43?htl4
a+le l [email protected]>439::*OM4 3*4Mreatest 'its3?*4 3:R-4/olly Earton3?:R-4 39OC=RY4C-:3?9OC=RY4 39OE:=Y4R9:3?9OE:=Y4 3ER94A.A03?ER94 3Y:R41A23?Y:R4 3?9/4 39/4 3*4-till "ot the !lues3?*4 3:R-4Mary oore3?:R-4 39OC=RY4CT3?9OC=RY4
39OE:=Y4Vir"in records3?9OE:=Y4 3ER9410.203?ER94 3Y:R41AA03?Y:R4 3?9/4 39/4 3*4Brid"e of -+ies3?*4 3:R-4;Eau3?:R-4 39OC=RY4CT3?9OC=RY4 39OE:=Y4-iren3?9OE:=Y4 3ER947.A03?ER94 3Y:R41A73?Y:R4 3?9/4 39/4 3*4Erivate /ancer3?*4 3:R-4ina urner3?:R-4 39OC=RY4CT3?9OC=RY4 39OE:=Y49a+itol3?9OE:=Y4 3ER94.A03?ER94 3Y:R41AF3?Y:R4 3?9/4 3*4he dock of the !ay3?*4 3:R-4Otis Reddin"3?:R-4 39OC=RY4C-:3?9OC=RY4 39OE:=Y4:tlantic3?9OE:=Y4 3ER947.A03?ER94
-
8/11/2019 XML_Karox
49/73
3Y:R41A73?Y:R4 3?9/4 39/4 3*4Eicture !ook3?*4 3:R-4-i+ly Red3?:R-4 39OC=RY4C3?9OC=RY4 39OE:=Y4lektra3?9OE:=Y4
3ER947.203?ER94 3Y:R41AH3?Y:R4 3?9/43?9::*OM4
save this file as cdcatalo".l
;T;=9bakgro(nd+olor GffffffPwidth "$$WPUDdis%lay blokPmargin+bottom 0$%tPmargin+left $P
UT4T8olor G--$$$$Pfont+siVe )$%tPU;RT47Tolor G$$$$--Pfont+siVe )$%tP
U=NATRE@R48E8;RE=&@;ADis%lay blokPolor G$$$$$$Pmargin+left )$%tPU
-
8/11/2019 XML_Karox
50/73
sa,e this as cd-catalog.css
inally& vie#$ he 9/ 9atalo" foratted #ith the 9-- file
3>l [email protected]>4
3>l6stylesheet ty+e@5tet?css5 href@5cdIcatalo".css5>4
3>l [email protected]>439::*OM4 3*4Mreatest 'its3?*4 3:R-4/olly Earton3?:R-4 39OC=RY4C-:3?9OC=RY4 39OE:=Y4R9:3?9OE:=Y4 3ER94A.A03?ER94 3Y:R41A23?Y:R4 3?9/4 39/4 3*4-till "ot the !lues3?*4 3:R-4Mary oore3?:R-4 39OC=RY4CT3?9OC=RY4 39OE:=Y4Vir"in records3?9OE:=Y4 3ER9410.203?ER94 3Y:R41AA03?Y:R4 3?9/4 39/4 3*4Brid"e of -+ies3?*4 3:R-4;Eau3?:R-4 39OC=RY4CT3?9OC=RY4
http://www.w3schools.com/xml/cd_catalog_with_css.xmlhttp://www.w3schools.com/xml/cd_catalog_with_css.xml -
8/11/2019 XML_Karox
51/73
39OE:=Y4-iren3?9OE:=Y4 3ER947.A03?ER94 3Y:R41A73?Y:R4 3?9/4 39/4 3*4Erivate /ancer3?*4 3:R-4ina urner3?:R-4
39OC=RY4CT3?9OC=RY4 39OE:=Y49a+itol3?9OE:=Y4 3ER94.A03?ER94 3Y:R41AF3?Y:R4 3?9/4 3*4he dock of the !ay3?*4 3:R-4Otis Reddin"3?:R-4 39OC=RY4C-:3?9OC=RY4 39OE:=Y4:tlantic3?9OE:=Y4 3ER947.A03?ER94 3Y:R41A73?Y:R4 3?9/4 39/4 3*4Eicture !ook3?*4 3:R-4-i+ly Red3?:R-4
39OC=RY4C3?9OC=RY4 39OE:=Y4lektra3?9OE:=Y4 3ER947.203?ER94 3Y:R41AH3?Y:R4 3?9/43?9::*OM4
save this file cdIcata#ith9--.l
0
1. here in the 2!3 doc%ment is the correct place to refer to anexternal style sheet5a* t the top of the doc%ment* 6n the sectionc*6n the sectiond* t the end of the doc%ment
$. hich 2!3 tag is %sed to de(ne an internal style sheet5
a* * c*
. hat is the correct 2!3 for refering to an external style sheet5
-
8/11/2019 XML_Karox
52/73
-
8/11/2019 XML_Karox
53/73
* c*
Chapter 5
X+L
-
8/11/2019 XML_Karox
54/73
X+L
,.1 %ntroduction to X+L
XL, 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 >h1eleent defines a headin"& and the !ro#ser kno#s howto displaythese 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#serto understand.
XL - The tyle heet of XML
Because X* does not use predefined tags(#e can use any ta"s #e #ant)& the eanin"s ofthese 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 displayan X* docuent.
-
8/11/2019 XML_Karox
55/73
n order to dis+lay X* docuents& it is necessary to have a echanis to descri!e ho# thedocuent should !e dis+layed. One of these echaniss is 9--& !ut X-* (the eXtensi!letylesheet Lan"ua"e) is the +referred style sheet lan"ua"e of X*& and X-* is far oreso+histicated than the 9-- used !y '*.
XL - 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
f you don;t understand the eanin" of this& think of X-* as a lan"ua"e that can transformX*into '*& a lan"ua"e that can filter and sort X* data& a lan"ua"e that can address partsof anX* docuent& a lan"ua"e that can formatX* data !ased on the data value& like dis+layin"ne"ative nu!ers in red& and a lan"ua"e that can outputX* data to different devices& like screen&+a+er or voice.
XL actually consists of three languages" The most important is XLT"
XL 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-* orattin" O!ects 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 otherX* docuents.XEath is a lan"ua"e for addressin" +arts of an X* docuent. XEath #as desi"ned to !e used !yX-*.orattin" is the +rocess of turnin" the result of an X-* transforation into a suita!le out+ut forfor 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 eists for X-* orattin" O!ects& !ut a descri+tion can !e found insidethe X-* 1.0 Recoendation.You can read ore a!out the %F9 X-* activities in our %F9 X-* section.
XLT - XL Transformations
X-* is the ost i+ortant +art of the X-* -tandard. t is the +art of X-* that is used to transforan 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"nia!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. t canrearran"e and sort the eleents& and test and ake decisions a!out #hich eleents to dis+lay& anda lot ore.
: coon #ay to descri!e the transforation +rocess is to say that X-* uses X-* to transfor anX* source treeinto an X* result tree (or an X* source documentinto an X* resultdocument)
$ow does it work?
n the transforation +rocess& X-* uses XEath to define +arts of the source docuent that matchone or ore +redefined templates. %hen a atch is found& X-* #ill transformthe atchin" +artof the sourcedocuent into the resultdocuent. he +arts of the source docuent that do notatch a te+late #ill (as a "eneral rule) end u+ unodified in the result.
http://www.w3schools.com/w3c/w3c_xsl.asphttp://www.w3schools.com/w3c/w3c_xsl.asphttp://www.w3schools.com/w3c/w3c_xsl.asp -
8/11/2019 XML_Karox
56/73
This chapter will focus on XLT 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 "eneralter X-* in this tutorial.
E"#XL /rowsers
6ew +nternet browsers hae full support for XL at the moment"
We will use +nternet .&plorer to demonstrate XL in this tutorial"
We will use +nternet .&plorer
n order to +rocess an X* docuent usin" X-*& you #ill need an X* +arser #ith an X-* n"ine.-ince nternet +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 nternet +lorer H is not co+ati!le #ith the official %F9 X-* Recoendation.%hen nternet +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 H is not 100 co+ati!le #ith the official X-* standard.his restriction a++lies for !oth nternet +lorer H.0 and H.H.9lick here to read ore a!out the X-* activities at the %F9.
+nternet .&plorer F is /etter
nternet +lorer G fully su++orts the official %F9 X-* Recoendation.he X* Earser F.0 6 !uilt onto nternet +lorer G.0 and %indo#s XE 6 is !ased on !oth the %F9X-* 1.0 and the %F9 XEath 1.0 Recoendations.:t the oent& the ea+le code in this tutorial #ill only #ork in nternet +lorer H or later. anyof the ea+les #ill only #ork in nternet +lorer G.0.f you are serious a!out learnin" X-* you should u+"rade to nternet +lorer G.0.
http://www.w3schools.com/w3c/w3c_xsl.asphttp://www.w3schools.com/w3c/w3c_xsl.asp -
8/11/2019 XML_Karox
57/73
E") XL - Transformation @XLTA
Correct tyle heet Declaration
his is the correct #ay to declare an X-* style sheet accordin" to the %F9 X-* Recoendation$
f you are usin" nternet +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$
f you are usin" nternet +lorer H.0 you should use the (incorrect) code a!ove.
.&le study8 $ow to transform XML into $TML using XL"
The details of this e&le will be e&plained in the ne&t chapter"
tart with your XML Document
irst you start #ith the X* docuent that you #ant to transforminto '*$
-
8/11/2019 XML_Karox
58/73
Create an XL tyle heet Document
hen you create an X-* -tyle -heet #ith a transforation template8
8m%ire S(rlesK(e Sob Dylan N7; ol(mbia "$#*$ "*,2 ###
f you have an X-* co+liant !ro#ser& like nternet +lorer H.0 or later& your !ro#ser #ill nicelytransformyour X* into '*.
E" XL - Templates
C uses 4ules
-
8/11/2019 XML_Karox
59/73
f you have studied our 9-- -chool& you #ill kno# that 9-- uses one or ore rulesto define theout+ut of '* eleents. : selectoris used to associatethe rule #ith an '* element.he + selector in this 9-- rule tells that a 3+4 eleent should !e dis+layed usin" a font naedarial$
% font+family arial U
XL uses Templates
X-* uses one or oretemplatesto define ho# to transformX* eleents.: match attributeis used to associatethe te+late #ith an XML element. he atch attri!utecan also !e used to define a te+late for a #hole !ranch of the X* docuent& like #hen #e useatch@5?5 to define the #hole docuent.*ook at the follo#in" XL tyle heetthat contains a templateto out+ut the X* 9/ 9atalo"fro the +revious cha+ter$
Title ;rtist # #
-ince the style sheet is an X* docuent itself& the docuent !e"ins #ith an l declaration$ >?&ml ersionG;!"H;?
he &sl8stylesheetta" in the second line defines the start of the stylesheet.he &sl8templateta" in the third line defines the start of a te+late. he te+late attri!utematchG(5(associates (atches) the te+late to the root (?) of the X* source docuent.he rest of the docuent contains the te+late itself& ece+t for the last t#o lines that defines theend 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 .lementhe result fro the +revious sa+le #as a little disa++ointin"& !ecause no data #as co+ied fro theX* docuent to the out+ut.he X-* >&sl8alue-ofeleent can !e used to selectX* eleents into the out+ut strea ofthe X-* transforation$
-
8/11/2019 XML_Karox
60/73
&sl8for-eacheleent to select every X*eleent into the out+ut strea of the transforation.
XL 6or .ach
The >&sl8for-each is used to select all elements into the output stream"
The >&sl8for-each .lement
he result fro the +revious ea+le #as a little disa++ointin"& !ecause only one line of data #asco+ied fro the X* docuent to the out+ut.he X-* >&sl8for-eacheleent can !e used to select every X* eleent into the out+ut streaof the X-* transforation$
Title ;rtist
-
8/11/2019 XML_Karox
61/73
he &sl8for-eacheleent locates eleents in the X* docuent and re+eats a +art of thete+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 Raaotti
One ni"ht only Bee Mees
-ylvias other /r.'ook
a""ie ay Rod -te#art
Roana :ndrea Bocelli
%hen a an loves a #oan Eercy -led"e
Black an"el -ava"e Rose
1AAA Mray =oinees any
or 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
-
8/11/2019 XML_Karox
62/73
E"E XL - %n the Client
+f your browser supports XML, XL can be used to transform the document to $TML inyour browser"
0 Iaacript olution
n the +revious cha+ter #e e+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 thetransforation.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 datafro one forat to another& su++ortin" different !ro#sers and different user needs.X-* transforation on the client side is !ound to !e a aor +art of the !ro#sers #ork tasks in thefuture& as #e #ill see a "ro#th in the s+ecialied !ro#ser arket (think$ Braille& -+eakin" %e!& %e!Erinters& 'andheld E9s& o!ile Ehones .....).
The XML file and the XL file
ake a ne# look at the X* docuent that you sa# in the +revious cha+ter$
-
8/11/2019 XML_Karox
63/73
:nd the acco+anyin" X-* stylesheet$
-
8/11/2019 XML_Karox
64/73
E"FXL ort
XL 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$
-
8/11/2019 XML_Karox
65/73
Transforming it in your /rowser
'ere is the si+le source code needed to transfor the X* file to '* in your !ro#ser$
// oad .&var xml new ;tive.=bXet&irosoft#.&D=&Fxml#asyn falsexml#loaddatalog#xmlF
// oad the .7var xsl new ;tive.=bXet&irosoft#.&D=&Fxsl#asyn falsexsl#loaddatalogLsort#xslF
// Transformdo(ment#writexml#transformAodexslFF
The 4esult
f you don;t have nternet +lorer G.0 to vie# the result& this is the result of the out+ut$
Title 0rtist
Roana :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 Raaotti
-till "ot the !lues Mary oore
Cnchain y heart ,oe 9ocker
-oulsville ,orn 'oel
or the "ood ties Tenny Ro"ers
idt o natten Ti *arsen
Eavarotti Mala 9oncert *uciano Eavarotti
1AAA Mray =oinees any
-
8/11/2019 XML_Karox
66/73
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"JXL 6ilter :uery
XL 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$
-
8/11/2019 XML_Karox
67/73
Title ;rtist
Transforming it in your /rowser
'ere is the si+le source code needed to transfor the X* file to '* in your !ro#ser. nsert thesource code !elo# into an .ht or .htl file$
// oad .&var xml new ;tive.=bXet&irosoft#.&D=&Fxml#asyn falsexml#loaddatalog#xmlF
// oad the .7var xsl new ;tive.=bXet&irosoft#.&D=&Fxsl#asyn falsexsl#loaddatalogLfilter#xslF
// Transformdo(ment#writexml#transformAodexslFF
The 4esult
f you don;t have nternet +lorer G.0 to vie# the result& this is the result of the out+ut$
Title 0rtist
+ire Burles8ue Bo! /ylan
E" XL Conditional +6
-
8/11/2019 XML_Karox
68/73
XL 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$
&sl8if testG(artistG;/ob Dylan;(""" some output """>5&sl8if
=o# take a look at your sli"htly adusted X-* stylesheet$
-
8/11/2019 XML_Karox
69/73
// oad .&var xml new ;tive.=bXet&irosoft#.&D=&Fxml#asyn falsexml#loaddatalog#xmlF
// oad the .7var xsl new ;tive.=bXet&irosoft#.&D=&Fxsl#asyn falsexsl#loaddatalogLif#xslF
// Transformdo(ment#writexml#transformAodexslFF
The 4esult
f you don;t have nternet +lorer G.0 to vie# the result& this is the result of the out+ut$
Title 0rtist
+ire Burles8ue Bo! /ylan
E"N XL Conditional Choose
XL 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$
-
8/11/2019 XML_Karox
70/73
o insert a conditional choose test a"ainst the content of the file& si+ly add the sl$choose&sl$#hen and sl$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 adusted X-* stylesheet$
-
8/11/2019 XML_Karox
71/73
// Transformdo(ment#writexml#transformAodexslFF
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 Raaotti
One ni"ht only Bee Mees
-ylvias other /r.'ook
a""ie ay Rod -te#art
Roana :ndrea Bocelli
%hen a an loves a #oan Eercy -led"e
Black an"el -ava"e Rose
1AAA Mray =oinees any
or 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
:U
10;;;;;; is a lan'ua'e for transformin' XML documents into other ty(es ofdocuments8 or into other XML documents.
a0 X+LTb0 X(ath
-
8/11/2019 XML_Karox
72/73
c0 X+L
d0
-
8/11/2019 XML_Karox
73/73