Displaying XML
XML describes data; it carries no instructions for how to look. So what happens when you open an XML file in a browser, and how can you make it look good?
Raw XML in a browser
If you open a .xml file that has no stylesheet attached, modern browsers show it as a collapsible tree view with coloured tags — handy for inspecting data, not meant for end users. Most browsers add a line such as "This XML file does not appear to have any style information associated with it."
If the file is not well-formed, the browser shows an error page instead, naming the line and column of the first problem. That makes the browser a quick (if basic) well-formedness checker — just like the playgrounds on this site:
<menu>
<dish>Pad Thai<dish>
<dish>Green curry</dish>
</menu>The first <dish> is "closed" with another start tag, so the parser eventually runs out of document while elements are still open.
Three ways to give XML a look
| Approach | How | Notes |
|---|---|---|
| CSS | <?xml-stylesheet type="text/css" href="menu.css"?> |
Simple, but can't reorder, sort or add text |
| XSLT | <?xml-stylesheet type="text/xsl" href="menu.xsl"?> |
Transforms XML into HTML; browser support is being phased out |
| JavaScript | Parse the XML, build HTML elements | Most flexible and the modern approach |
Styling with CSS
A processing instruction at the top of the XML file links a stylesheet. Every element name becomes a CSS type selector:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="menu.css"?>
<menu>
<dish><name>Pad Thai</name><price>9.50</price></dish>
</menu>menu { display: block; font-family: sans-serif; }
dish { display: block; margin: 8px 0; padding: 8px; border: 1px solid #ccc; }
name { font-weight: bold; }
price { color: #0060ac; margin-left: 1em; }XML elements have no default styles, so you must set display: block yourself. The demo below simulates the result by placing the parsed XML elements straight into an HTML page:
<!DOCTYPE html>
<html>
<head>
<style>
menu { display: block; font-family: sans-serif; padding: 0; margin: 0; }
dish { display: block; margin: 8px 0; padding: 8px; border: 1px solid #ccc; border-radius: 6px; }
name { font-weight: bold; }
price { color: #0060ac; margin-left: 1em; }
price::before { content: "€"; }
</style>
</head>
<body>
<div id="host"></div>
<script>
const xml = `<menu>
<dish><name>Pad Thai</name><price>9.50</price></dish>
<dish><name>Green curry</name><price>10.80</price></dish>
<dish><name>Mango sticky rice</name><price>5.20</price></dish>
</menu>`;
const doc = new DOMParser().parseFromString(xml, 'application/xml');
document.getElementById('host').append(document.importNode(doc.documentElement, true));
</script>
</body>
</html>CSS can only decorate the tree as it is. It can't sort the dishes, compute a total, or turn <price> into a table cell.
Building HTML with JavaScript
The flexible way is to read the values and generate whatever HTML you need:
<!DOCTYPE html>
<html>
<head>
<style>
table { border-collapse: collapse; font-family: sans-serif; }
th, td { border: 1px solid #ccc; padding: 6px 12px; }
th { background: #0060ac; color: white; }
td.num { text-align: right; }
</style>
</head>
<body>
<table id="t"><tr><th>Dish</th><th>Price</th></tr></table>
<script>
const xml = `<menu>
<dish><name>Pad Thai</name><price>9.50</price></dish>
<dish><name>Green curry</name><price>10.80</price></dish>
<dish><name>Mango sticky rice</name><price>5.20</price></dish>
</menu>`;
const doc = new DOMParser().parseFromString(xml, 'application/xml');
const dishes = [...doc.querySelectorAll('dish')]
.map(d => ({ name: d.querySelector('name').textContent, price: Number(d.querySelector('price').textContent) }))
.sort((a, b) => a.price - b.price); // sorting: something CSS can't do
for (const d of dishes) {
const row = document.getElementById('t').insertRow();
row.insertCell().textContent = d.name;
const c = row.insertCell();
c.textContent = '€' + d.price.toFixed(2);
c.className = 'num';
}
const total = dishes.reduce((s, d) => s + d.price, 0);
console.log('Dishes:', dishes.length, 'Total:', total.toFixed(2));
</script>
</body>
</html>Tip: Use
textContent(notinnerHTML) when copying values from XML into the page. Data from outside your site could contain markup, andtextContentalways treats it as plain text.
Test yourself
What do browsers show when you open a well-formed XML file with no stylesheet?
- A blank page
- A tree view of the document
- A download dialog only
- An error
Why is CSS alone limited for displaying XML?
- CSS can't select XML elements
- CSS can style elements but can't sort, filter or restructure the data
- CSS only works on SVG
- CSS requires a DTD