
Learn XML by completing one small document workflow. This guide uses a fictional two-book library: you will inspect its structure, check its rules, select its records, and produce an HTML reading list. A text editor, Python 3.10 or later, and basic terminal familiarity are enough.
Start with a document you can explain
Download the XML labs pack and extract it into a private folder. Open library.xml in a plain-text editor. The root is library; each book has an identifier, a title, and a copy count. The default namespace gives those element names a shared identity.
<library xmlns="urn:yenra:library">
<book id="A1">
<title>Field Guide & Notes</title>
<copies>2</copies>
</book>
</library>This excerpt contains one book; the download adds B2, Atlas, with three copies. & represents the ampersand in the title. Matching end tags and quoted attributes make the structure explicit. The W3C XML specification defines those syntax rules. Explain each element before adding more markup: names should communicate the document's meaning to its consumers.
Run the workflow and inspect the result
From the extracted folder, install lxml in your preferred Python environment and run:
python -m pip install lxml
python learn.pyThe script validates the document, prints both titles with counts, reports Total copies: 5, and writes library.html. Open that output in a browser: it should contain two list items, including the correctly displayed ampersand. Keep the XML as the source; the generated HTML is a presentation of it.
Next, make a copy of library.xml, change A1's count from 2 to 4, and pass that filename to the script. Expect seven total copies and an updated HTML list. This controlled change tests whether you understand which source field drives the output.
Find the rule behind each failure
| Change | Expected result | What it teaches |
|---|---|---|
| Run invalid-copies.xml | Validation error for a negative count | The schema restricts copies to nonnegative integers. |
| Run duplicate-id.xml | Duplicate-key validation error | A book identifier must be unique within the library. |
| Remove a closing tag in a copy | XML syntax error | Parsing happens before schema validation. |
| Use a plain book query without a namespace | No matching namespaced books | A name includes its namespace URI. |
Run the two supplied invalid files as python learn.py invalid-copies.xml and python learn.py duplicate-id.xml. Both should fail. Read the message and locate the relevant rule in library.xsd. A valid document can still contain a title or inventory count that is factually wrong; that requires an application or human check. The lxml validation documentation explains schema diagnostics.
Connect selection and transformation
The Python script binds l to urn:yenra:library and selects /l:library/l:book. The chosen prefix is local to the query. The namespace URI is what must match. In this example, the unprefixed id attribute has no namespace. See the Namespaces in XML rules.
The XSLT 1.0 stylesheet visits those same books and emits HTML list items. Open library.xsl beside the generated HTML and trace one title through the template. For further practice, add a third book with a unique ID and predict the new count before running the script.
These exercises accept small UTF-8 local files, reject DTD/entity declarations, and constrain resource loading. They are a learning environment; a public upload service also needs request, resource, and execution limits. Learning XML by Erik T. Ray offers a longer conceptual introduction; the second-edition publisher listing identifies the edition and contents. Pair older book examples with the documentation for your installed tools.