SlideShare a Scribd company logo
1 of 12
BAB III Memformat XML
Viewing XML Files




Raw XML files can be viewed in all major browsers.

Don't expect XML files to be displayed as HTML pages.




Viewing XML Files


<?xml version="1.0" encoding="ISO-8859-1"?>
- <note>
   <to>Tove</to>
   <from>Jani</from>
   <heading>Reminder</heading>
   <body>Don't forget me this weekend!</body>
  </note>
Look at this XML file: note.xml
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.
Note: In Netscape, Opera, and Safari, only the element text will be displayed. To view the raw XML, you must right
click the page and select "View Source"




Viewing an Invalid XML File

If an erroneous XML file is opened, the browser will report the error.
Look at this XML file: note_error.xml




Other XML Examples

Viewing some XML documents will help you get the XML feeling.
An XML CD catalog
This is a CD collection, stored as XML data.
An XML plant catalog
This is a plant catalog from a plant shop, stored as XML data.
A Simple Food Menu
This is a breakfast food menu from a restaurant, stored as XML data.




Why Does XML Display Like This?

XML documents do not carry information about how to display the data.
Since 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.
Without any information about how to display the data, most browsers will just display the XML document as it is.
In the next chapters, we will take a look at different solutions to the display problem, using CSS, XSLT and
JavaScript.




Displaying XML with CSS




With CSS (Cascading Style Sheets) you can add display information to an XML document.




Displaying your XML Files with CSS?

It is possible to use CSS to format an XML document.
Below is an example of how to use a CSS style sheet to format an XML document:
Take a look at this XML file: The CD catalog
Then look at this style sheet: The CSS file
Finally, view: The CD catalog formatted with the CSS file
Below is a fraction of the XML file. The second line links the XML file to the CSS file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="cd_catalog.css"?>
<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>
.
.
.
.
</CATALOG>
Formatting XML with CSS is not the most common method.
W3C recommend using XSLT instead. See the next chapter.
Displaying XML with XSLT




With XSLT you can transform an XML document into HTML.




Displaying XML with XSLT

XSLT is the recommended style sheet language of XML.
XSLT (eXtensible Stylesheet Language Transformations) is far more sophisticated than CSS.
One way to use XSLT is to transform XML into HTML before it is displayed by the browser as demonstrated in these
examples:
View the XML file, the XSLT style sheet, and View the result.
Below is a fraction of the XML file. The second line links the XML file to the XSLT file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="simple.xsl"?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>
       two of our famous Belgian Waffles
    </description>
    <calories>650</calories>
  </food>
</breakfast_menu>
If you want to learn more about XSLT, find our XSLT tutorial on our homepage.




Transforming XML with XSLT on the Server

In the example above, the XSLT transformation is done by the browser, when the browser reads the XML file.
Different browsers may produce different result when transforming XML with XSLT. To reduce this problem the XSLT
transformation can be done on the server.
View the result.
Note that the result of the output is exactly the same, either the transformation is done by the web server or by the
web browser.
Displaying XML with XSLT




With XSLT you can transform an XML document into HTML.




Displaying XML with XSLT

XSLT is the recommended style sheet language of XML.
XSLT (eXtensible Stylesheet Language Transformations) is far more sophisticated than CSS.
One way to use XSLT is to transform XML into HTML before it is displayed by the browser as demonstrated in these
examples:
View the XML file, the XSLT style sheet, and View the result.
Below is a fraction of the XML file. The second line links the XML file to the XSLT file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="simple.xsl"?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>
       two of our famous Belgian Waffles
    </description>
    <calories>650</calories>
  </food>
</breakfast_menu>
If you want to learn more about XSLT, find our XSLT tutorial on our homepage.




Transforming XML with XSLT on the Server

In the example above, the XSLT transformation is done by the browser, when the browser reads the XML file.
Different browsers may produce different result when transforming XML with XSLT. To reduce this problem the XSLT
transformation can be done on the server.
View the result.
Note that the result of the output is exactly the same, either the transformation is done by the web server or by the
web browser.
XML to HTML




This chapter explains how to display XML data as HTML.




Examples

Display XML data as an HTML table
Loads data from an XML file and displays it as an HTML table.




Display XML Data in HTML

In the last chapter, we explained how to parse XML and access the DOM with JavaScript.
In this example, we loop through an XML file (cd_catalog.xml), and display each CD element as an HTML table row:

<html>
<body>

<script type="text/javascript">
var xmlDoc=null;
if (window.ActiveXObject)
{// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{// code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
if (xmlDoc!=null)
{
xmlDoc.async=false;
xmlDoc.load("cd_catalog.xml");

document.write("<table border='1'>");

var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
{
document.write("<tr>");
document.write("<td>");
document.write(
x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td>");

document.write("<td>");
document.write(
x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
}
</script>

</body>
</html>
Try it yourself: Display XML data in an HTML table

Example explained

     •   We    check the browser, and load the XML using the correct parser
     •   We    create an HTML table with <table border="1">
     •   We    use getElementsByTagName() to get all XML CD nodes
     •   For   each CD node, we display data from ARTIST and TITLE as table data.
     •   We    end the table with </table>
For more information about using JavaScript and the XML DOM, visit our XML DOM tutorial.




Access Across Domains

For security reasons, modern browsers does not allow access across domains.
This means, that both the web page and the XML file it tries to load, must be located on the same server.
The examples on W3Schools all open XML files located on the W3Schools domain.
If you want to use the example above on one of your web pages, the XML files you load must be located on your
own server. Otherwise the xmlDoc.load() method, will generate the error "Access is denied".
Rancangan Jaringan Komputer
Rancangan Jaringan Komputer
Rancangan Jaringan Komputer
Rancangan Jaringan Komputer
Rancangan Jaringan Komputer
Rancangan Jaringan Komputer

More Related Content

What's hot (12)

Lotus Notes Tips
Lotus Notes TipsLotus Notes Tips
Lotus Notes Tips
 
Fork forms library
Fork forms libraryFork forms library
Fork forms library
 
Form 1 Term 2 Week 4.3
Form 1   Term 2   Week 4.3Form 1   Term 2   Week 4.3
Form 1 Term 2 Week 4.3
 
"Getting Started with XSLT" presentation slides
"Getting Started with XSLT" presentation slides"Getting Started with XSLT" presentation slides
"Getting Started with XSLT" presentation slides
 
art2830 1st html tag presentation
art2830 1st html tag presentationart2830 1st html tag presentation
art2830 1st html tag presentation
 
hw4_specifications
hw4_specificationshw4_specifications
hw4_specifications
 
Dw Lesson01
Dw Lesson01Dw Lesson01
Dw Lesson01
 
Html tags
Html tagsHtml tags
Html tags
 
0016text[1].Txt.Xhtml
0016text[1].Txt.Xhtml0016text[1].Txt.Xhtml
0016text[1].Txt.Xhtml
 
php
phpphp
php
 
HTML 5 Basics Part Two
HTML 5 Basics Part TwoHTML 5 Basics Part Two
HTML 5 Basics Part Two
 
Creating a basic joomla
Creating a basic joomlaCreating a basic joomla
Creating a basic joomla
 

Viewers also liked (12)

Dasar dasar mikrotik
Dasar dasar mikrotikDasar dasar mikrotik
Dasar dasar mikrotik
 
Bab ii seting ip dan dhcp server
Bab ii seting ip dan dhcp serverBab ii seting ip dan dhcp server
Bab ii seting ip dan dhcp server
 
Masalah komputer
Masalah komputerMasalah komputer
Masalah komputer
 
Makalah TIK - Kunjungan Warnet
Makalah TIK - Kunjungan WarnetMakalah TIK - Kunjungan Warnet
Makalah TIK - Kunjungan Warnet
 
Modul Mikrotik
Modul MikrotikModul Mikrotik
Modul Mikrotik
 
Materi workshop Mikrotik #3
Materi workshop Mikrotik #3Materi workshop Mikrotik #3
Materi workshop Mikrotik #3
 
Ppt Printer
Ppt PrinterPpt Printer
Ppt Printer
 
Mikrotik ppt
Mikrotik pptMikrotik ppt
Mikrotik ppt
 
Modul Workshop Mikrotik Bandwidth Management
Modul Workshop Mikrotik Bandwidth ManagementModul Workshop Mikrotik Bandwidth Management
Modul Workshop Mikrotik Bandwidth Management
 
MTCNA
MTCNAMTCNA
MTCNA
 
Keselamatan Komputer
Keselamatan KomputerKeselamatan Komputer
Keselamatan Komputer
 
Diktat Praktikum Aplikasi Berbasis Jaringan
Diktat Praktikum Aplikasi Berbasis JaringanDiktat Praktikum Aplikasi Berbasis Jaringan
Diktat Praktikum Aplikasi Berbasis Jaringan
 

Similar to Rancangan Jaringan Komputer

Web programming xml
Web programming  xmlWeb programming  xml
Web programming xml
Uma mohan
 
Web programming unit IIII XML &DOM NOTES BY BHAVSINGH MALOTH
Web programming unit IIII XML &DOM NOTES BY BHAVSINGH MALOTHWeb programming unit IIII XML &DOM NOTES BY BHAVSINGH MALOTH
Web programming unit IIII XML &DOM NOTES BY BHAVSINGH MALOTH
Bhavsingh Maloth
 

Similar to Rancangan Jaringan Komputer (20)

Introduction to xml
Introduction to xmlIntroduction to xml
Introduction to xml
 
Xml material
Xml materialXml material
Xml material
 
Xml material
Xml materialXml material
Xml material
 
Xml material
Xml materialXml material
Xml material
 
Web programming xml
Web programming  xmlWeb programming  xml
Web programming xml
 
XML - The Extensible Markup Language
XML - The Extensible Markup LanguageXML - The Extensible Markup Language
XML - The Extensible Markup Language
 
Wp unit III
Wp unit IIIWp unit III
Wp unit III
 
Unit 2.2
Unit 2.2Unit 2.2
Unit 2.2
 
XML DTD Validate
XML DTD ValidateXML DTD Validate
XML DTD Validate
 
Introduction to xml schema
Introduction to xml schemaIntroduction to xml schema
Introduction to xml schema
 
Unit 2.2
Unit 2.2Unit 2.2
Unit 2.2
 
Introduction of xml and xslt
Introduction of xml and xsltIntroduction of xml and xslt
Introduction of xml and xslt
 
Xml PPT
Xml PPTXml PPT
Xml PPT
 
Web programming unit IIII XML &DOM NOTES BY BHAVSINGH MALOTH
Web programming unit IIII XML &DOM NOTES BY BHAVSINGH MALOTHWeb programming unit IIII XML &DOM NOTES BY BHAVSINGH MALOTH
Web programming unit IIII XML &DOM NOTES BY BHAVSINGH MALOTH
 
Xslt
XsltXslt
Xslt
 
Xslt
XsltXslt
Xslt
 
Transforming xml with XSLT
Transforming  xml with XSLTTransforming  xml with XSLT
Transforming xml with XSLT
 
Xml
XmlXml
Xml
 
Xml description
Xml descriptionXml description
Xml description
 
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
The Ebook Developer's Toolbox - ebookcraft 2016 - Sanders Kleinfeld
 

More from Candra Adi Putra

Modul v pengenalan mikrotik
Modul  v pengenalan mikrotikModul  v pengenalan mikrotik
Modul v pengenalan mikrotik
Candra Adi Putra
 

More from Candra Adi Putra (20)

Puasa dan pemanfaatan media sosial
Puasa dan pemanfaatan media sosialPuasa dan pemanfaatan media sosial
Puasa dan pemanfaatan media sosial
 
Seting IP Manual in Windows, Mac OS X, Linux and Android
Seting IP Manual in Windows, Mac OS X, Linux and AndroidSeting IP Manual in Windows, Mac OS X, Linux and Android
Seting IP Manual in Windows, Mac OS X, Linux and Android
 
Mengenal Peralatan Jaringan
Mengenal Peralatan JaringanMengenal Peralatan Jaringan
Mengenal Peralatan Jaringan
 
Candra lab gis v 1
Candra lab gis v 1Candra lab gis v 1
Candra lab gis v 1
 
Layanan pelengkap twitter
Layanan pelengkap twitterLayanan pelengkap twitter
Layanan pelengkap twitter
 
Responsive web design
Responsive web designResponsive web design
Responsive web design
 
Budaya internet emoticon
Budaya internet emoticonBudaya internet emoticon
Budaya internet emoticon
 
Budaya internet flamewar
Budaya internet flamewarBudaya internet flamewar
Budaya internet flamewar
 
Budaya internet meme
Budaya internet memeBudaya internet meme
Budaya internet meme
 
Budaya internet troll
Budaya internet trollBudaya internet troll
Budaya internet troll
 
E commerce dengan php mysql.docx
E commerce dengan php mysql.docxE commerce dengan php mysql.docx
E commerce dengan php mysql.docx
 
Modul v pengenalan mikrotik
Modul  v pengenalan mikrotikModul  v pengenalan mikrotik
Modul v pengenalan mikrotik
 
ReactOS desktop
ReactOS desktopReactOS desktop
ReactOS desktop
 
Bab iv billing
Bab iv billingBab iv billing
Bab iv billing
 
Bab iii filesharing
Bab iii  filesharingBab iii  filesharing
Bab iii filesharing
 
Bab i dasar dasar jaringan
Bab i  dasar dasar jaringanBab i  dasar dasar jaringan
Bab i dasar dasar jaringan
 
Anatomi hasil pencarian Google
Anatomi hasil pencarian GoogleAnatomi hasil pencarian Google
Anatomi hasil pencarian Google
 
Best web app
Best web appBest web app
Best web app
 
Php modul1 dasar dasar php
Php modul1  dasar dasar phpPhp modul1  dasar dasar php
Php modul1 dasar dasar php
 
Ebook tutorial pemrograman android
Ebook tutorial pemrograman android Ebook tutorial pemrograman android
Ebook tutorial pemrograman android
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 

Rancangan Jaringan Komputer

  • 1. BAB III Memformat XML Viewing XML Files Raw XML files can be viewed in all major browsers. Don't expect XML files to be displayed as HTML pages. Viewing XML Files <?xml version="1.0" encoding="ISO-8859-1"?> - <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> Look at this XML file: note.xml 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. Note: In Netscape, Opera, and Safari, only the element text will be displayed. To view the raw XML, you must right click the page and select "View Source" Viewing an Invalid XML File If an erroneous XML file is opened, the browser will report the error. Look at this XML file: note_error.xml Other XML Examples Viewing some XML documents will help you get the XML feeling. An XML CD catalog This is a CD collection, stored as XML data. An XML plant catalog This is a plant catalog from a plant shop, stored as XML data. A Simple Food Menu This is a breakfast food menu from a restaurant, stored as XML data. Why Does XML Display Like This? XML documents do not carry information about how to display the data. Since 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.
  • 2. Without any information about how to display the data, most browsers will just display the XML document as it is. In the next chapters, we will take a look at different solutions to the display problem, using CSS, XSLT and JavaScript. Displaying XML with CSS With CSS (Cascading Style Sheets) you can add display information to an XML document. Displaying your XML Files with CSS? It is possible to use CSS to format an XML document. Below is an example of how to use a CSS style sheet to format an XML document: Take a look at this XML file: The CD catalog Then look at this style sheet: The CSS file Finally, view: The CD catalog formatted with the CSS file Below is a fraction of the XML file. The second line links the XML file to the CSS file: <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/css" href="cd_catalog.css"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR> </CD> . . . . </CATALOG> Formatting XML with CSS is not the most common method. W3C recommend using XSLT instead. See the next chapter.
  • 3. Displaying XML with XSLT With XSLT you can transform an XML document into HTML. Displaying XML with XSLT XSLT is the recommended style sheet language of XML. XSLT (eXtensible Stylesheet Language Transformations) is far more sophisticated than CSS. One way to use XSLT is to transform XML into HTML before it is displayed by the browser as demonstrated in these examples: View the XML file, the XSLT style sheet, and View the result. Below is a fraction of the XML file. The second line links the XML file to the XSLT file: <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="simple.xsl"?> <breakfast_menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description> two of our famous Belgian Waffles </description> <calories>650</calories> </food> </breakfast_menu> If you want to learn more about XSLT, find our XSLT tutorial on our homepage. Transforming XML with XSLT on the Server In the example above, the XSLT transformation is done by the browser, when the browser reads the XML file. Different browsers may produce different result when transforming XML with XSLT. To reduce this problem the XSLT transformation can be done on the server. View the result. Note that the result of the output is exactly the same, either the transformation is done by the web server or by the web browser.
  • 4. Displaying XML with XSLT With XSLT you can transform an XML document into HTML. Displaying XML with XSLT XSLT is the recommended style sheet language of XML. XSLT (eXtensible Stylesheet Language Transformations) is far more sophisticated than CSS. One way to use XSLT is to transform XML into HTML before it is displayed by the browser as demonstrated in these examples: View the XML file, the XSLT style sheet, and View the result. Below is a fraction of the XML file. The second line links the XML file to the XSLT file: <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="simple.xsl"?> <breakfast_menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description> two of our famous Belgian Waffles </description> <calories>650</calories> </food> </breakfast_menu> If you want to learn more about XSLT, find our XSLT tutorial on our homepage. Transforming XML with XSLT on the Server In the example above, the XSLT transformation is done by the browser, when the browser reads the XML file. Different browsers may produce different result when transforming XML with XSLT. To reduce this problem the XSLT transformation can be done on the server. View the result. Note that the result of the output is exactly the same, either the transformation is done by the web server or by the web browser.
  • 5. XML to HTML This chapter explains how to display XML data as HTML. Examples Display XML data as an HTML table Loads data from an XML file and displays it as an HTML table. Display XML Data in HTML In the last chapter, we explained how to parse XML and access the DOM with JavaScript. In this example, we loop through an XML file (cd_catalog.xml), and display each CD element as an HTML table row: <html> <body> <script type="text/javascript"> var xmlDoc=null; if (window.ActiveXObject) {// code for IE xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); } else if (document.implementation.createDocument) {// code for Mozilla, Firefox, Opera, etc. xmlDoc=document.implementation.createDocument("","",null); } else { alert('Your browser cannot handle this script'); } if (xmlDoc!=null) { xmlDoc.async=false; xmlDoc.load("cd_catalog.xml"); document.write("<table border='1'>"); var x=xmlDoc.getElementsByTagName("CD"); for (i=0;i<x.length;i++) { document.write("<tr>"); document.write("<td>"); document.write( x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue); document.write("</td>"); document.write("<td>"); document.write( x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); document.write("</td>"); document.write("</tr>");
  • 6. } document.write("</table>"); } </script> </body> </html> Try it yourself: Display XML data in an HTML table Example explained • We check the browser, and load the XML using the correct parser • We create an HTML table with <table border="1"> • We use getElementsByTagName() to get all XML CD nodes • For each CD node, we display data from ARTIST and TITLE as table data. • We end the table with </table> For more information about using JavaScript and the XML DOM, visit our XML DOM tutorial. Access Across Domains For security reasons, modern browsers does not allow access across domains. This means, that both the web page and the XML file it tries to load, must be located on the same server. The examples on W3Schools all open XML files located on the W3Schools domain. If you want to use the example above on one of your web pages, the XML files you load must be located on your own server. Otherwise the xmlDoc.load() method, will generate the error "Access is denied".