A Well-Formed Problem
Time limit1sMemory limit128 MB
Parse a series of XML documents and decide whether each one satisfies six well-formedness rules, reporting the verdict per document.
- Level
Medium6 of 10
- Topics
- String, Stack, Hash map, Implementation
- Solved
- No attempts yet
Problem
XML (eXtensible Markup Language) has strict formatting requirements, and an XML parser must report anything that breaks the rules of a well-formed document. A document is well-formed when it satisfies every constraint listed below.
XML documents are built from elements, which contain character data and/or other elements. An element's start-tag may also declare attributes. Consider this document:
<?xml version="1.0"?>
<customer>
<name>
<first>John</first>
<last>Doe</last>
</name>
<address>
<street>
<number>15</number>
<direction>West</direction>
<name>34th</name>
</street>
<city>New York</city>
<state-code>NY</state-code>
<zip-code format="PLUS4">10001-0001</zip-code>
<country-code>USA</country-code>
</address>
<orders/>
</customer>
The identifiers inside angle brackets are the document's elements. In the zip-code element, format is an attribute. Every element except orders has both a start-tag and an end-tag. orders is an empty element: the trailing /> closes it, so it needs no separate end-tag. The first line is a processing instruction for the parser and is not an element.
A document is well-formed if and only if it obeys all of the following rules:
- Exactly one element is not contained within any other element. That element is the root (document) element. Above,
customeris the root. - Elements must nest properly. A non-empty element's start-tag must be paired with a matching end-tag.
- An end-tag's name must match its start-tag's name. Names are case-sensitive, so an element opened with
<address>must be closed with</address>. - No attribute may appear more than once in the same start-tag or empty-element tag.
- An element must not contain another element with the same name anywhere inside it. For example, an
addresselement may not contain anotheraddresselement. - Every named attribute must have a value.
Input
The input contains a series of XML documents. Each document begins with a line containing only the processing instruction <?xml version="1.0"?>. The input ends with a line containing only <?end?> -- a sentinel that marks the end of the input; it is not a real XML processing instruction. As in every XML document, white space between elements and attributes is ignored. You may assume the following:
- The only processing instruction present is the XML version instruction, and it appears only at the start of each document.
- Element and attribute names are case-sensitive;
<Address>and<address>are different. - Element and attribute names use only alphanumeric characters and the dash (
-). - No XML comments appear in the input.
- Attribute values are always enclosed in double quotes.
Output
For each XML document, print one line: well-formed if the document is well-formed, or non well-formed otherwise. Print the verdicts in the same order as the documents.