Working with XML Lesson 9 of 9
XML Final Project — A School Event Feed Reader
Build a complete XML project — design an XML file of school events with attributes and dates, check it, query it with XPath, and display it as filterable cards with JavaScript.
In this project you'll design an XML document for school events, then build a small web page that reads it, filters it by category, sorts it by date and shows it as cards. It brings together XML design, well-formedness, XPath and JavaScript.
Step 1: Design the XML
<?xml version="1.0" encoding="UTF-8"?>
<events school="Rizal National High School">
<event id="E1" category="academic">
<title>Quarterly Exams</title>
<date>2026-12-14</date>
<place>All classrooms</place>
</event>
<event id="E2" category="sports">
<title>Intramurals Basketball Finals</title>
<date>2026-12-05</date>
<place>Covered court</place>
</event>
<event id="E3" category="culture">
<title>Christmas Program & Lantern Contest</title>
<date>2026-12-18</date>
<place>Gymnasium</place>
</event>
<event id="E4" category="academic">
<title>Science Fair</title>
<date>2026-12-09</date>
<place>Library</place>
</event>
</events>
Design choices: id and category are attributes (metadata); title, date and place are elements (the data). Dates use the YYYY-MM-DD format so they sort correctly as text. The & is written as &amp;.
Step 2: Useful XPath queries
| Question | XPath |
|---|---|
| All event titles | //event/title |
| Academic events | //event[@category='academic'] |
| Events before December 10 | //event[translate(date,'-','') < 20261210] |
| Number of events | count(//event) |
Step 3: The event board
<style>
body { font-family: system-ui, sans-serif; background: #f3f6f4; margin: 0; padding: 14px; }
h2 { margin: 0 0 8px; }
.filters button { border: 1px solid #ccd3cf; background: white; border-radius: 99px; padding: 6px 12px; margin: 0 4px 10px 0; cursor: pointer; }
.filters button.on { background: #0f5c4d; color: white; border-color: #0f5c4d; }
.card { background: white; border-radius: 12px; padding: 12px 14px; margin-bottom: 10px; box-shadow: 0 3px 10px rgba(0,0,0,.06); display: flex; gap: 12px; }
.date { text-align: center; min-width: 52px; color: #0f5c4d; font-weight: bold; }
.date span { display: block; font-size: 24px; }
.tag { font-size: 12px; background: #e3f1ec; color: #0f5c4d; padding: 2px 8px; border-radius: 99px; }
.err { color: crimson; }
</style>
<h2 id="school">Events</h2>
<div class="filters" id="filters"></div>
<div id="list"></div>
<script>
const xmlText = `<?xml version="1.0" encoding="UTF-8"?>
<events school="Rizal National High School">
<event id="E1" category="academic"><title>Quarterly Exams</title><date>2026-12-14</date><place>All classrooms</place></event>
<event id="E2" category="sports"><title>Intramurals Basketball Finals</title><date>2026-12-05</date><place>Covered court</place></event>
<event id="E3" category="culture"><title>Christmas Program & Lantern Contest</title><date>2026-12-18</date><place>Gymnasium</place></event>
<event id="E4" category="academic"><title>Science Fair</title><date>2026-12-09</date><place>Library</place></event>
</events>`;
const doc = new DOMParser().parseFromString(xmlText, "application/xml");
const list = document.getElementById("list");
if (doc.querySelector("parsererror")) {
list.innerHTML = '<p class="err">The events file is not well-formed XML.</p>';
} else {
document.getElementById("school").textContent = "📅 " + doc.documentElement.getAttribute("school");
// Read every event into a plain object
const events = [...doc.querySelectorAll("event")].map((e) => ({
id: e.getAttribute("id"),
category: e.getAttribute("category"),
title: e.querySelector("title").textContent,
date: e.querySelector("date").textContent,
place: e.querySelector("place").textContent,
})).sort((a, b) => a.date.localeCompare(b.date));
const categories = ["all", ...new Set(events.map((e) => e.category))];
let current = "all";
function render() {
list.innerHTML = "";
events.filter((e) => current === "all" || e.category === current).forEach((e) => {
const d = new Date(e.date + "T00:00");
const card = document.createElement("div");
card.className = "card";
card.innerHTML = '<div class="date"></div><div><strong class="t"></strong><br><small class="p"></small> <span class="tag"></span></div>';
card.querySelector(".date").innerHTML = d.toLocaleDateString("en-PH", { month: "short" }) + "<span>" + d.getDate() + "</span>";
card.querySelector(".t").textContent = e.title;
card.querySelector(".p").textContent = "📍 " + e.place;
card.querySelector(".tag").textContent = e.category;
list.append(card);
});
}
const filters = document.getElementById("filters");
categories.forEach((c) => {
const b = document.createElement("button");
b.textContent = c[0].toUpperCase() + c.slice(1);
if (c === current) b.className = "on";
b.onclick = () => {
current = c;
filters.querySelectorAll("button").forEach((x) => x.classList.toggle("on", x === b));
render();
};
filters.append(b);
});
render();
}
</script>
How it works
- Parse and check:
DOMParserreads the XML; aparsererrorshows a friendly message instead of a broken page. - Convert: each
<event>becomes a plain JavaScript object, which is easier to sort and filter. - Sort:
YYYY-MM-DDdates sort correctly as text withlocaleCompare. - Build filters from the categories found in the data, so adding a new category needs no code change.
- Render safely with
textContentfor values from the XML.
Challenges
- Add a
<time>element and show it on each card. - Hide events whose date has already passed.
- Write an XSD for the events file that requires a date of type
xs:dateand limits category to academic, sports or culture. - Save the XML as
events.xmland load it withfetchon a local server. - Turn the events into an RSS feed so phones can subscribe.
What's next?
Congratulations on finishing the XML course! 🎉 You now understand a format that quietly powers sitemaps, feeds, graphics and office files. Continue with JavaScript to build more data-driven pages, or the PHP course to generate XML feeds and sitemaps on a server.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04