XML, or Extensible Markup Language, is a markup language used to contain, but not display, data. It’s a textual data format similar to HTML, but designed for both human and machine readability and simplicity. XML is used widely by engineers in all computer science fields, web designers and developers, and more. Because of its broad use in engineering fields, it’s vital that developers know how to answer XML interview questions if and when they pop up.
Check out this guide for an introduction to XML, or dive right into XML coding with this course, where you can learn the rules, syntax, and other essentials of the XML language.
Why Learn XML?
There are no rules in XML regarding what the data it stores is, only how that data is represented. XML is designed for easy conversion back and forth to and from other formats, a process called parsing. For an example of this, check out this guide on how to convert an Excel spreadsheet into an XML document.
Because of its usability and broad applications, it’s really important to know how XML works, what to use it for, and more.
XML Interview Questions
Learn the fundamentals of XML with this training course, or review the following sample XML interview questions for preparation.
- What are the differences between HTML and XML?
HTML is used to display data, primarily on web pages. While XML is designed for human readability, it’s actually only used for storing data. HTML handles both the structure and the appearance of its data, while XML only needs to represent it in a way that’s readable for humans, and able to be processed and converted back into a document by machines. Lastly, HTML tags are stringent and pre-defined, while XML tags can be customized to suit the user’s needs.
There are also some very specific formatting rules that set them apart – for instance, XML requires quotes around attribute values, while HTML does not – but the primary difference is in functionality. XML is driven by content, while HTML is driven by format.
- What does an XML document’s structure look like?
<?xml version="1.0" encoding="UTF-8"?> <book> <title>The Road</title> <author>Cormac McCarthy</author> <genre>Post-apocalyptic fiction</genre> <date>September 26, 2006</date> </book>
While HTML and XML are not functionally the same, they do share many basic formatting principles. Learn more about HTML and its structure in this web design course.
- Explain all the elements in the above XML document example.
<?xml version="1.0" encoding="UTF-8"?>
The first line in the XML document is the XML declaration, which defines which version of XML is being used.
The second line in the XML document is the root element, which describes what kind of data the document is representing. In our example XML document, the root element is <book>, which we can reasonably assume means the listed data is that of a book.
<title>The Road</title> <author>Cormac McCarthy</author> <genre>Post-apocalyptic fiction</genre> <date>September 26, 2006</date>
The elements inside of the root element are called child elements. Root elements can contain an unlimited amount of child elements, and child elements can contain an unlimited amount of sub-child elements and sibling elements, which are child elements on the same level as them.
The final line in our example XML document closes the root element.
- What is an XML attribute?
An attribute is a value that defines additional information about an XML element. For example:
<librarymembers> <patron id="5678910"> <name>Christina Woo</name> <age>25</age> </patron> </librarymembers>
In the above example, <patron> is an element and id is an attribute of the element <patron>.
- What are some basic syntactical rules in XML?
- Open tags must be closed.
- Example: <date>January 7</date>
- XML is case sensitive, meaning the case in open tags must match the case in closing tags.
- Example: <name> should closed with </name>, not </Name>, or </NAME>, or </nAmE>
- Child tags should be closed before parent tags are closed.
- Example: <name><firstN>Hyung</firstN> <lastN>Park</lastN></name>
- What is XML Schema? Provide an example.
XML Schema is a separate XML file that defines what elements in an XML instance document needs to contain. For instance:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string"/> <xs:element name="genre" type="xs:string"/> <xs:element name="date" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
- What is DTD?
DTD stands for Document Type Definition. These are used to define the structure of an XML document by listing legal attributes and elements, specifically the names of elements, how and where these elements can be used, the order of these elements, the element attributes, proper nesting syntax of elements, and more.
To implement a DTD in an XML document, you can either include the element definitions of the DTD within the actual XML document you want to define, or create a separate file for the DTD and simply reference its file name within the appropriate XML document.
- What is a simple element?
In XML, simple elements are exclusively text-based. Simple elements lack attributes, child elements, and cannot be left empty.
- What is a complex element?
Unlike simple elements, complex elements can hold multiple attributes and elements. These include text-only elements, elements that contain other elements exclusively, elements that contain a combination of text and additional sub-elements, and empty elements.
- Explain what a CDATA section in XML is.
CDATA means character data. When an XML document is run through a parser, text contained within a designated CDATA section will be overlooked.
- Use <![CDATA[ to open a CDATA section
- Use ]]> to close a CDATA section
- You cannot nest CDATA sections
- What is XPath?
XPath is a query language similar to SQL in its ability to retrieve data nodes from documents, but designed specifically for the rules and syntax of XML. With XPath, you can find and retrieve attributes, elements, or specific values from XML files. You can learn the practical applications of XPath in this five-day XML course.
Check out this course on how to become a web developer, a useful review for those pursuing or preparing for a job in web development. You might also consider a tutorial course on XSLT, a language used in conjunction with XML, like XPath.