You are on page 1of 41

XML Guidelines

XML stands for EXtensible Markup Language.


Introduction to XML
XML was designed to transport and store data.
HTML was designed to display data.
What is XML?
XML stands for EXtensible Markup Language
XML is a markup language much like HTML
XML was designed to carry data, not to display data
XML tags are not predefined. You must define your own tags
XML is designed to be selfdescripti!e
XML is a "#$ %ecommendation
The Difference Between XML and HTML
XML is not a replacement for HTML.
XML and HTML were designed with different goals&
XML was designed to transport and store data, with focus on what data is.
HTML was designed to display data, with focus on how data looks.
HTML is about displaying information, while XML is about carrying information.
XML was created to structure, store, and transport information.
XML is Used to Create New Internet Languages
' lot of new (nternet languages are created with XML.
Here are some e)amples&
XHTML the latest !ersion of HTML
"*+L for describing a!ailable web ser!ices
"', and "ML as markup languages for handheld de!ices
%** languages for news feeds
%+- and ."L for describing resources and ontology
*M(L for describing multimedia for the web
An Ea!"#e XML Docu!ent
XML documents use a selfdescribing and simple synta)&
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove<to>
<!rom>"#ni<!rom>
<$e#ding>%eminder<$e#ding>
<&od'>(on)t !orget me t$is *ee+end,<&od'>
<note>
The first line is the XML declaration. (t defines the XML !ersion /0.12 and the encoding
used /(*.33450 6 Latin07"est European character set2.
The ne)t line describes the root element of the document /like saying& 8this document is
a note82&
<note>
The ne)t 9 lines describe 9 child elements of the root /to, from, heading, and body2&
<to>Tove<to>
<!rom>"#ni<!rom>
<$e#ding>%eminder<$e#ding>
<&od'>(on)t !orget me t$is *ee+end,<&od'>
'nd finally the last line defines the end of the root element&
<note>
You can assume, from this e)ample, that the XML document contains a note to To!e
from :ani.
XML Syntax Rules
The synta) rules of XML are !ery simple and logical. The rules are easy to learn, and
easy to use.

A## XML E#e!ents Must Ha$e a C#osing Tag
<->T$is is # -#r#gr#-$<->
<->T$is is #not$er -#r#gr#-$<->
XML Tags are Case %ensiti$e
XML elements are defined using XML tags.
XML tags are case sensiti!e. "ith XML, the tag ;Letter< is different from the tag
;letter<.
.pening and closing tags must be written with the same case&
<.ess#ge>T$is is incorrect<mess#ge>
<mess#ge>T$is is correct<mess#ge>
Note: 8.pening and closing tags8 are often referred to as 8*tart and end tags8. =se
whate!er you prefer. (t is e)actly the same thing.
XML E#e!ents Must &e 'ro"er#( Nested
(n HTML, you will often see improperly nested elements&
<&><i>T$is text is &old #nd it#lic<&><i>
(n XML, all elements must be properly nested within each other&
<&><i>T$is text is &old #nd it#lic<i><&>
(n the e)ample abo!e, 8,roperly nested8 simply means that since the ;i< element is
opened inside the ;b< element, it must be closed inside the ;b< element.
XML Docu!ents Must Ha$e a )oot E#e!ent
XML documents must contain one element that is the parent of all other elements. This
element is called the root element.
<root>
<c$ild>
<s/&c$ild>.....<s/&c$ild>
<c$ild>
<root>
XML Attri&ute *a#ues Must &e +uoted
XML elements can ha!e attributes in name7!alue pairs >ust like in HTML.
(n XML the attribute !alue must always be ?uoted. *tudy the two XML documents
below. The first one is incorrect, the second is correct&
<note d#te=10110001>
<to>Tove<to>
<!rom>"#ni<!rom>
<note>
<note d#te="10110001">
<to>Tove<to>
<!rom>"#ni<!rom>
<note>
The error in the first document is that the date attribute in the note element is not ?uoted.
Entit( )eferences
*ome characters ha!e a special meaning in XML.
(f you place a character like 8;8 inside an XML element, it will generate an error because
the parser interprets it as the start of a new element.
This will generate an XML error&
<mess#ge>i! s#l#r' < 1000 t$en<mess#ge>
To a!oid this error, replace the 8;8 character with an entity reference&
<mess#ge>i! s#l#r' 2lt3 1000 t$en<mess#ge>
There are 4 predefined entity references in XML&
@ltA ; less than
@gtA < greater than
@ampA @ ampersand
@aposA B apostrophe
@?uotA 8 ?uotation mark
Note: .nly the characters 8;8 and 8@8 are strictly illegal in XML. The greater than
character is legal, but it is a good habit to replace it.
Co!!ents in XML
The synta) for writing comments in XML is similar to that of HTML.
;C This is a comment <
With XML, White %"ace is 'reser$ed
HTML reduces multiple white space characters to a single white space&
HTML& Hello my name is To!e
.utput& Hello my name is To!e.
"ith XML, the white space in your document is not truncated.
XML Elements
'n XML document contains XML Elements.
What is an XML E#e!ent?
'n XML element is e!erything from /including2 the elementBs start tag to /including2 the
elementBs end tag.
'n element can contain other elements, simple te)t or a mi)ture of both. Elements can
also ha!e attributes.
<&oo+store>
<&oo+ c#tegor'="45I6(%78">
<title>5#rr' 9otter<title>
<#/t$or>" :. %o*ling<#/t$or>
<'e#r>0005<'e#r>
<-rice>09.99<-rice>
<&oo+>
<&oo+ c#tegor'=";7<">
<title>6e#rning =.6<title>
<#/t$or>7ri+ T. %#'<#/t$or>
<'e#r>000><'e#r>
<-rice>>9.95<-rice>
<&oo+>
<&oo+store>
(n the e)ample abo!e, ;bookstore< and ;book< ha!e element contents, because they
contain other elements. ;author< has text content because it contains te)t.
(n the e)ample abo!e only ;book< has an attribute /category68$H(L+%ED82.

XML Na!ing )u#es
XML elements must follow these naming rules&
Dames can contain letters, numbers, and other characters
Dames must not start with a number or punctuation character
Dames must not start with the letters )ml /or XML, or Xml, etc2
Dames cannot contain spaces
'ny name can be used, no words are reser!ed.

Best Na!ing 'ractices
Make names descripti!e. Dames with an underscore separator are nice& ;firstEname<,
;lastEname<.
Dames should be short and simple, like this& ;bookEtitle< not like this&
;theEtitleEofEtheEbook<.
'!oid 88 characters. (f you name something 8firstname,8 some software may think you
want to subtract name from first.
'!oid 8.8 characters. (f you name something 8first.name,8 some software may think that
8name8 is a property of the ob>ect 8first.8
'!oid 8&8 characters. $olons are reser!ed to be used for something called namespaces
/more later2.
XML documents often ha!e a corresponding database. ' good practice is to use the
naming rules of your database for the elements in the XML documents.
DonEnglish letters like FGH are perfectly legal in XML, but watch out for problems if
your software !endor doesnBt support them.

XML E#e!ents are Etensi&#e
XML elements can be e)tended to carry more information.
Look at the following XML e)ample&
<note>
<to>Tove<to>
<!rom>"#ni<!rom>
<&od'>(on)t !orget me t$is *ee+end,<&od'>
<note>
LetBs imagine that we created an application that e)tracted the ;to<, ;from<, and ;body<
elements from the XML document to produce this output&
MESSAGE
o: To!e
!rom: :ani
+onBt forget me this weekendC
(magine that the author of the XML document added some e)tra information to it&
<note>
<d#te>0008-01-10<d#te>
<to>Tove<to>
<!rom>"#ni<!rom>
<$e#ding>%eminder<$e#ding>
<&od'>(on)t !orget me t$is *ee+end,<&od'>
<note>
*hould the application break or crashI
Do. The application should still be able to find the ;to<, ;from<, and ;body< elements in
the XML document and produce the same output.
.ne of the beauties of XML, is that it can often be e)tended without breaking
applications.
XML Attributes
XML elements can ha!e attributes in the start tag, >ust like HTML.
'ttributes pro!ide additional information about elements.

XML Attri&utes
-rom HTML you will remember this& ;img src68computer.gif8<. The 8src8 attribute
pro!ides additional information about the ;img< element.
(n HTML /and in XML2 attributes pro!ide additional information about elements&
<img src="com-/ter.gi!">
<# $re!="demo.#s-">
'ttributes often pro!ide information that is not a part of the data. (n the e)ample below,
the file type is irrele!ant to the data, but important to the software that wants to
manipulate the element&
<!ile t'-e="gi!">com-/ter.gi!<!ile>

XML Attri&utes Must &e +uoted
'ttribute !alues must always be enclosed in ?uotes, but either single or double ?uotes can
be used. -or a personBs se), the person tag can be written like this&
<-erson sex="!em#le">
or like this&
<-erson sex=)!em#le)>
(f the attribute !alue itself contains double ?uotes you can use single ?uotes, like in this
e)ample&
<g#ngster n#me=)?eorge "S$otg/n" @iegler)>
or you can use character entities&
<g#ngster n#me="?eorge 2A/ot3S$otg/n2A/ot3 @iegler">

XML E#e!ents $s- Attri&utes
Take a look at these e)amples&
<-erson sex="!em#le">
<!irstn#me>Bnn#<!irstn#me>
<l#stn#me>Smit$<l#stn#me>
<-erson>
<-erson>
<sex>!em#le<sex>
<!irstn#me>Bnn#<!irstn#me>
<l#stn#me>Smit$<l#stn#me>
<-erson>
(n the first e)ample se) is an attribute. (n the last, se) is an element. Joth e)amples
pro!ide the same information.
There are no rules about when to use attributes and when to use elements. 'ttributes are
handy in HTML. (n XML my ad!ice is to a!oid them. =se elements instead.

M( .a$orite Wa(
The following three XML documents contain e)actly the same information&
' date attribute is used in the first e)ample&
<note d#te="10010008">
<to>Tove<to>
<!rom>"#ni<!rom>
<$e#ding>%eminder<$e#ding>
<&od'>(on)t !orget me t$is *ee+end,<&od'>
<note>
' date element is used in the second e)ample&
<note>
<d#te>10010008<d#te>
<to>Tove<to>
<!rom>"#ni<!rom>
<$e#ding>%eminder<$e#ding>
<&od'>(on)t !orget me t$is *ee+end,<&od'>
<note>
'n e)panded date element is used in the third& /TH(* (* MY -'K.%(TE2&
<note>
<d#te>
<d#'>10<d#'>
<mont$>01<mont$>
<'e#r>0008<'e#r>
<d#te>
<to>Tove<to>
<!rom>"#ni<!rom>
<$e#ding>%eminder<$e#ding>
<&od'>(on)t !orget me t$is *ee+end,<&od'>
<note>

A$oid XML Attri&utes?
*ome of the problems with using attributes are&
attributes cannot contain multiple !alues /elements can2
attributes cannot contain tree structures /elements can2
attributes are not easily e)pandable /for future changes2
'ttributes are difficult to read and maintain. =se elements for data. =se attributes for
information that is not rele!ant to the data.
+onBt end up like this&
<note d#'="10" mont$="01" 'e#r="0008"
to="Tove" !rom=""#ni" $e#ding="%eminder"
&od'="(on)t !orget me t$is *ee+end,">
<note>

XML Attri&utes for Metadata
*ometimes (+ references are assigned to elements. These (+s can be used to identify
XML elements in much the same way as the (+ attribute in HTML. This e)ample
demonstrates this&
<mess#ges>
<note id="501">
<to>Tove<to>
<!rom>"#ni<!rom>
<$e#ding>%eminder<$e#ding>
<&od'>(on)t !orget me t$is *ee+end,<&od'>
<note>
<note id="500">
<to>"#ni<to>
<!rom>Tove<!rom>
<$e#ding>%eC %eminder<$e#ding>
<&od'>I *ill not<&od'>
<note>
<mess#ges>
The (+ abo!e is >ust an identifier, to identify the different notes. (t is not a part of the note
itself.
"hat (Bm trying to say here is that metadata /data about data2 should be stored as
attributes, and that data itself should be stored as elements.
XML "alidation
XML with correct synta) is 8"ell -ormed8 XML.
XML !alidated against a +T+ is 8Kalid8 XML.

We## .or!ed XML Docu!ents
' 8"ell -ormed8 XML document has correct XML synta).
The synta) rules were described in the pre!ious chapters&
XML documents must ha!e a root element
XML elements must ha!e a closing tag
XML tags are case sensiti!e
XML elements must be properly nested
XML attribute !alues must be ?uoted
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove<to>
<!rom>"#ni<!rom>
<$e#ding>%eminder<$e#ding>
<&od'>(on)t !orget me t$is *ee+end,<&od'>
<note>

*a#id XML Docu!ents
' 8Kalid8 XML document is a 8"ell -ormed8 XML document, which also conforms to
the rules of a +ocument Type +efinition /+T+2&
<?xml version="1.0" encoding="ISO-8859-1"?>
<,(O4TD97 note SDST7. "8ote.dtd">
<note>
<to>Tove<to>
<!rom>"#ni<!rom>
<$e#ding>%eminder<$e#ding>
<&od'>(on)t !orget me t$is *ee+end,<&od'>
<note>
The +.$TY,E declaration in the e)ample abo!e, is a reference to an e)ternal +T+ file.
The content of the file is shown in the paragraph below.

XML DTD
The purpose of a +T+ is to define the structure of an XML document. (t defines the
structure with a list of legal elements&
<,(O4TD97 note E
<,767.78T note FtoG!romG$e#dingG&od'H>
<,767.78T to FI94(BTBH>
<,767.78T !rom FI94(BTBH>
<,767.78T $e#ding FI94(BTBH>
<,767.78T &od' FI94(BTBH>
J>
(f you want to study +T+, you will find our +T+ tutorial on our homepage.

XML %che!a
"#$ supports an XML based alternati!e to +T+ called XML *chema&
<xsCelement n#me="note">
<xsCcom-lexT'-e>
<xsCseA/ence>
<xsCelement n#me="to" t'-e="xsCstring">
<xsCelement n#me="!rom" t'-e="xsCstring">
<xsCelement n#me="$e#ding" t'-e="xsCstring">
<xsCelement n#me="&od'" t'-e="xsCstring">
<xsCseA/ence>
<xsCcom-lexT'-e>
<xsCelement>
(f you want to study XML *chema, you will find our *chema tutorial on our homepage.

"ie#in$ XML !iles
%aw XML files can be !iewed in all ma>or browsers.
+onBt e)pect XML files to be displayed as HTML pages.

*iewing XML .i#es
;I)ml !ersion680.18 encoding68(*.334508I<
;note<
;to<o%e;7to<
;from<&ani;7from<
;heading<Reminder;7heading<
;body<'on(t for$et me this #ee)end*;7body<
;7note<
Look at this XML file& note.)ml
The XML document will be displayed with colorcoded root and child elements. ' plus
/L2 or minus sign /2 to the left of the elements can be clicked to e)pand or collapse the
element structure. To !iew the raw XML source /without the L and signs2, select 8Kiew
,age *ource8 or 8Kiew *ource8 from the browser menu.
Note: (n Detscape, .pera, and *afari, only the element te)t will be displayed. To !iew
the raw XML, you must right click the page and select 8Kiew *ource8
'isplayin$ XML #ith +SS
"ith $** /$ascading *tyle *heets2 you can add display information to an XML
document.

Dis"#a(ing (our XML .i#es with C%%?
(t is possible to use $** to format an XML document.
Jelow is an e)ample of how to use a $** style sheet to format an XML document&
Jelow is a fraction of the XML file. The second line links the XML file to the $** file&
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-st'les$eet t'-e="textcss" $re!="cdKc#t#log.css"?>
<4BTB6O?>
<4(>
<TIT67>7m-ire </rlesA/e<TIT67>
<B%TIST><o& ('l#n<B%TIST>
<4OL8T%D>LSB<4OL8T%D>
<4O.9B8D>4ol/m&i#<4O.9B8D>
<9%I47>10.90<9%I47>
<D7B%>1985<D7B%>
<4(>
<4(>
<TIT67>5ide 'o/r $e#rt<TIT67>
<B%TIST><onnie T'ler<B%TIST>
<4OL8T%D>L:<4OL8T%D>
<4O.9B8D>4<S %ecords<4O.9B8D>
<9%I47>9.90<9%I47>
<D7B%>1988<D7B%>
<4(>
.
.
.
.
<4BTB6O?>
-ormatting XML with $** is not the most common method.
"#$ recommend using X*LT instead. *ee the ne)t chapter.
'isplayin$ XML #ith XSL
Dis"#a(ing XML with X%LT
X*LT is the recommended style sheet language of XML.
X*LT /eXtensible *tylesheet Language Transformations2 is far more sophisticated than
$**.
.ne way to use X*LT is to transform XML into HTML before it is displayed by the
browser as demonstrated in these e)amples&
Jelow is a fraction of the XML file. The second line links the XML file to the X*LT file&
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-st'les$eet t'-e="textxsl" $re!="sim-le.xsl"?>
<&re#+!#stKmen/>
<!ood>
<n#me><elgi#n ;#!!les<n#me>
<-rice>M5.95<-rice>
<descri-tion>
t*o o! o/r !#mo/s <elgi#n ;#!!les
<descri-tion>
<c#lories>N50<c#lories>
<!ood>
<&re#+!#stKmen/>
Load and parse an XML file
;html<
;head<
;script type68te)t7>a!ascript8<
function parseXML/2
M
try 77(nternet E)plorer
M
)ml+oc6new 'cti!eX.b>ect/8Microsoft.XML+.M82A
N
catch/e2
M
try 77-irefo), MoOilla, .pera, etc.
M
)ml+oc6document.implementation.create+ocument/88,88,null2A
N
catch/e2
M
alert/e.message2A
returnA
N
N
)ml+oc.async6falseA
)ml+oc.load/8note.)ml82A
document.getElementJy(d/8to82.innerHTML6)ml+oc.getElementsJyTagDame/8to82P1Q.
childDodesP1Q.nodeKalueA
document.getElementJy(d/8from82.innerHTML6)ml+oc.getElementsJyTagDame/8fro
m82P1Q.childDodesP1Q.nodeKalueA
document.getElementJy(d/8message82.innerHTML6)ml+oc.getElementsJyTagDame/8
body82P1Q.childDodesP1Q.nodeKalueA
N
;7script<
;7head<
;body onload68parseXML/28<
;h0<"#*chools (nternal Dote;7h0<
;p<;b<To&;7b< ;span id68to8<;7span<;br 7<
;b<-rom&;7b< ;span id68from8<;7span<;br 7<
;b<Message&;7b< ;span id68message8<;7span<
;7p<
;7body<
;7html<
Your %esult&
,-Schools Internal Note
o: To!e
!rom: :ani
Messa$e: +onBt forget me this weekendC
Load and parse an XML string
;html<
;head<
;script type68te)t7>a!ascript8<
function parseXML/2
M
te)t68;note<8A
te)t6te)tL8;to<To!e;7to<8A
te)t6te)tL8;from<:ani;7from<8A
te)t6te)tL8;heading<%eminder;7heading<8A
te)t6te)tL8;body<+onBt forget me this weekendC;7body<8A
te)t6te)tL8;7note<8A
try 77(nternet E)plorer
M
)ml+oc6new 'cti!eX.b>ect/8Microsoft.XML+.M82A
)ml+oc.async68false8A
)ml+oc.loadXML/te)t2A
N
catch/e2
M
try 77-irefo), MoOilla, .pera, etc.
M
parser6new +.M,arser/2A
)ml+oc6parser.parse-rom*tring/te)t,8te)t7)ml82A
N
catch/e2
M
alert/e.message2A
returnA
N
N
document.getElementJy(d/8to82.innerHTML6)ml+oc.getElementsJyTagDame/8to82P1Q.
childDodesP1Q.nodeKalueA
document.getElementJy(d/8from82.innerHTML6)ml+oc.getElementsJyTagDame/8fro
m82P1Q.childDodesP1Q.nodeKalueA
document.getElementJy(d/8message82.innerHTML6)ml+oc.getElementsJyTagDame/8
body82P1Q.childDodesP1Q.nodeKalueA
N
;7script<
;7head<
;body onload68parseXML/28<
;h0<"#*chools (nternal Dote;7h0<
;p<;b<To&;7b< ;span id68to8<;7span<;br 7<
;b<-rom&;7b< ;span id68from8<;7span<;br 7<
;b<Message&;7b< ;span id68message8<;7span<
;7p<
;7body<
;7html<
Your %esult&
,-Schools Internal Note
o: To!e
!rom: :ani
Messa$e: +onBt forget me this weekendC
Loading XML with Microsoft/s XML 'arser
MicrosoftBs XML parser is built into (nternet E)plorer 4 and higher.
The following :a!a*cript fragment loads an XML document /8note.)ml82 into the parser&
v#r xml(oc=ne* Bctive=O&OectF".icroso!t.=.6(O."H3
xml(oc.#s'nc="!#lse"3
xml(oc.lo#dF"note.xml"H3
Example explained:
The first line of the script abo!e creates an empty Microsoft XML document
ob>ect.
The second line turns off asynchroniOed loading, to make sure that the parser will
not continue e)ecution of the script before the document is fully loaded.
The third line tells the parser to load an XML document called 8note.)ml8.
The following :a!a*cript fragment loads a string called t)t into the parser&
v#r xml(oc=ne* Bctive=O&OectF".icroso!t.=.6(O."H3
xml(oc.#s'nc="!#lse"3
xml(oc.lo#d=.6FtxtH3
Note: The loadXML./ method is used for loading strings /te)t2, load./ is used for
loading files.

XML 'arser in .irefo and 0ther Browsers
The following :a!a*cript fragment loads an XML document /8note.)ml82 into the parser&
v#r xml(oc=doc/ment.im-lement#tion.cre#te(oc/mentF""G""Gn/llH3
xml(oc.#s'nc="!#lse"3
xml(oc.lo#dF"note.xml"H3
Example explained:
The first line of the script abo!e creates an empty XML document ob>ect.
The second line turns off asynchroniOed loading, to make sure that the parser will
not continue e)ecution of the script before the document is fully loaded.
The third line tells the parser to load an XML document called 8note.)ml8.
The following :a!a*cript fragment loads a string called t)t into the parser&
v#r -#rser=ne* (O.9#rserFH3
v#r doc=-#rser.-#rsePromStringFtxtG"textxml"H3
Example explained:
The first line of the script abo!e creates an empty XML document ob>ect.
The second line tells the parser to load a string called t)t.
Note: (nternet E)plorer uses the loadXML/2 method to parse an XML string, while other
browsers uses the +.M,arser ob>ect.
XML '0M
The +.M /+ocument .b>ect Model2 defines a standard way for accessing and
manipulating documents.
The XML D0M
The XML +.M /XML +ocument .b>ect Model2 defines a standard way for accessing
and manipulating XML documents.
The +.M !iews XML documents as a treestructure. 'll elements can be accessed
through the +.M tree. Their content /te)t and attributes2 can be modified or deleted, and
new elements can be created. The elements, their te)t, and their attributes are all known
as nodes.
(n the e)amples below we use the following +.M reference to get the te)t from the ;to<
element&
)ml+oc.getElementsJyTagDame/8to82P1Q.childDodesP1Q.nodeKalue
xml'oc the XML document created by the parser.
$etElements1ya$Name.2to2/345 the first ;to< element
childNodes345 the first child of the ;to< element /the te)t node2
node"alue the !alue of the node /the te)t itself2
The HTML D0M
The HTML +.M /HTML +ocument .b>ect Model2 defines a standard way for accessing
and manipulating HTML documents.
'll HTML elements can be accessed through the HTML +.M.
(n the e)amples below we use the following +.M reference to change the te)t of the
HTML element where id68to8&
document.getElementJy(d/8to82.innerHTML6
document the HTML document
$etElement1yId.2to2/ the HTML element where id68to8
inner6ML the inner te)t of the HTML element
'arsing an XML .i#e 1 A Cross &rowser Ea!"#e
The following code loads an XML document /8note.)ml82 into the XML parser&
<$tml>
<$e#d>
<scri-t t'-e="textO#v#scri-t">
!/nction -#rse=.6FH
Q
tr' Internet 7x-lorer
Q
xml(oc=ne* Bctive=O&OectF".icroso!t.=.6(O."H3
R
c#tc$FeH
Q
tr' Pire!oxG .oSill#G O-er#G etc.
Q
xml(oc=doc/ment.im-lement#tion.cre#te(oc/mentF""G""Gn/llH3
R
c#tc$FeH
Q
#lertFe.mess#geH3
ret/rn3
R
R
xml(oc.#s'nc=!#lse3
xml(oc.lo#dF"note.xml"H3
doc/ment.get7lement<'IdF"to"H.inner5T.6=
xml(oc.get7lements<'T#g8#meF"to"HE0J.c$ild8odesE0J.nodeT#l/e3
doc/ment.get7lement<'IdF"!rom"H.inner5T.6=
xml(oc.get7lements<'T#g8#meF"!rom"HE0J.c$ild8odesE0J.nodeT#l/e3
doc/ment.get7lement<'IdF"mess#ge"H.inner5T.6=
xml(oc.get7lements<'T#g8#meF"&od'"HE0J.c$ild8odesE0J.nodeT#l/e3
R
<scri-t>
<$e#d>
<&od' onlo#d="-#rse=.6FH">
<$1>;>Sc$ools Intern#l 8ote<$1>
<-><&>ToC<&> <s-#n id="to"><s-#n><&r >
<&>PromC<&> <s-#n id="!rom"><s-#n><&r >
<&>.ess#geC<&> <s-#n id="mess#ge"><s-#n>
<->
<&od'>
<$tml>
.utput&
,-Schools Internal Note
o: To!e
!rom: :ani
Messa$e: +onBt forget me this weekendC
Try it yourself

I!"ortant Note
To e)tract the te)t 8:ani8 from the XML, the synta) is&
get7lements<'T#g8#meF"!rom"HE0J.c$ild8odesE0J.nodeT#l/e
(n the XML e)ample there is only one ;from< tag, but you still ha!e to specify the array
inde) P1Q, because the XML parser method getElementsJyTagDame/2 returns an array of
all ;from< nodes.

'arsing an XML %tring 1 A Cross &rowser Ea!"#e
The following code loads and parses an XML string&
<$tml>
<$e#d>
<scri-t t'-e="textO#v#scri-t">
!/nction -#rse=.6FH
Q
text="<note>"3
text=textU"<to>Tove<to>"3
text=textU"<!rom>"#ni<!rom>"3
text=textU"<$e#ding>%eminder<$e#ding>"3
text=textU"<&od'>(on)t !orget me t$is *ee+end,<&od'>"3
text=textU"<note>"3
tr' Internet 7x-lorer
Q
xml(oc=ne* Bctive=O&OectF".icroso!t.=.6(O."H3
xml(oc.#s'nc="!#lse"3
xml(oc.lo#d=.6FtextH3
R
c#tc$FeH
Q
tr' Pire!oxG .oSill#G O-er#G etc.
Q
-#rser=ne* (O.9#rserFH3
xml(oc=-#rser.-#rsePromStringFtextG"textxml"H3
R
c#tc$FeH
Q
#lertFe.mess#geH3
ret/rn3
R
R
doc/ment.get7lement<'IdF"to"H.inner5T.6=
xml(oc.get7lements<'T#g8#meF"to"HE0J.c$ild8odesE0J.nodeT#l/e3
doc/ment.get7lement<'IdF"!rom"H.inner5T.6=
xml(oc.get7lements<'T#g8#meF"!rom"HE0J.c$ild8odesE0J.nodeT#l/e3
doc/ment.get7lement<'IdF"mess#ge"H.inner5T.6=
xml(oc.get7lements<'T#g8#meF"&od'"HE0J.c$ild8odesE0J.nodeT#l/e3
R
<scri-t>
<$e#d>
<&od' onlo#d="-#rse=.6FH">
<$1>;>Sc$ools Intern#l 8ote<$1>
<-><&>ToC<&> <s-#n id="to"><s-#n><&r >
<&>PromC<&> <s-#n id="!rom"><s-#n><&r >
<&>.ess#geC<&> <s-#n id="mess#ge"><s-#n>
<->
<&od'>
<$tml>
.utput&
,-Schools Internal Note
o: To!e
!rom: :ani
Messa$e: +onBt forget me this weekendC
XML to 6ML
This chapter e)plains how to display XML data as HTML.
Dis"#a( XML Data in HTML
(n the last chapter, we e)plained how to parse XML and access the +.M with :a!a*cript.
(n this e)ample, we loop through an XML file /cdEcatalog.)ml2, and display each $+
element as an HTML table row&
<$tml>
<&od'>
<scri-t t'-e="textO#v#scri-t">
v#r xml(oc=n/ll3
i! F*indo*.Bctive=O&OectH
Q code !or I7
xml(oc=ne* Bctive=O&OectF".icroso!t.=.6(O."H3
R
else i! Fdoc/ment.im-lement#tion.cre#te(oc/mentH
Q code !or .oSill#G Pire!oxG O-er#G etc.
xml(oc=doc/ment.im-lement#tion.cre#te(oc/mentF""G""Gn/llH3
R
else
Q
#lertF)Do/r &ro*ser c#nnot $#ndle t$is scri-t)H3
R
i! Fxml(oc,=n/llH
Q
xml(oc.#s'nc=!#lse3
xml(oc.lo#dF"cdKc#t#log.xml"H3
doc/ment.*riteF"<t#&le &order=)1)>"H3
v#r x=xml(oc.get7lements<'T#g8#meF"4("H3
!or Fi=03i<x.lengt$3iUUH
Q
doc/ment.*riteF"<tr>"H3
doc/ment.*riteF"<td>"H3
doc/ment.*riteF
xEiJ.get7lements<'T#g8#meF"B%TIST"HE0J.c$ild8odesE0J.nodeT#l/eH3
doc/ment.*riteF"<td>"H3
doc/ment.*riteF"<td>"H3
doc/ment.*riteF
xEiJ.get7lements<'T#g8#meF"TIT67"HE0J.c$ild8odesE0J.nodeT#l/eH3
doc/ment.*riteF"<td>"H3
doc/ment.*riteF"<tr>"H3
R
doc/ment.*riteF"<t#&le>"H3
R
<scri-t>
<&od'>
<$tml>
Try it yourself& +isplay XML data in an HTML table
Ea!"#e e"#ained
"e check the browser, and load the XML using the correct parser
"e create an HTML table with ;table border6808<
"e use getElementsJyTagDame/2 to get all XML $+ nodes
-or each $+ node, we display data from '%T(*T and T(TLE as table data.
"e end the table with ;7table<
-or more information about using :a!a*cript and the XML +.M, !isit our XML +.M
tutorial.
he XML6ttpRe7uest 0b8ect
The XMLHttp%e?uest ob>ect pro!ides a way to communicate with a ser!er after a web
page has loaded.

What is the XMLHtt")e2uest 0&3ect?
The XMLHttp%e?uest ob>ect is the de%eloper9s dream, because you can&
=pdate a web page with new data without reloading the page
%e?uest data from a ser!er after the page has loaded
%ecei!e data from a ser!er after the page has loaded
*end data to a ser!er in the background
The XMLHttp%e?uest ob>ect is supported in all modern browsers.
Creating an XMLHtt")e2uest 0&3ect
$reating an XMLHttp%e?uest ob>ect is done with one single line of :a!a*cript.
(n all modern browsers /including (ER2&
v#r xml$tt-=ne* =.65tt-%eA/estFH
(n (nternet E)plorer 4 and S&
v#r xml$tt-=ne* Bctive=O&OectF".icroso!t.=.65TT9"H
Example
<scri-t t'-e="textO#v#scri-t">
v#r xml$tt-3
!/nction lo#d=.6(ocF/rlH
Q
xml$tt-=n/ll3
i! F*indo*.=.65tt-%eA/estH
Q code !or #ll ne* &ro*sers
xml$tt-=ne* =.65tt-%eA/estFH3
R
else i! F*indo*.Bctive=O&OectH
Q code !or I75 #nd I7N
xml$tt-=ne* Bctive=O&OectF".icroso!t.=.65TT9"H3
R
i! Fxml$tt-,=n/llH
Q
xml$tt-.onre#d'st#tec$#nge=st#teK4$#nge3
xml$tt-.o-enF"?7T"G/rlGtr/eH3
xml$tt-.sendFn/llH3
R
else
Q
#lertF"Do/r &ro*ser does not s/--ort =.65TT9."H3
R
R
!/nction st#teK4$#ngeFH
Q
i! Fxml$tt-.re#d'St#te==VH
Q V = "lo#ded"
i! Fxml$tt-.st#t/s==000H
Q 000 = O:
...o/r code $ere...
R
else
Q
#lertF"9ro&lem retrieving =.6 d#t#"H3
R
R
R
<scri-t>
Try it yourself using :a!a*cript
Note: onreadystatechange is an e!ent handler. The !alue /stateE$hange2 is the name of a
function which is triggered when the state of the XMLHttp%e?uest ob>ect changes. *tates
run from 1 /uninitialiOed2 to 9 /complete2. .nly when the state 6 9, we can e)ecute our
code.

Wh( Use As(nc4true?
.ur e)amples use 8true8 in the third parameter of open/2.
This parameter specifies whether the re?uest should be handled asynchronously.
True means that the script continues to run after the send/2 method, without waiting for a
response from the ser!er.
The onreadystatechange e!ent complicates the code. Jut it is the safest way if you want
to pre!ent the code from stopping if you donBt get a response from the ser!er.
Jy setting the parameter to 8false8, your can a!oid the e)tra onreadystatechange code.
=se this if itBs not important to e)ecute the rest of the code if the re?uest fails.
XML Application
This chapter demonstrates a small XML application built with HTML and :a!a*cript

The XML Ea!"#e Docu!ent
Look at the following XML document /8cdEcatalog.)ml82, that represents a $+ catalog&
<?xml version="1.0" encoding="ISO-8859-1"?>
<4BTB6O?>
<4(>
<TIT67>7m-ire </rlesA/e<TIT67>
<B%TIST><o& ('l#n<B%TIST>
<4OL8T%D>LSB<4OL8T%D>
<4O.9B8D>4ol/m&i#<4O.9B8D>
<9%I47>10.90<9%I47>
<D7B%>1985<D7B%>
<4(>
.
.
... more ...
.
Load the XML Docu!ent
To load the XML document /cdEcatalog.)ml2, we use the same code as we used in the
XML ,arser chapter&
v#r xml(oc3
i! F*indo*.Bctive=O&OectH
Q code !or I7
xml(oc=ne* Bctive=O&OectF".icroso!t.=.6(O."H3
R
else i! Fdoc/ment.im-lement#tion.cre#te(oc/mentH
Q code !or Pire!oxG .oSill#G O-er#G etc.
xml(oc=doc/ment.im-lement#tion.cre#te(oc/mentF""G""Gn/llH3
R
else
Q
#lertF)Do/r &ro*ser c#nnot $#ndle t$is scri-t)H3
R
xml(oc.#s'nc=!#lse3
xml(oc.lo#dF"cdKc#t#log.xml"H3
'fter the e)ecution of this code, )ml+oc is an XML +.M ob>ect, accessible by
:a!a*cript.

Dis"#a( XML Data as an HTML Ta&#e
The following code displays an HTML table filled with data from the XML +.M ob>ect&
doc/ment.*riteF"<t#&le &order=)1)>"H3
v#r x=xml(oc.get7lements<'T#g8#meF"4("H3
!or Fv#r i=03i<x.lengt$3iUUH
Q
doc/ment.*riteF"<tr>"H3
doc/ment.*riteF"<td>"H3
doc/ment.*riteF
xEiJ.get7lements<'T#g8#meF"B%TIST"HE0J.c$ild8odesE0J.nodeT#l/eH3
doc/ment.*riteF"<td>"H3
doc/ment.*riteF"<td>"H3
doc/ment.*riteF
xEiJ.get7lements<'T#g8#meF"TIT67"HE0J.c$ild8odesE0J.nodeT#l/eH3
doc/ment.*riteF"<td>"H3
doc/ment.*riteF"<tr>"H3
R
doc/ment.*riteF"<t#&le>"H3
-or each $+ element in the XML document, a table row is created. Each table row
contains two table data cells with '%T(*T and T(TLE data from the current $+ element.
Dis"#a( XML Data in an( HTML E#e!ent
XML data can be copied into any HTML element that can display te)t.
The code below is part of the ;head< section of the HTML file. (t gets the XML data
from the first ;$+< element and displays it in the HTML element with the id68show8&
v#r x=xml(oc.get7lements<'T#g8#meF"4("H3
i=03
!/nction dis-l#'FH
Q
#rtist=
FxEiJ.get7lements<'T#g8#meF"B%TIST"HE0J.c$ild8odesE0J.nodeT#l/eH3
title=
FxEiJ.get7lements<'T#g8#meF"TIT67"HE0J.c$ild8odesE0J.nodeT#l/eH3
'e#r=
FxEiJ.get7lements<'T#g8#meF"D7B%"HE0J.c$ild8odesE0J.nodeT#l/eH3
txt="BrtistC "U#rtistU"<&r >TitleC "UtitleU"<&r >De#rC "U'e#r3
doc/ment.get7lement<'IdF"s$o*"H.inner5T.6=txt3
R
The body of the HTML document contains an onload e!entattribute that will call the
display/2 function when the page has loaded. (t also contains a :di% id;(sho#(< element
to recei!e the XML data.
<&od' onlo#d="dis-l#'FH">
<div id=)s$o*)><div>
<&od'>
Try it yourself& *ee how the XML data is displayed inside the ;di!< element.
"ith the e)ample abo!e, you will only see data from the first $+ element in the XML
document. To na!igate to the ne)t line of data, you ha!e to add some more code.

Add a Na$igation %cri"t
To add na!igation to the e)ample abo!e, create two functions called ne)t/2 and
pre!ious/2&
!/nction nextFH
Q
i! Fi<x.lengt$-1H
Q
iUU3
dis-l#'FH3
R
R
!/nction -revio/sFH
Q
i! Fi>0H
Q
i--3
dis-l#'FH3
R
R
The ne)t/2 function makes sure that nothing is displayed if you already are at the last $+
element, and the pre!ious /2 function makes sure that nothing is displayed if you already
are at the first $+ element.
The ne)t/2 and pre!ious/2 functions are called by clicking ne)t7pre!ious buttons&
<in-/t t'-e="&/tton" onclic+="-revio/sFH" v#l/e="-revio/s" >
<in-/t t'-e="&/tton" onclic+="nextFH" v#l/e="next" >
XML Namespaces
XML Damespaces pro!ide a method to a!oid element name conflicts.

Na!e Conf#icts
(n XML, element names are defined by the de!eloper. This often results in a conflict
when trying to mi) XML documents from different XML applications.
This XML carries HTML table information&
<t#&le>
<tr>
<td>B--les<td>
<td><#n#n#s<td>
<tr>
<t#&le>
This XML carries information about a table /a piece of furniture2&
<t#&le>
<n#me>B!ric#n 4o!!ee T#&le<n#me>
<*idt$>80<*idt$>
<lengt$>100<lengt$>
<t#&le>
(f these XML fragments were added together, there would be a name conflict. Joth
contain a ;table< element, but the elements ha!e different content and meaning.
'n XML parser will not know how to handle these differences.

%o#$ing the Na!e Conf#ict Using a 'refi
Dame conflicts in XML can easily be a!oided using a name prefi).
This XML carries information about an HTML table, and a piece of furniture&
<$Ct#&le>
<$Ctr>
<$Ctd>B--les<$Ctd>
<$Ctd><#n#n#s<$Ctd>
<$Ctr>
<$Ct#&le>
<!Ct#&le>
<!Cn#me>B!ric#n 4o!!ee T#&le<!Cn#me>
<!C*idt$>80<!C*idt$>
<!Clengt$>100<!Clengt$>
<!Ct#&le>
(n the e)ample abo!e, there will be no conflict because the two ;table< elements ha!e
different names.

XML Na!es"aces 1 The !#ns Attri&ute
"hen using prefi)es in XML, a socalled namespace for the prefi) must be defined.
The namespace is defined by the xmlns attribute in the start tag of an element.
The namespace declaration has the following synta). )mlns&prefix68URI8.
<root>
<$Ct#&le xmlnsC$="$tt-C***.*>.orgT%$tmlV">
<$Ctr>
<$Ctd>B--les<$Ctd>
<$Ctd><#n#n#s<$Ctd>
<$Ctr>
<$Ct#&le>
<!Ct#&le xmlnsC!="$tt-C***.*>sc$ools.com!/rnit/re">
<!Cn#me>B!ric#n 4o!!ee T#&le<!Cn#me>
<!C*idt$>80<!C*idt$>
<!Clengt$>100<!Clengt$>
<!Ct#&le>
<root>
(n the e)ample abo!e, the )mlns attribute in the ;table< tag gi!e the h& and f& prefi)es a
?ualified namespace.
"hen a namespace is defined for an element, all child elements with the same prefi) are
associated with the same namespace.
Damespaces can be declared in the elements where they are used or in the XML root
element&
<root
xmlnsC$="$tt-C***.*>.orgT%$tmlV"
xmlnsC!="$tt-C***.*>sc$ools.com!/rnit/re">
<$Ct#&le>
<$Ctr>
<$Ctd>B--les<$Ctd>
<$Ctd><#n#n#s<$Ctd>
<$Ctr>
<$Ct#&le>
<!Ct#&le>
<!Cn#me>B!ric#n 4o!!ee T#&le<!Cn#me>
<!C*idt$>80<!C*idt$>
<!Clengt$>100<!Clengt$>
<!Ct#&le>
<root>
Note: The namespace =%( is not used by the parser to look up information.
The purpose is to gi!e the namespace a uni?ue name. Howe!er, often companies use the
namespace as a pointer to a web page containing namespace information.
Unifor! )esource Identifier 5U)I6
' =niform Resource Identifier /=%(2 is a string of characters which identifies an
(nternet %esource.
The most common =%( is the =niform Resource Locator /=%L2 which identifies an
(nternet domain address. 'nother, not so common type of =%( is the =ni%ersal Resource
Name /=%D2.
(n our e)amples we will only use =%Ls.

Defau#t Na!es"aces
+efining a default namespace for an element sa!es us from using prefi)es in all the child
elements. (t has the following synta)&
xmlns="namespaceURI"
This XML carries HTML table information&
<t#&le xmlns="$tt-C***.*>.orgT%$tmlV">
<tr>
<td>B--les<td>
<td><#n#n#s<td>
<tr>
<t#&le>
This XML carries information about a piece of furniture&
<t#&le xmlns="$tt-C***.*>sc$ools.com!/rnit/re">
<n#me>B!ric#n 4o!!ee T#&le<n#me>
<*idt$>80<*idt$>
<lengt$>100<lengt$>
<t#&le>

Na!es"aces in )ea# Use
X*LT is an XML language that can be used to transform XML documents into other
formats, like HTML.
(n the X*LT document below, you can see that most of the tags are HTML tags.
The tags that are not HTML tags ha!e the prefi) )sl, identified by the namespace
)mlns&)sl68http&77www.w#.org705557X*L7Transform8&
<?xml version="1.0" encoding="ISO-8859-1"?>
<xslCst'les$eet version="1.0"
xmlnsCxsl="$tt-C***.*>.org1999=S6Tr#ns!orm">
<xslCtem-l#te m#tc$="">
<$tml>
<&od'>
<$0>.' 4( 4ollection<$0>
<t#&le &order="1">
<tr>
<t$ #lign="le!t">Title<t$>
<t$ #lign="le!t">Brtist<t$>
<tr>
<xslC!or-e#c$ select="c#t#logcd">
<tr>
<td><xslCv#l/e-o! select="title"><td>
<td><xslCv#l/e-o! select="#rtist"><td>
<tr>
<xslC!or-e#c$>
<t#&le>
<&od'>
<$tml>
<xslCtem-l#te>
<xslCst'les$eet>
XML +'AA
'll te)t in an XML document will be parsed by the parser.
Jut te)t inside a $+'T' section will be ignored by the parser.

'CDATA 1 'arsed Character Data
XML parsers normally parse all the te)t in an XML document.
"hen an XML element is parsed, the te)t between the XML tags is also parsed&
<mess#ge>T$is text is #lso -#rsed<mess#ge>
The parser does this because XML elements can contain other elements, as in this
e)ample, where the ;name< element contains two other elements /first and last2&
<n#me><!irst><ill<!irst><l#st>?#tes<l#st><n#me>
and the parser will break it up into subelements like this&
<n#me>
<!irst><ill<!irst>
<l#st>?#tes<l#st>
<n#me>
,arsed $haracter +ata /,$+'T'2 is a term used about te)t data that will be parsed by the
XML parser.

CDATA 1 5Un"arsed6 Character Data
The term $+'T' is used about te)t data that should not be parsed by the XML parser.
$haracters like 8;8 and 8@8 are illegal in XML elements.
8;8 will generate an error because the parser interprets it as the start of a new element.
8@8 will generate an error because the parser interprets it as the start of an character
entity.
*ome te)t, like :a!a*cript code, contains a lot of 8;8 or 8@8 characters. To a!oid errors
script code can be defined as $+'T'.
E!erything inside a $+'T' section is ignored by the parser.
' $+'T' section starts with 8:*3+'AA38 and ends with 855<8&
<scri-t>
<,E4(BTBE
!/nction m#tc$*oF#G&H
Q
i! F# < & 22 # < 0H t$en
Q
ret/rn 13
R
else
Q
ret/rn 03
R
R
JJ>
<scri-t>
(n the e)ample abo!e, e!erything inside the $+'T' section is ignored by the parser.
Notes on +'AA sections:
' $+'T' section cannot contain the string 8QQ<8. Dested $+'T' sections are not
allowed.
The 8QQ<8 that marks the end of the $+'T' section cannot contain spaces or line breaks.
XML on the Ser%er
XML files are plain te)t files >ust like HTML files.
XML can easily be stored and generated by a standard web ser!er.

%toring XML .i#es on the %er$er
XML files can be stored on an (nternet ser!er e)actly the same way as HTML files.
*tart "indows Dotepad and write the following lines&
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<!rom>"#ni<!rom>
<to>Tove<to>
<mess#ge>%emem&er me t$is *ee+end<mess#ge>
<note>
*a!e the file on your web ser!er with a proper name like 8note.)ml8.

7enerating XML with A%'
XML can be generated on a ser!er without any installed XML software.
To generate an XML response from the ser!er simply write the following code and sa!e
it as an '*, file on the web ser!er&
<W
res-onse.4ontentT'-e="textxml"
res-onse.;riteF"<?xml version=)1.0) encoding=)ISO-8859-1)?>"H
res-onse.;riteF"<note>"H
res-onse.;riteF"<!rom>"#ni<!rom>"H
res-onse.;riteF"<to>Tove<to>"H
res-onse.;riteF"<mess#ge>%emem&er me t$is *ee+end<mess#ge>"H
res-onse.;riteF"<note>"H
W>
Dote that the content type of the response must be set to 8te)t7)ml8.

7enerating XML with 'H'
To generate an XML response from the ser!er using ,H,, use following code&
<?-$-
$e#derF"4ontent-t'-eC textxml"H3
ec$o "<?xml version=)1.0) encoding=)ISO-8859-1)?>"3
ec$o "<note>"3
ec$o "<!rom>"#ni<!rom>"3
ec$o "<to>Tove<to>"3
ec$o "<mess#ge>%emem&er me t$is *ee+end<mess#ge>"3
ec$o "<note>"3
?>
Dote that the content type of the response header must be set to 8te)t7)ml8.

7enerating XML .ro! a Data&ase
XML can be generated from a database without any installed XML software.
To generate an XML database response from the ser!er, simply write the following code
and sa!e it as an '*, file on the web ser!er&
<W
res-onse.4ontentT'-e = "textxml"
set conn=Server.4re#teO&OectF"B(O(<.4onnection"H
conn.-rovider=".icroso!t."et.O67(<.V.03"
conn.o-en server.m#--#t$F"d&d#t#&#se.md&"H
sAl="select !n#meGln#me !rom t&l?/est<oo+"
set rs=4onn.7xec/teFsAlH
res-onse.*riteF"<?xml version=)1.0) encoding=)ISO-8859-1)?>"H
res-onse.*riteF"<g/est&oo+>"H
*$ile Fnot rs.7OPH
res-onse.*riteF"<g/est>"H
res-onse.*riteF"<!n#me>" 2 rsF"!n#me"H 2 "<!n#me>"H
res-onse.*riteF"<ln#me>" 2 rsF"ln#me"H 2 "<ln#me>"H
res-onse.*riteF"<g/est>"H
rs..ove8extFH
*end
rs.closeFH
conn.closeFH
res-onse.*riteF"<g/est&oo+>"H
W>
The e)ample abo!e uses '*, with '+..
(f you want to study '*, and '+., you will find the tutorials on our homepage.

Transfor!ing XML with X%LT on the %er$er
This '*, transforms an XML file to XHTML on the ser!er&
<W
)6o#d =.6
set xml = Server.4re#teO&OectF".icroso!t.=.6(O."H
xml.#s'nc = !#lse
xml.lo#dFServer..#-9#t$F"sim-le.xml"HH
)6o#d =S6
set xsl = Server.4re#teO&OectF".icroso!t.=.6(O."H
xsl.#s'nc = !#lse
xsl.lo#dFServer..#-9#t$F"sim-le.xsl"HH
)Tr#ns!orm !ile
%es-onse.;riteFxml.tr#ns!orm8odeFxslHH
W>
E)ample e)plained
The first block of code creates an instance of the Microsoft XML parser
/XML+.M2, and loads the XML file into memory.
The second block of code creates another instance of the parser and loads the X*L
file into memory.
The last line of code transforms the XML document using the X*L document, and
sends the result as XHTML to your browser. DiceC

%a$ing XML To a .i#e Using A%'
This '*, e)ample creates a simple XML document and sa!es it on the ser!er&
<W
text="<note>"
text=text 2 "<to>Tove<to>"
text=text 2 "<!rom>"#ni<!rom>"
text=text 2 "<$e#ding>%eminder<$e#ding>"
text=text 2 "<&od'>(on)t !orget me t$is *ee+end,<&od'>"
text=text 2 "<note>"
set xml(oc=Server.4re#teO&OectF".icroso!t.=.6(O."H
xml(oc.#s'nc="!#lse"
xml(oc.lo#d=.6FtextH
xml(oc.S#veF"test.xml"H
W>
XML '0M Ad%anced
The XML +.M /+ocument .b>ect Model2 defines a standard way for accessing and
manipulating XML documents.

The XML D0M
The +.M !iews XML documents as a treestructure. 'll elements can be accessed
through the +.M tree. Their content /te)t and attributes2 can be modified or deleted, and
new elements can be created. The elements, their te)t, and their attributes are all known
as nodes.
7et the *a#ue of an E#e!ent
The following code retrie!es the te)t !alue of the first ;title< element&
x=xml(oc.get7lements<'T#g8#meF"title"HE0J.c$ild8odesE0J3
txt=x.nodeT#l/e3
%esult& t)t 6 8E!eryday (talian8
7et the *a#ue of an Attri&ute
The following code retrie!es the te)t !alue of the 8lang8 attribute of the first ;title<
element&
txt=xml(oc.get7lements<'T#g8#meF"title"HE0J.getBttri&/teF"l#ng"H3
%esult& t)t 6 8en8
Change the *a#ue of an E#e!ent
The following code changes the te)t !alue of the first ;title< element&
x=xml(oc.get7lements<'T#g8#meF"title"HE0J.c$ild8odesE0J3
x.nodeT#l/e="7#s' 4oo+ing"3
Change the *a#ue of an Attri&ute
The set'ttribute/2 method can be used to change the !alue of an e)isting attribute, or to
create a new attribute.
The following code adds a new attribute called 8edition8 /with the !alue 8first82 to each
;book< element&
x=xml(oc.get7lements<'T#g8#meF"&oo+"H3
!orFi=03i<x.lengt$3iUUH
Q
xEiJ.setBttri&/teF"edition"G"!irst"H3
R
Create an E#e!ent
The createElement/2 method creates a new element node.
The createTe)tDode/2 method creates a new te)t node.
The append$hild/2 method adds a child node to a node /after the last child2.
To create a new element with te)t content, it is necessary to create both an element node
and a te)t node.
The following code creates an element /;edition<2, and adds it to the first ;book<
element&
ne*el=xml(oc.cre#te7lementF"edition"H3
ne*text=xml(oc.cre#teText8odeF"Pirst"H3
ne*el.#--end4$ildFne*textH3
x=xml(oc.get7lements<'T#g8#meF"&oo+"H3
xE0J.#--end4$ildFne*elH3
E)ample e)plained&
$reate an ;edition< element
$reate a te)t node with !alue 6 8-irst8
'ppend the te)t node to the ;edition< element
'ppend the ;edition< element to the first ;book< element
)e!o$e an E#e!ent
The remo!e$hild/2 method remo!es a specified node /or element2.
The following code fragment will remo!e the first node in the first ;book< element&
x=xml(oc.get7lements<'T#g8#meF"&oo+"HE0J3
x.remove4$ildFx.c$ild8odesE0JH3
XML Examples
These e)amples demonstrate XML files, XML formatting and XML transformation
/X*LT2.
They also demonstrate :a!a*cript used together with XML /':'X2.

"ie#in$ XML !iles
<?xml version="1.0" encoding="ISO-8859-1"?>
<,-- 7dited &' =.6S-'X -->
<note>
<to>Tove<to>
<!rom>"#ni<!rom>
<$e#ding>%eminder<$e#ding>
<&od'>(on)t !orget me t$is *ee+end,<&od'>
<note>
XML #ith +SS
Kiew an XML $+ catalog
<?xml version="1.0" encoding="ISO-8859-1"?>
<,-- 7dited &' =.6S-'X -->
<4BTB6O?>
<4(>
<TIT67>7m-ire </rlesA/e<TIT67>
<B%TIST><o& ('l#n<B%TIST>
<4OL8T%D>LSB<4OL8T%D>
<4O.9B8D>4ol/m&i#<4O.9B8D>
<9%I47>10.90<9%I47>
<D7B%>1985<D7B%>
<4(>
<4(>
<TIT67>5ide 'o/r $e#rt<TIT67>
<B%TIST><onnie T'ler<B%TIST>
<4OL8T%D>L:<4OL8T%D>
<4O.9B8D>4<S %ecords<4O.9B8D>
<9%I47>9.90<9%I47>
<D7B%>1988<D7B%>
<4(>
<4(>
<TIT67>?re#test 5its<TIT67>
<B%TIST>(oll' 9#rton<B%TIST>
<4OL8T%D>LSB<4OL8T%D>
<4O.9B8D>%4B<4O.9B8D>
<9%I47>9.90<9%I47>
<D7B%>1980<D7B%>
<4(>
<4(>
<TIT67>Still got t$e &l/es<TIT67>
<B%TIST>?#r' .oore<B%TIST>
<4OL8T%D>L:<4OL8T%D>
<4O.9B8D>Tirgin records<4O.9B8D>
<9%I47>10.00<9%I47>
<D7B%>1990<D7B%>
<4(>
<4(>
<TIT67>7ros<TIT67>
<B%TIST>7ros %#m#SSotti<B%TIST>
<4OL8T%D>7L<4OL8T%D>
<4O.9B8D><.?<4O.9B8D>
<9%I47>9.90<9%I47>
<D7B%>1991<D7B%>
<4(>
<4(>
<TIT67>One nig$t onl'<TIT67>
<B%TIST><ee ?ees<B%TIST>
<4OL8T%D>L:<4OL8T%D>
<4O.9B8D>9ol'dor<4O.9B8D>
<9%I47>10.90<9%I47>
<D7B%>1998<D7B%>
<4(>
<4(>
<TIT67>S'lvi#s .ot$er<TIT67>
<B%TIST>(r.5oo+<B%TIST>
<4OL8T%D>L:<4OL8T%D>
<4O.9B8D>4<S<4O.9B8D>
<9%I47>8.10<9%I47>
<D7B%>191><D7B%>
<4(>
<4(>
<TIT67>.#ggie .#'<TIT67>
<B%TIST>%od Ste*#rt<B%TIST>
<4OL8T%D>L:<4OL8T%D>
<4O.9B8D>9ic+*ic+<4O.9B8D>
<9%I47>8.50<9%I47>
<D7B%>1990<D7B%>
<4(>
<4(>
<TIT67>%om#nS#<TIT67>
<B%TIST>Bndre# <ocelli<B%TIST>
<4OL8T%D>7L<4OL8T%D>
<4O.9B8D>9ol'dor<4O.9B8D>
<9%I47>10.80<9%I47>
<D7B%>199N<D7B%>
<4(>
<4(>
<TIT67>;$en # m#n loves # *om#n<TIT67>
<B%TIST>9erc' Sledge<B%TIST>
<4OL8T%D>LSB<4OL8T%D>
<4O.9B8D>Btl#ntic<4O.9B8D>
<9%I47>8.10<9%I47>
<D7B%>1981<D7B%>
<4(>
<4(>
<TIT67><l#c+ #ngel<TIT67>
<B%TIST>S#v#ge %ose<B%TIST>
<4OL8T%D>7L<4OL8T%D>
<4O.9B8D>.eg#<4O.9B8D>
<9%I47>10.90<9%I47>
<D7B%>1995<D7B%>
<4(>
<4(>
<TIT67>1999 ?r#mm' 8ominees<TIT67>
<B%TIST>.#n'<B%TIST>
<4OL8T%D>LSB<4OL8T%D>
<4O.9B8D>?r#mm'<4O.9B8D>
<9%I47>10.00<9%I47>
<D7B%>1999<D7B%>
<4(>
<4(>
<TIT67>Por t$e good times<TIT67>
<B%TIST>:enn' %ogers<B%TIST>
<4OL8T%D>L:<4OL8T%D>
<4O.9B8D>./ci+ .#ster<4O.9B8D>
<9%I47>8.10<9%I47>
<D7B%>1995<D7B%>
<4(>
<4(>
<TIT67><ig ;illie st'le<TIT67>
<B%TIST>;ill Smit$<B%TIST>
<4OL8T%D>LSB<4OL8T%D>
<4O.9B8D>4ol/m&i#<4O.9B8D>
<9%I47>9.90<9%I47>
<D7B%>1991<D7B%>
<4(>
<4(>
<TIT67>T/-elo 5one'<TIT67>
<B%TIST>T#n .orrison<B%TIST>
<4OL8T%D>L:<4OL8T%D>
<4O.9B8D>9ol'dor<4O.9B8D>
<9%I47>8.00<9%I47>
<D7B%>1911<D7B%>
<4(>
<4(>
<TIT67>So/lsville<TIT67>
<B%TIST>"orn 5oel<B%TIST>
<4OL8T%D>8or*#'<4OL8T%D>
<4O.9B8D>;7B<4O.9B8D>
<9%I47>1.90<9%I47>
<D7B%>199N<D7B%>
<4(>
<4(>
<TIT67>T$e ver' &est o!<TIT67>
<B%TIST>4#t Stevens<B%TIST>
<4OL8T%D>L:<4OL8T%D>
<4O.9B8D>Isl#nd<4O.9B8D>
<9%I47>8.90<9%I47>
<D7B%>1990<D7B%>
<4(>
<4(>
<TIT67>Sto-<TIT67>
<B%TIST>S#m <ro*n<B%TIST>
<4OL8T%D>L:<4OL8T%D>
<4O.9B8D>B #nd .<4O.9B8D>
<9%I47>8.90<9%I47>
<D7B%>1988<D7B%>
<4(>
<4(>
<TIT67><ridge o! S-ies<TIT67>
<B%TIST>T)9#/<B%TIST>
<4OL8T%D>L:<4OL8T%D>
<4O.9B8D>Siren<4O.9B8D>
<9%I47>1.90<9%I47>
<D7B%>1981<D7B%>
<4(>
<4(>
<TIT67>9riv#te (#ncer<TIT67>
<B%TIST>Tin# T/rner<B%TIST>
<4OL8T%D>L:<4OL8T%D>
<4O.9B8D>4#-itol<4O.9B8D>
<9%I47>8.90<9%I47>
<D7B%>198><D7B%>
<4(>
<4(>
<TIT67>.idt om n#tten<TIT67>
<B%TIST>:im 6#rsen<B%TIST>
<4OL8T%D>7L<4OL8T%D>
<4O.9B8D>.edle'<4O.9B8D>
<9%I47>1.80<9%I47>
<D7B%>198><D7B%>
<4(>
<4(>
<TIT67>9#v#rotti ?#l# 4oncert<TIT67>
<B%TIST>6/ci#no 9#v#rotti<B%TIST>
<4OL8T%D>L:<4OL8T%D>
<4O.9B8D>(744B<4O.9B8D>
<9%I47>9.90<9%I47>
<D7B%>1991<D7B%>
<4(>
<4(>
<TIT67>T$e doc+ o! t$e &#'<TIT67>
<B%TIST>Otis %edding<B%TIST>
<4OL8T%D>LSB<4OL8T%D>
<4O.9B8D>Btl#ntic<4O.9B8D>
<9%I47>1.90<9%I47>
<D7B%>1981<D7B%>
<4(>
<4(>
<TIT67>9ict/re &oo+<TIT67>
<B%TIST>Sim-l' %ed<B%TIST>
<4OL8T%D>7L<4OL8T%D>
<4O.9B8D>7le+tr#<4O.9B8D>
<9%I47>1.00<9%I47>
<D7B%>1985<D7B%>
<4(>
<4(>
<TIT67>%ed<TIT67>
<B%TIST>T$e 4omm/n#rds<B%TIST>
<4OL8T%D>L:<4OL8T%D>
<4O.9B8D>6ondon<4O.9B8D>
<9%I47>1.80<9%I47>
<D7B%>1981<D7B%>
<4(>
<4(>
<TIT67>Lnc$#in m' $e#rt<TIT67>
<B%TIST>"oe 4oc+er<B%TIST>
<4OL8T%D>LSB<4OL8T%D>
<4O.9B8D>7.I<4O.9B8D>
<9%I47>8.00<9%I47>
<D7B%>1981<D7B%>
<4(>
<4BTB6O?>
Kiew the corresponding $** file
4BTB6O?
Q
&#c+gro/nd-colorC I!!!!!!3
*idt$C 100W3
R
4(
Q
dis-l#'C &loc+3
m#rgin-&ottomC >0-t3
m#rgin-le!tC 03
R
TIT67
Q
colorC IPP00003
!ont-siSeC 00-t3
R
B%TIST
Q
colorC I0000PP3
!ont-siSeC 00-t3
R
4OL8T%DG9%I47GD7B%G4O.9B8D
Q
dis-l#'C &loc+3
colorC I0000003
m#rgin-le!tC 00-t3
R
XML #ith XSL
Kiew an XML food menu
<?xml version="1.0" encoding="ISO-8859-1"?>
<,-- 7dited &' =.6S-'X -->
<&re#+!#stKmen/>
<!ood>
<n#me><elgi#n ;#!!les<n#me>
<-rice>M5.95<-rice>
<descri-tion>t*o o! o/r !#mo/s <elgi#n ;#!!les *it$
-lent' o! re#l m#-le s'r/-<descri-tion>
<c#lories>N50<c#lories>
<!ood>
<!ood>
<n#me>Str#*&err' <elgi#n ;#!!les<n#me>
<-rice>M1.95<-rice>
<descri-tion>lig$t <elgi#n *#!!les covered *it$
str#*&erries #nd *$i--ed cre#m<descri-tion>
<c#lories>900<c#lories>
<!ood>
<!ood>
<n#me><err'-<err' <elgi#n ;#!!les<n#me>
<-rice>M8.95<-rice>
<descri-tion>lig$t <elgi#n *#!!les covered *it$ #n
#ssortment o! !res$ &erries #nd *$i--ed cre#m<descri-tion>
<c#lories>900<c#lories>
<!ood>
<!ood>
<n#me>Prenc$ To#st<n#me>
<-rice>MV.50<-rice>
<descri-tion>t$ic+ slices m#de !rom o/r $omem#de
so/rdo/g$ &re#d<descri-tion>
<c#lories>N00<c#lories>
<!ood>
<!ood>
<n#me>5omest'le <re#+!#st<n#me>
<-rice>MN.95<-rice>
<descri-tion>t*o eggsG &#con or s#/s#geG to#stG #nd o/r
ever--o-/l#r $#s$ &ro*ns<descri-tion>
<c#lories>950<c#lories>
<!ood>
<&re#+!#stKmen/>
Kiew the corresponding X*LT stylesheet
<?xml version="1.0" encoding="ISO-8859-1"?>
<,-- 7dited &' =.6S-'X -->
<$tml xslCversion="1.0"
xmlnsCxsl="$tt-C***.*>.org1999=S6Tr#ns!orm"
xmlns="$tt-C***.*>.org1999x$tml">
<&od' st'le="!ont-!#mil'CBri#l3!ont-siSeC10-t3&#c+gro/nd-
colorCI777777">
<xslC!or-e#c$ select="&re#+!#stKmen/!ood">
<div st'le="&#c+gro/nd-colorCte#l3colorC*$ite3-#ddingCV-x">
<s-#n st'le="!ont-*eig$tC&old"><xslCv#l/e-o!
select="n#me"><s-#n>
- <xslCv#l/e-o! select="-rice">
<div>
<div st'le="m#rgin-le!tC00-x3m#rgin-&ottomC1em3!ont-siSeC10-t">
<xslCv#l/e-o! select="descri-tion">
<s-#n st'le="!ont-st'leCit#lic">
<xslCv#l/e-o! select="c#lories"> Fc#lories -er servingH
<s-#n>
<div>
<xslC!or-e#c$>
<&od'>
<$tml>
EN'

You might also like