Common causes
1. Different capitalisation
XML names are case-sensitive, unlike HTML. <price> must be closed with </price>, never </Price> or </PRICE>.
<price currency="EUR">129.90</Price><price currency="EUR">129.90</price>2. An element that was never closed
If <name> is left open, the parser expects </name> and reports the parent’s end tag as mismatched. Look for the element named in the “Expected” part of the message.
<item>
<name>Keyboard
<qty>1</qty>
</item><item>
<name>Keyboard</name>
<qty>1</qty>
</item>3. Overlapping elements
Elements must nest completely. <b><i>text</b></i> closes the outer element before the inner one, which HTML browsers tolerate but XML forbids.
<p><b><i>Important</b></i></p><p><b><i>Important</i></b></p>4. HTML void elements written the HTML way
In XHTML, SVG and other XML documents, <br>, <img> and <input> must be closed. Use the self-closing form. The parser treats the unclosed <br> as a parent of everything after it, so the error appears at the next end tag.
<p>Line one<br>Line two</p><p>Line one<br/>Line two</p>5. An element renamed at one end only
A find-and-replace or a schema change can rename the start tag and miss the end tag, especially when the two are far apart. A namespace prefix counts as part of the name too, so <ns:item> cannot be closed by </item>.
<itemList>
<item>Keyboard</item>
</items><items>
<item>Keyboard</item>
</items>What each parser tells you
For an unclosed <name> on line 2 whose parent closes on line 4, the messages differ a lot in how much they reveal:
- libxml2 (xmllint, lxml, PHP):
Opening and ending tag mismatch: name line 2 and item. The open element, where it started, and the tag it met. - .NET:
The 'name' start tag on line 2 position 4 does not match the end tag of 'item'. Line 4, position 3.The same information, plus columns. - Java (JDK):
The element type "name" must be terminated by the matching end-tag "</name>".Names the open element, and the position is that of the end tag it met. - Firefox:
mismatched tag. Expected: </name>.Names the tag it wanted. - Python ElementTree (expat):
mismatched tag: line 4, column 2. A position and nothing else, counted from column 0.
The reported line is where the parser noticed, which is usually the end tag that did not fit. In this example the mistake is on line 2 and the error is on line 4, and in a long file those can be thousands of lines apart.
Tracking down the real mistake in a large file
Follow the element that was left open, not the line in the message:
- Take the open element’s name from libxml2, .NET or Java. Run
xmllint --noout file.xmlif your own parser does not give it. - Go to the reported line and search upwards for that start tag, checking whether each one has its end tag.
- In an editor with XML support, use code folding: the region that refuses to fold where you expect is the one left open.
- Fix that one problem and parse again. One unclosed tag often makes all later tags look wrong, so ignore any secondary errors until the first is gone.
With Python’s standard library you can print the whole chain of open elements yourself:
import xml.parsers.expat as expat
stack = []
parser = expat.ParserCreate()
parser.StartElementHandler = lambda name, attrs: stack.append((name, parser.CurrentLineNumber))
parser.EndElementHandler = lambda name: stack.pop()
try:
parser.Parse(open("feed.xml", "rb").read(), True)
except expat.ExpatError as err:
print(err, "still open:", stack)
The last entry in stack is the element that was not closed, with the line it started on. PasteKit’s message gives the same information: the open element, its line, and the end tag it found.
Why generated XML should not hit this
A serialiser cannot produce a mismatched tag, because it writes the end tag for you. Build documents with xml.etree.ElementTree, XMLStreamWriter or XmlWriter rather than joining strings. If you must use a template, keep each element’s start and end tags in the same template block, so a conditional cannot emit one without the other.
For files that people edit, add a well-formedness check to CI: xmllint --noout exits non-zero if any file it is given is malformed. It also catches SVG icons and XHTML snippets, which are frequently written as HTML and only fail when an XML tool reads them.
HTML habits are the root of many cases. Browsers repair <br>, overlapping tags and mixed case silently, so markup can look fine on screen and still be rejected the moment it is treated as XML.
Frequently asked questions
How do I read libxml2's "price line 4 and Price"?
The first name is the element that is still open and the line where it started; the second is the end tag that was found. So the message means “<price> from line 4 was closed with </Price>”.
Why does the error point far below the real mistake?
An element left open is only detected when an end tag for one of its ancestors appears, which can be many lines later. PasteKit names the open element and the line where it started.
Can a parser recover from mismatched tags?
XML parsers must stop at the first well-formedness error. HTML parsers recover, which is why the same markup may display in a browser but fail as XML.
The two names look identical. What else can be wrong?
Check for a namespace prefix on one tag only, a difference in case, or a look-alike Unicode character copied from a document. A space before the closing > is allowed, so </price > is fine, but one inside the name is not.