**
In our XML tutorial you will learn what XML is and the difference between XML and HTML. You will also learn how to start using XML in your applications.
**
Introduction to XML
XML was designed to describe data and to focus on what data is.
HTML was designed to display data and to focus on how data looks.
What you should already know
Before you continue, you should have some basic understanding of the following:
- WWW, HTML and the basics of building Web pages
- Web scripting languages like JavaScript or VBScript
If you want to study these subjects first, before you start reading about XML, you can find the tutorials you need at W3Schools' Home Page .
What is XML?
- XML stands for E X tensible M arkup L anguage
- XML is a markup language much like HTML
- XML was designed to describe data
- XML tags are not predefined. You must define your own tags
- XML uses a Document Type Definition (DTD) or an XML Schema to describe the data
- XML with a DTD or XML Schema is designed to be self-descriptive
The main difference between XML and HTML
XML was designed to carry data.
XML is not a replacement for HTML.
XML and HTML were designed with different goals:
XML was designed to describe data and to focus on what data is.
HTML was designed to display data and to focus on how data looks.
HTML is about displaying information, while XML is about describing information.
XML does not DO anything
XML was not designed to DO anything.
Maybe it is a little hard to understand, but XML does not DO anything. XML was created to structure, store and to send information.
The following example is a note to Tove from Jani, stored as XML:
1<note>
2<to>Tove</to>
3<from>Jani</from>
4<heading>Reminder</heading>
5<body>Don't forget me this weekend!</body>
6</note>
The note has a header and a message body. It also has sender and receiver information. But still, this XML document does not DO anything. It is just pure information wrapped in XML tags. Someone must write a piece of software to send, receive or display it.
XML is free and extensible
XML tags are not predefined. You must "invent" your own tags.
The tags used to mark up HTML documents and the structure of HTML documents are predefined. The author of HTML documents can only use tags that are defined in the HTML standard (like
1<p>, <h1>, etc.).
2
3XML allows the author to define his own tags and his own document structure.
4
5The tags in the example above (like <to> and <from>) are not defined in any XML standard. These tags are "invented" by the author of the XML document.
6
7* * *
8
9## XML is a complement to HTML
10
11**XML is not a replacement for HTML.**
12
13It is important to understand that XML is not a replacement for HTML. In future Web development it is most likely that XML will be used to describe the data, while HTML will be used to format and display the same data.
14
15My best description of XML is this: **XML is a cross-platform, software and hardware independent tool for transmitting information.**
16
17* * *
18
19## XML in future Web development
20
21**XML is going to be everywhere.**
22
23We have been participating in XML development since its creation. It has been amazing to see how quickly the XML standard has been developed and how quickly a large number of software vendors have adopted the standard.
24
25We strongly believe that XML will be as important to the future of the Web as HTML has been to the foundation of the Web and that XML will be the most common tool for all data manipulation and data transmission.
26
27* * *
28
29## XML Joke
30
31Question: When should I use XML?
32
33Answer: When you need a buzzword in your resume.
34
35* * *
36
37
38
39
40# How can XML be Used?
41
42 
43
44* * *
45
46It is important to understand that XML was designed to store, carry, and exchange data. XML was not designed to display data.
47
48* * *
49
50## XML can Separate Data from HTML
51
52**With XML, your data is stored outside your HTML.**
53
54When HTML is used to display data, the data is stored inside your HTML. With XML, data can be stored in separate XML files. This way you can concentrate on using HTML for data layout and display, and be sure that changes in the underlying data will not require any changes to your HTML.
55
56XML data can also be stored inside HTML pages as "Data Islands". You can still concentrate on using HTML only for formatting and displaying the data.
57
58* * *
59
60## XML is used to Exchange Data
61
62**With XML, data can be exchanged between incompatible systems.**
63
64In the real world, computer systems and databases contain data in incompatible formats. One of the most time-consuming challenges for developers has been to exchange data between such systems over the Internet.
65
66Converting the data to XML can greatly reduce this complexity and create data that can be read by many different types of applications.
67
68* * *
69
70## XML and B2B
71
72**With XML, financial information can be exchanged over the Internet.**
73
74Expect to see a lot about XML and B2B (Business To Business) in the near future.
75
76XML is going to be the main language for exchanging financial information between businesses over the Internet. A lot of interesting B2B applications are under development.
77
78* * *
79
80## XML can be used to Share Data
81
82**With XML, plain text files can be used to share data.**
83
84Since XML data is stored in plain text format, XML provides a software- and hardware-independent way of sharing data.
85
86This makes it much easier to create data that different applications can work with. It also makes it easier to expand or upgrade a system to new operating systems, servers, applications, and new browsers.
87
88* * *
89
90## XML can be used to Store Data
91
92**With XML, plain text files can be used to store data.**
93
94XML can also be used to store data in files or in databases. Applications can be written to store and retrieve information from the store, and generic applications can be used to display the data.
95
96* * *
97
98## XML can make your Data more Useful
99
100**With XML, your data is available to more users.**
101
102Since XML is independent of hardware, software and application, you can make your data available to other than only standard HTML browsers.
103
104Other clients and applications can access your XML files as data sources, like they are accessing databases. Your data can be made available to all kinds of "reading machines" (agents), and it is easier to make your data available for blind people, or people with other disabilities.
105
106* * *
107
108## XML can be used to Create new Languages
109
110**XML is the mother of WAP and WML.**
111
112The Wireless Markup Language (WML), used to markup Internet applications for handheld devices like mobile phones, is written in XML.
113
114You can read more about WML in our WML tutorial .
115
116* * *
117
118## If Developers have Sense
119
120**If they DO have sense, all future applications will exchange their data in XML.**
121
122The future might give us word processors, spreadsheet applications and databases that can read each other's data in a pure text format, without any conversion utilities in between.
123
124We can only pray that Microsoft and all the other software vendors will agree.
125
126* * *
127
128
129
130
131# XML Syntax
132
133 
134
135* * *
136
137The syntax rules of XML are very simple and very strict. The rules are very easy to learn, and very easy to use.
138
139Because of this, creating software that can read and manipulate XML is very easy to do.
140
141* * *
142
143## An example XML document
144
145**XML documents use a self-describing and simple syntax.**
146
147
148 <?xml version="1.0" encoding="ISO-8859-1"?>
149<note>
150<to>Tove</to>
151<from>Jani</from>
152<heading>Reminder</heading>
153<body>Don't forget me this weekend!</body>
154</note>
155
156---
157
158The first line in the document - the XML declaration - defines the XML version and the character encoding used in the document. In this case the document conforms to the 1.0 specification of XML and uses the ISO-8859-1 (Latin-1/West European) character set.
159
160The next line describes the root element of the document (like it was saying: "this document is a note"):
161
162
163 <note>
164
165---
166
167The next 4 lines describe 4 child elements of the root (to, from, heading, and body):
168
169
170 <to>Tove</to>
171<from>Jani</from>
172<heading>Reminder</heading>
173<body>Don't forget me this weekend!</body>
174
175---
176
177And finally the last line defines the end of the root element:
178
179
180 </note>
181
182---
183
184Can you detect from this example that the XML document contains a Note to Tove from Jani? Don't you agree that XML is pretty self-descriptive?
185
186* * *
187
188## All XML elements must have a closing tag
189
190**With XML, it is illegal to omit the closing tag.**
191
192In HTML some elements do not have to have a closing tag. The following code is legal in HTML:
193
194
195 <p>This is a paragraph
196 <p>This is another paragraph
197
198---
199
200In XML all elements must have a closing tag, like this:
201
202
203 <p>This is a paragraph</p>
204<p>This is another paragraph</p>
205
206---
207
208**Note** : You might have noticed from the previous example that the XML declaration did not have a closing tag. This is not an error. The declaration is not a part of the XML document itself. It is not an XML element, and it should not have a closing tag.
209
210* * *
211
212## XML tags are case sensitive
213
214**Unlike HTML, XML tags are case sensitive.**
215
216With XML, the tag <letter> is different from the tag <letter>.
217
218Opening and closing tags must therefore be written with the same case:
219
220
221 <message>This is incorrect</message>
222<message>This is correct</message>
223
224---
225
226
227
228* * *
229
230## All XML elements must be properly nested
231
232**Improper nesting of tags makes no sense to XML.**
233
234In HTML some elements can be improperly nested within each other like this:
235
236
237 <b><i>This text is bold and italic</i></b>
238
239---
240
241In XML all elements must be properly nested within each other like this:
242
243
244 <b><i>This text is bold and italic</i></b>
245
246---
247
248
249
250* * *
251
252## All XML documents must have a root element
253
254**All XML documents must contain a single tag pair to define a root element.**
255
256All other elements must be within this root element.
257
258All elements can have sub elements (child elements). Sub elements must be correctly nested within their parent element:
259
260
261 <root>
262<child>
263<subchild>.....</subchild>
264</child>
265</root>
266
267---
268
269
270
271* * *
272
273## Attribute values must always be quoted
274
275**With XML, it is illegal to omit quotation marks around attribute values.**
276
277XML elements can have attributes in name/value pairs just like in HTML. In XML the attribute value must always be quoted. Study the two XML documents below. The first one is incorrect, the second is correct:
278
279
280 <?xml version="1.0" encoding="ISO-8859-1"?>
281<note date="12/11/2002">
282<to>Tove</to>
283<from>Jani</from>
284</note>
285
286---
287
288
289
290
291 <?xml version="1.0" encoding="ISO-8859-1"?>
292<note date="12/11/2002">
293<to>Tove</to>
294<from>Jani</from>
295</note>
296
297---
298
299The error in the first document is that the date attribute in the note element is not quoted.
300
301This is correct: date="12/11/2002". This is incorrect: date=12/11/2002.
302
303* * *
304
305## With XML, white space is preserved
306
307**With XML, the white space in your document is not truncated** .
308
309This is unlike HTML. With HTML, a sentence like this:
310
311Hello my name is Tove,
312
313will be displayed like this:
314
315Hello my name is Tove,
316
317because HTML reduces multiple, consecutive white space characters to a single white space.
318
319* * *
320
321## With XML, CR / LF is converted to LF
322
323**With XML, a new line is always stored as LF** .
324
325Do you know what a typewriter is? Well, a typewriter is a mechanical device which was used last century to produce printed documents. :-)
326
327After you have typed one line of text on a typewriter, you have to manually return the printing carriage to the left margin position and manually feed the paper up one line.
328
329In Windows applications, a new line is normally stored as a pair of characters: carriage return (CR) and line feed (LF). The character pair bears some resemblance to the typewriter actions of setting a new line. In Unix applications, a new line is normally stored as a LF character. Macintosh applications use only a CR character to store a new line.
330
331* * *
332
333## Comments in XML
334
335The syntax for writing comments in XML is similar to that of HTML.
336
337<!-- This is a comment -->
338
339* * *
340
341## There is nothing special about XML
342
343There is nothing special about XML. It is just plain text with the addition of some XML tags enclosed in angle brackets.
344
345Software that can handle plain text can also handle XML. In a simple text editor, the XML tags will be visible and will not be handled specially.
346
347In an XML-aware application however, the XML tags can be handled specially. The tags may or may not be visible, or have a functional meaning, depending on the nature of the application.
348
349* * *
350
351
352
353
354# XML Elements
355
356 
357
358* * *
359
360XML Elements are extensible and they have relationships.
361
362XML Elements have simple naming rules.
363
364* * *
365
366## XML Elements are Extensible
367
368**XML documents can be extended to carry more information.**
369
370Look at the following XML NOTE example:
371
372
373 <note>
374<to>Tove</to>
375<from>Jani</from>
376<body>Don't forget me this weekend!</body>
377</note>
378
379---
380
381Let's imagine that we created an application that extracted the <to>, <from>, and <body> elements from the XML document to produce this output:
382
383**MESSAGE**
384
385**To:** Tove
386**From:** Jani
387
388Don't forget me this weekend!
389
390---
391
392Imagine that the author of the XML document added some extra information to it:
393
394
395 <note>
396<date>2002-08-01</date>
397<to>Tove</to>
398<from>Jani</from>
399<heading>Reminder</heading>
400<body>Don't forget me this weekend!</body>
401</note>
402
403---
404
405Should the application break or crash?
406
407No. The application should still be able to find the <to>, <from>, and <body> elements in the XML document and produce the same output.
408
409**XML documents are E** **xtensible.**
410
411* * *
412
413## XML Elements have Relationships
414
415**Elements are related as parents and children.**
416
417To understand XML terminology, you have to know how relationships between XML elements are named, and how element content is described.
418
419Imagine that this is a description of a book:
420
421**My First XML**
422
423Introduction to XML
424
425 * What is HTML
426 * What is XML
427
428
429
430XML Syntax
431
432 * Elements must have a closing tag
433 * Elements must be properly nested
434
435
436---
437
438Imagine that this XML document describes the book:
439
440
441 <book>
442<title>My First XML</title>
443<prod id="33-657" media="paper"></prod>
444<chapter>Introduction to XML
445 <para>What is HTML</para>
446<para>What is XML</para>
447</chapter>
448<chapter>XML Syntax
449 <para>Elements must have a closing tag</para>
450<para>Elements must be properly nested</para>
451</chapter>
452</book>
453
454---
455
456Book is the **root element** . Title, prod, and chapter are **child elements** of book. Book is the **parent element** of title, prod, and chapter. Title, prod, and chapter are **siblings** (or **sister elements** ) because they have the same parent.
457
458* * *
459
460## Elements have Content
461
462**Elements can have different content types.**
463
464An **XML element** is everything from (including) the element's start tag to (including) the element's end tag.
465
466An element can have **element** content, **mixed** content, **simple** content, or **empty** content. An element can also have **attributes** .
467
468In the example above, book has **element content** , because it contains other elements. Chapter has **mixed content** because it contains both text and other elements. Para has **simple content** (or **text content** ) because it contains only text. Prod has **empty content** , because it carries no information.
469
470In the example above only the prod element has **attributes** . The **attribute** named id has the **value** "33-657". The **attribute** named media has the **value** "paper".
471
472* * *
473
474## Element Naming
475
476**XML elements must follow these naming rules:**
477
478 * Names can contain letters, numbers, and other characters
479 * Names must not start with a number or punctuation character
480 * Names must not start with the letters xml (or XML or Xml ..)
481 * Names cannot contain spaces
482
483
484
485Take care when you "invent" element names and follow these simple rules:
486
487Any name can be used, no words are reserved, but the idea is to make names descriptive. Names with an underscore separator are nice.
488
489Examples: <first_name>, <last_name>.
490
491Avoid "-" and "." in names. For example, if you name something "first-name," it could be a mess if your software tries to subtract name from first. Or if you name something "first.name," your software may think that "name" is a property of the object "first."
492
493Element names can be as long as you like, but don't exaggerate. Names should be short and simple, like this: <book_title> not like this: <the_title_of_the_book>.
494
495XML documents often have a corresponding database, in which fields exist corresponding to elements in the XML document. A good practice is to use the naming rules of your database for the elements in the XML documents.
496
497Non-English letters like éòá are perfectly legal in XML element names, but watch out for problems if your software vendor doesn't support them.
498
499The ":" should not be used in element names because it is reserved to be used for something called namespaces (more later).
500
501* * *
502
503
504
505
506# XML Attributes
507
508 
509
510* * *
511
512XML elements can have attributes in the start tag, just like HTML.
513
514Attributes are used to provide additional information about elements.
515
516* * *
517
518## XML Attributes
519
520**XML elements can have attributes.**
521
522From HTML you will remember this: <img src="computer.gif"/>. The SRC attribute provides additional information about the IMG element.
523
524In HTML (and in XML) attributes provide additional information about elements:
525
526
527 <img src="computer.gif"/>
528<a href="demo.asp">
529
530---
531
532Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but important to the software that wants to manipulate the element:
533
534
535 <file type="gif">computer.gif</file>
536
537---
538
539
540
541* * *
542
543## Quote Styles, "female" or 'female'?
544
545Attribute values must always be enclosed in quotes, but either single or double quotes can be used. For a person's sex, the person tag can be written like this:
546
547
548 <person sex="female">
549
550---
551
552or like this:
553
554
555 <person sex="female">
556
557---
558
559**Note:** If the attribute value itself contains double quotes it is necessary to use single quotes, like in this example:
560
561
562 <gangster name='George "Shotgun" Ziegler'>
563
564---
565
566**Note:** If the attribute value itself contains single quotes it is necessary to use double quotes, like in this example:
567
568
569 <gangster name="George 'Shotgun' Ziegler">
570
571---
572
573
574
575* * *
576
577## Use of Elements vs. Attributes
578
579**Data can be stored in child elements or in attributes.**
580
581Take a look at these examples:
582
583
584 <person sex="female">
585<firstname>Anna</firstname>
586<lastname>Smith</lastname>
587</person>
588
589---
590
591
592
593
594 <person>
595<sex>female</sex>
596<firstname>Anna</firstname>
597<lastname>Smith</lastname>
598</person>
599
600---
601
602In the first example sex is an attribute. In the last, sex is a child element. Both examples provide the same information.
603
604There are no rules about when to use attributes, and when to use child elements. My experience is that attributes are handy in HTML, but in XML you should try to avoid them. Use child elements if the information feels like data.
605
606* * *
607
608## My Favorite Way
609
610**I like to store data in child elements.**
611
612The following three XML documents contain exactly the same information:
613
614A date attribute is used in the first example:
615
616
617 <note date="12/11/2002">
618<to>Tove</to>
619<from>Jani</from>
620<heading>Reminder</heading>
621<body>Don't forget me this weekend!</body>
622</note>
623
624---
625
626A date element is used in the second example:
627
628
629 <note>
630<date>12/11/2002</date>
631<to>Tove</to>
632<from>Jani</from>
633<heading>Reminder</heading>
634<body>Don't forget me this weekend!</body>
635</note>
636
637---
638
639An expanded date element is used in the third: (THIS IS MY FAVORITE):
640
641
642 <note>
643<date>
644<day>12</day>
645<month>11</month>
646<year>2002</year>
647</date>
648<to>Tove</to>
649<from>Jani</from>
650<heading>Reminder</heading>
651<body>Don't forget me this weekend!</body>
652</note>
653
654---
655
656
657
658* * *
659
660## Avoid using attributes?
661
662**Should you avoid using attributes?**
663
664Some of the problems with using attributes are:
665
666 * attributes cannot contain multiple values (child elements can)
667 * attributes are not easily expandable (for future changes)
668 * attributes cannot describe structures (child elements can)
669 * attributes are more difficult to manipulate by program code
670 * attribute values are not easy to test against a Document Type Definition (DTD) - which is used to define the legal elements of an XML document
671
672
673
674If you use attributes as containers for data, you end up with documents that are difficult to read and maintain. Try to use **elements** to describe data. Use attributes only to provide information that is not relevant to the data.
675
676Don't end up like this ( if you think this looks like XML, you have not understood the point):
677
678
679 <note body="Don't forget me this weekend!" day="12" from="Jani" heading="Reminder" month="11" to="Tove" year="2002">
680</note>
681
682---
683
684
685
686* * *
687
688## An Exception to my Attribute rule
689
690**Rules always have exceptions.**
691
692My rule about attributes has one exception:
693
694Sometimes I assign ID references to elements. These ID references can be used to access XML elements in much the same way as the NAME or ID attributes in HTML. This example demonstrates this:
695
696
697 <messages>
698<note id="p501">
699<to>Tove</to>
700<from>Jani</from>
701<heading>Reminder</heading>
702<body>Don't forget me this weekend!</body>
703</note>
704<note id="p502">
705<to>Jani</to>
706<from>Tove</from>
707<heading>Re: Reminder</heading>
708<body>I will not!</body>
709</note>
710</messages>
711
712---
713
714The ID in these examples is just a counter, or a unique identifier, to identify the different notes in the XML file, and not a part of the note data.
715
716What I am trying to say here is that metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.
717
718* * *
719
720
721
722
723# XML Validation
724
725 
726
727* * *
728
729XML with correct syntax is Well Formed XML.
730
731XML validated against a DTD is Valid XML.
732
733* * *
734
735## "Well Formed" XML documents
736
737**A "Well Formed" XML document has correct XML syntax.**
738
739A "Well Formed" XML document is a document that conforms to the XML syntax rules that were described in the previous chapters:
740
741
742 <?xml version="1.0" encoding="ISO-8859-1"?>
743<note>
744<to>Tove</to>
745<from>Jani</from>
746<heading>Reminder</heading>
747<body>Don't forget me this weekend!</body>
748</note>
749
750---
751
752
753
754* * *
755
756## "Valid" XML documents
757
758**A "Valid" XML document also conforms to a DTD.**
759
760A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a Document Type Definition (DTD):
761
762
763 <?xml version="1.0" encoding="ISO-8859-1"?>
764<!DOCTYPE note SYSTEM "InternalNote.dtd">
765
766<note>
767<to>Tove</to>
768<from>Jani</from>
769<heading>Reminder</heading>
770<body>Don't forget me this weekend!</body>
771</note>
772
773---
774
775
776
777* * *
778
779## XML DTD
780
781**A DTD defines the legal elements of an XML document.**
782
783The purpose of a DTD is to define the legal building blocks of an XML document. It defines the document structure with a list of legal elements. You can read more about DTD, and how to validate your XML documents in our DTD tutorial .
784
785* * *
786
787## XML Schema
788
789**XML Schema is an XML based alternative to DTD.**
790
791W3C supports an alternative to DTD called XML Schema. You can read more about XML Schema in our Schema tutorial .
792
793* * *
794
795## Errors will Stop you
796
797**Errors in XML documents will stop the XML program.**
798
799The W3C XML specification states that a program should not continue to process an XML document if it finds a validation error. The reason is that XML software should be easy to write, and that all XML documents should be compatible.
800
801With HTML it was possible to create documents with lots of errors (like when you forget an end tag). One of the main reasons that HTML browsers are so big and incompatible, is that they have their own ways to figure out what a document should look like when they encounter an HTML error.
802
803With XML this should not be possible.
804
805* * *
806
807## A general XML Validator
808
809To help you validate your xml files, we have created this link so that you can Validate any XML file .
810
811* * *
812
813
814
815
816# XML Browser Support
817
818 
819
820* * *
821
822In our XML tutorial we have focused on Internet Explorer 5.0+.
823
824* * *
825
826## XML on this Web Site
827
828**Many browsers support XML. We are focusing on Internet Explorer 5.0+.**
829
830Some of you have complained about this, but we think this is the most practical way to demonstrate real XML examples for you over the Internet.
831
832* * *
833
834## XML in Mozilla Firefox 1.0
835
836Mozilla Firefox 1.0 supports the XML 1.0 standard.
837
838* * *
839
840## XML in Netscape 6
841
842Netscape 6 supports XML.
843
844To look at the XML source in Netscape 6: right-click on the page and select "View Page Source".
845
846* * *
847
848## XML in Internet Explorer 5.0
849
850Internet Explorer 5.0 supports the XML 1.0 standard.
851
852Internet Explorer 5.0 supports most of the international standards for XML 1.0 and the XML DOM (Document Object Model). These standards are set by the World Wide Web Consortium (W3C).
853
854Internet Explorer 5.0 has the following XML support:
855
856 * Viewing of XML documents
857 * Full support for W3C DTD standards
858 * XML embedded in HTML as Data Islands
859 * Binding XML data to HTML elements
860 * Transforming and displaying XML with XSL
861 * Displaying XML with CSS
862 * Access to the XML DOM
863
864
865
866Internet Explorer 5.0 also has support for Behaviors:
867
868 * Behaviors is a Microsoft-only technology
869 * Behaviors can separate scripts from an HTML page.
870 * Behaviors can store XML data on the client's disk.
871
872
873
874Examples of all these features are given in the next chapters of this web site.
875
876* * *
877
878
879
880
881# Viewing XML Files
882
883 
884
885* * *
886
887Raw XML files can be viewed in Mozilla Firefox, IE 5.0+ and in Netscape 6.
888
889However, to make XML documents to display like nice web pages, you will have to add some display information.
890
891* * *
892
893## Viewing XML Files
894
895**In Mozilla Firefox and IE 5.0+:**
896
897Click on a link to an XML file, type the direct URL in the address bar, or double-click on the name of an XML file in a folder. The XML document will be displayed with color-coded root and child elements. A plus (+) or minus sign (-) to the left of the elements can be clicked to expand or collapse the element structure. To view the raw XML source (without the + and - signs), select "View Page Source" or "View Source" from the browser menu.
898
899**In Netscape 6:**
900
901Open the XML file (via a link or URL), then right-click in XML file and select "View Page Source". The XML document will then be displayed with color-coded root and child elements.
902
903Look at this XML file: note.xml
904
905**Note: Do not expect XML files to be formatted like HTML documents!**
906
907* * *
908
909## Viewing an Invalid XML File
910
911If an erroneous XML file is opened, the browser will report the error.
912
913Look at this XML file: note_error.xml
914
915* * *
916
917## Other XML Examples
918
919Viewing some XML documents will help you get the XML feeling.
920
921An XML CD catalog
922This is my father's CD collection, stored as XML data (old and boring titles I think.... :-)).
923
924An XML plant catalog
925This is a plant catalog from a plant shop, stored as XML data.
926
927A Simple Food Menu
928This is a breakfast food menu from a restaurant, stored as XML data.
929
930* * *
931
932## Why does XML display like this?
933
934XML documents do not carry information about how to display the data.
935
936Since XML tags are "invented" by the author of the XML document, browsers do not know if a tag like <table> describes an HTML table or a dining table.
937
938Without any information about how to display the data, most browsers will just display the XML document as it is.
939
940In the next chapters, we will take a look at different solutions to the display problem, using CSS, XSL, JavaScript, and XML Data Islands.
941
942* * *
943
944
945
946
947# Displaying XML with CSS
948
949 
950
951* * *
952
953With CSS (Cascading Style Sheets) you can add display information to an XML document.
954
955* * *
956
957## Displaying your future XML files with CSS?
958
959Would you use CSS to format your future XML files? No, we don't think so! But we could not resist giving it a try:
960
961Take a look at this XML file: The CD catalog
962
963Then look at this style sheet: The CSS file
964
965Finally, view: The CD catalog formatted with the CSS file
966
967Below is a fraction of the XML file. The second line, ** <?xml-stylesheet type="text/css" href="cd_catalog.css"?> ** , links the XML file to the CSS file:
968
969
970 <?xml version="1.0" encoding="ISO-8859-1"?>
971<?xml-stylesheet type="text/css" href="cd_catalog.css"?>
972<catalog>
973<cd>
974<title>Empire Burlesque</title>
975<artist>Bob Dylan</artist>
976<country>USA</country>
977<company>Columbia</company>
978<price>10.90</price>
979<year>1985</year>
980</cd>
981<cd>
982<title>Hide your heart</title>
983<artist>Bonnie Tyler</artist>
984<country>UK</country>
985<company>CBS Records</company>
986<price>9.90</price>
987<year>1988</year>
988</cd>
989 .
990 .
991 .
992 .
993 </catalog>
994
995---
996
997We DO NOT believe that formatting XML with CSS is the future of the Web. Even if it looks right to use CSS this way, we DO believe that formatting with XSL will be the new standard (as soon as all main browsers support it).
998
999* * *
1000
1001## Creating your future Homepages with XML?
1002
1003Will you be writing your future Homepages in XML? No, we don't think you will! But we could not resist giving it a try : A homepage written in XML .
1004
1005We DO NOT believe that XML will be used to create future Homepages.
1006
1007* * *
1008
1009
1010
1011
1012# Displaying XML with XSL
1013
1014 
1015
1016* * *
1017
1018With XSL you can add display information to your XML document.
1019
1020* * *
1021
1022## Displaying XML with XSL
1023
1024**XSL is the preferred style sheet language of XML.**
1025
1026XSL (the eXtensible Stylesheet Language) is far more sophisticated than CSS. One way to use XSL is to transform XML into HTML before it is displayed by the browser as demonstrated in these examples:
1027
1028If you have Netscape 6 or IE 5 or higher you can view the XML file and the XSL style sheet .
1029
1030View the result in IE 6
1031
1032View the result in IE 5
1033
1034Below is a fraction of the XML file, with an added XSL reference. The second line, ** <?xml-stylesheet type="text/xsl" href="simple.xsl"?> ** , links the XML file to the XSL file:
1035
1036
1037 <?xml version="1.0" encoding="ISO-8859-1"?>
1038<?xml-stylesheet type="text/xsl" href="simple.xsl"?>
1039<breakfast_menu>
1040<food>
1041<name>Belgian Waffles</name>
1042<price>$5.95</price>
1043<description>
1044 two of our famous Belgian Waffles
1045 </description>
1046<calories>650</calories>
1047</food>
1048</breakfast_menu>
1049
1050---
1051
1052If you want to learn more about XSL, please visit our XSL tutorial .
1053
1054* * *
1055
1056
1057
1058
1059# XML in Data Islands
1060
1061 
1062
1063* * *
1064
1065With Internet Explorer 5.0 and higher, XML can be embedded within HTML pages in Data Islands.
1066
1067* * *
1068
1069## XML Embedded in HTML
1070
1071**The unofficial <xml> tag is used to embed XML data within HTML. **
1072
1073XML data can be embedded directly in an HTML page like this:
1074
1075
1076 <xml id="note">
1077<note>
1078<to>Tove</to>
1079<from>Jani</from>
1080<heading>Reminder</heading>
1081<body>Don't forget me this weekend!</body>
1082</note>
1083</xml>
1084
1085---
1086
1087Or a separate XML file can be embedded like this:
1088
1089
1090 <xml id="note" src="note.xml">
1091</xml>
1092
1093---
1094
1095**Note that the <xml> tag is an HTML element, not an XML element. **
1096
1097* * *
1098
1099## Data Binding
1100
1101**Data Islands can be bound to HTML elements (like HTML tables).**
1102
1103In the example below, an XML Data Island with an ID "cdcat" is loaded from an external XML file. An HTML table is bound to the Data Island with a data source attribute, and finally the tabledata elements are bound to the XML data with a data field attribute inside a span.
1104
1105
1106 <html>
1107<body>
1108<xml id="cdcat" src="cd_catalog.xml"></xml>
1109<table border="1" datasrc="#cdcat">
1110<tr>
1111<td><span datafld="ARTIST"></span></td>
1112<td><span datafld="TITLE"></span></td>
1113</tr>
1114</table>
1115</body>
1116</html>
1117
1118---
1119
1120If you are running IE 5.0 or higher, you can try it yourself .
1121
1122With IE 5.0 and higher you can also view the external XML file .
1123
1124Also try this example , demonstrating <thead>, <tbody>, and <tfoot>.
1125
1126* * *
1127
1128
1129
1130
1131# The Microsoft XML Parser
1132
1133 
1134
1135* * *
1136
1137To read and update - create and manipulate - an XML document, you need an XML parser.
1138
1139* * *
1140
1141## Using the XML parser
1142
1143**The Microsoft XML parser comes with Microsoft Internet Explorer 5.0.**
1144
1145Once you have installed IE 5.0, the parser is available to scripts, both inside HTML documents and inside ASP files. The parser features a language-neutral programming model that supports:
1146
1147 * JavaScript, VBScript, Perl, VB, Java, C++ and more
1148 * W3C XML 1.0 and XML DOM
1149 * DTD and validation
1150
1151
1152
1153If you are using JavaScript in IE 5.0, you can create an XML document object with the following code:
1154
1155
1156 var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
1157
1158---
1159
1160If you are using VBScript, you create the XML document object with the following code:
1161
1162
1163 set xmlDoc=CreateObject("Microsoft.XMLDOM")
1164
1165---
1166
1167If you are using VBScript in ASP, you can use the following code:
1168
1169
1170 set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")
1171
1172---
1173
1174
1175
1176* * *
1177
1178## Loading an XML file into the parser
1179
1180**XML files can be loaded into the parser using script code.**
1181
1182The following code loads an XML document (note.xml) into the XML parser:
1183
1184
1185 <script type="text/javascript">
1186 var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
1187 xmlDoc.async="false"
1188 xmlDoc.load("note.xml")
1189 // ....... processing the document goes here
1190 </script>
1191
1192---
1193
1194The second line in the code above creates an instance of the Microsoft XML parser.
1195
1196The third line turns off asynchronized loading, to make sure that the parser will not continue execution before the document is fully loaded.
1197
1198The fourth line tells the parser to load the XML document called note.xml.
1199
1200* * *
1201
1202## Loading pure XML text into the parser
1203
1204**XML text can also be loaded from a text string.**
1205
1206The following code loads a text string into the XML parser:
1207
1208
1209 <script type="text/javascript">
1210
1211 var text="<note>"
1212 text=text+"<to>Tove</to><from>Jani</from>"
1213 text=text+"<heading>Reminder</heading>"
1214 text=text+"<body>Don't forget me this weekend!</body>"
1215 text=text+"</note>"
1216
1217 var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
1218 xmlDoc.async="false"
1219 xmlDoc.loadXML(text)
1220 // ....... processing the document goes here
1221 </script>
1222
1223---
1224
1225Note that the "loadXML" method (instead of the "load" method) is used to load a text string.
1226
1227* * *
1228
1229## Displaying XML with JavaScript
1230
1231**To display XML you can use JavaScript.**
1232
1233JavaScript (or VBScript) can be used to import data from an XML file and display the XML data inside an HTML page.
1234
1235To see how XML and HTML complement each other this way; first look at the **XML** document ( note.xml ), then open the **HTML** document ( note.htm ) that contains a JavaScript which reads the XML file and displays the information inside predefined spans in the HTML page.
1236
1237To see how it works, Try It Yourself
1238
1239You can see a lot more of this kind of JavaScript in our DOM School .
1240
1241* * *
1242
1243
1244
1245
1246# XML in Real Life
1247
1248 
1249
1250* * *
1251
1252Some real-life examples of how XML can be used to carry information.
1253
1254* * *
1255
1256## Example: XML News
1257
1258**XMLNews is a specification for exchanging news and other information.**
1259
1260Using such a standard makes it easier for both news producers and news consumers to produce, receive, and archive any kind of news information across different hardware, software, and programming languages.
1261
1262* * *
1263
1264## An example XML News document:
1265
1266
1267 <?xml version="1.0" encoding="ISO-8859-1"?>
1268<nitf>
1269<head>
1270<title>Colombia Earthquake</title>
1271</head>
1272<body>
1273<body.head>
1274<headline>
1275<hl1>143 Dead in Colombia Earthquake</hl1>
1276</headline>
1277<byline>
1278<bytag>By Jared Kotler, Associated Press Writer</bytag>
1279</byline>
1280<dateline>
1281<location>Bogota, Colombia</location>
1282<story.date>Monday January 25 1999 7:28 ET</story.date>
1283</dateline>
1284</body.head>
1285</body>
1286</nitf>
1287
1288---
1289
1290
1291
1292* * *</tfoot></tbody></thead></xml></xml></table></gangster></gangster></person></person></a></the_title_of_the_book></book_title></last_name></first_name></body></from></to></body></from></to></letter></letter></p></p></from></to></h1></p>