XML Basics Cheat Sheet
A foundational reference for XML document structure, elements, attributes, namespaces, and validation with XSD and DTD.
Document Structure
Declaration, elements, and attributes.
<?xml version="1.0" encoding="UTF-8"?><library> <book id="b1" available="true"> <title>The Pragmatic Programmer</title> <author>David Thomas</author> <price currency="USD">39.99</price> </book> <!-- This is a comment --></library>
Namespaces
Avoiding element name collisions across vocabularies.
<root xmlns:h="http://www.w3.org/TR/html4/" xmlns:f="https://example.com/furniture"> <h:table> <h:tr><h:td>Apples</h:td></h:tr> </h:table> <f:table> <f:width>80</f:width> </f:table></root>
XPath Basics
Selecting nodes within an XML document.
/library/book <!-- all book children of library -->//title <!-- all title elements, any depth -->/library/book[@id='b1'] <!-- book with id attribute b1 -->/library/book[1] <!-- first book element -->//book/author/text() <!-- text content of author -->//price[@currency='USD'] <!-- price elements with attribute -->
Key Concepts
Core terminology and validation approaches.
- Well-formed- Every opening tag has a matching closing tag and tags are properly nested
- Valid- Well-formed AND conforms to a schema (DTD or XSD)
- Attribute vs. element- Attributes hold metadata on a tag; elements hold structured/nested content
- CDATA section- `<![CDATA[ raw text ]]>` prevents special characters from being parsed as markup
- Entity references- `<` `>` `&` `'` `"` escape reserved characters
- XSD- XML Schema Definition; defines allowed structure, types, and constraints for validation
DTD Declarations
Defining allowed structure with an inline or external Document Type Definition.
<!DOCTYPE library [ <!ELEMENT library (book+)> <!ELEMENT book (title, author, price)> <!ATTLIST book id ID #REQUIRED available (true|false) "true"> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT price (#PCDATA)>]><!-- External DTD reference instead of inline: --><!DOCTYPE library SYSTEM "library.dtd">
XSD Complex Types & Restrictions
Constraining element structure and value ranges with XML Schema.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="book"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="price"> <xs:simpleType> <xs:restriction base="xs:decimal"> <xs:minInclusive value="0"/> <xs:maxInclusive value="9999.99"/> </xs:restriction> </xs:simpleType> </xs:element> </xs:sequence> <xs:attribute name="id" type="xs:ID" use="required"/> </xs:complexType> </xs:element></xs:schema>
XSLT Transformation
Converting XML into another format (here, HTML) with a stylesheet.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/library"> <ul> <xsl:for-each select="book"> <li><xsl:value-of select="title"/> - $<xsl:value-of select="price"/></li> </xsl:for-each> </ul> </xsl:template></xsl:stylesheet>
Advanced XPath Axes & Functions
Navigating relationships and filtering with functions beyond basic paths.
//book[price > 30] <!-- predicate with comparison -->//book[position() <= 2] <!-- first two matches -->ancestor::library <!-- axis: walk up to ancestor -->following-sibling::book[1] <!-- next sibling book -->//book[contains(title, 'Pragmatic')] <!-- substring match -->//*[local-name()='table'] <!-- ignore namespace prefix -->count(//book) <!-- aggregate function -->
Advanced Concepts
Parsing models and processing details that matter beyond basic well-formedness.
- DOM vs. SAX vs. StAX- DOM loads the whole tree in memory; SAX is event-driven/streaming and read-only; StAX is a pull-based streaming API
- Processing instruction- `<?xml-stylesheet type="text/xsl" href="style.xsl"?>` passes directives to the processor, not the document model
- XML entity expansion attack- Malicious DTDs with recursive entities can cause a 'billion laughs' denial-of-service; disable external entity resolution in parsers
- Mixed content- An element declared to allow both text and child elements interleaved, e.g. `<p>Hello <b>world</b>!</p>`
- Canonical XML (C14N)- A normalized serialization used so two logically-equal documents produce byte-identical output, needed for XML digital signatures
- xsi:schemaLocation- Attribute hinting which XSD file validates a given namespace, e.g. `xsi:schemaLocation="http://ex.com ex.xsd"`
- XInclude- `<xi:include href="chunk.xml"/>` merges external XML fragments into a document before parsing continues
Every XML document can have only one root element — if you need multiple top-level items, wrap them in a single container element.