Sitemap
Ali Aslam

Ali Aslam

Avid Learner | Principal Software Engineer | Full Stack Developer

XML And Allied Technologies

13 min readJul 10, 2024

XML or Extensible Markup Language is a markup language and file format for storing, transmitting and reconstructing arbitrary data.

You can switch to video version of article

From a web development perspective, XML is eerily similar to HTML and though it has nothing to do formally with HTML as a language, it is many a times considered a mother language of HTML. Strictly speaking, the original parent language of HTML was SGML or Standard Generalized Markup Language. But you probably would never hear about SGML itself, as it is known by its two derivatives XML and HTML. Though technically siblings, I think XML is more abstract than HTML and in my humble opinion the path to learning HTML passes through XML. So let’s keep HTML at arm’s length for a while and focus on XML.

So XML was originally designed to store and transport data. It is a text based format and its greatest quality being that it is understandable by both humans and machine. It is standardized by Open Standards, and driving goal is simplicity, generality and usability across internet.

Before we dive into its syntax let’s do a little mental exercise. Let’s say your mom, tired of dad asking where his things are in the house all the time, assigns you the unenviable task of organizing things in the house in a text file, which can be read and understood by everyone at your home. I know there are better ways to do that but just cut me some slack for trying to come up with a good example for XML :) All right, so if you are for some reason constrained to just use a text file what would the text file look like? You’re welcome to take a pause and think, then read ahead.

So house is like a tree structure. It is a facility which is organized into rooms, rooms have types, qualities or attributes describing their role, like garage, bedroom, bathroom, TV room, kitchen etc. In each room, there are items and containers containing other items like cupboards, storage boxes, drawers and so on. In short, there is a hierarchy or tree like organization in house and you are trying to put the tree definition in a text file. Unless you’re a genius who can come up with something totally unique, most people would resort to using a markup and content strategy where where they would indicate starting and ending of a container concept such as room with a markup, or you can say some sort of starting and end markers making a sort of outline, and contained within the markup would be content, which can itself be markup, or markups and other content.

Now simply put this intuitive way of representing a hierarchy within a text file created in notepad is called XML. There’s no rocket science here. Well i better save this until after I have stated the formal definition of XML, which you need to just read and I’ll explain it in a sec.

So formally, characters making up an XML document are divided into markup and content which may be distinguished by the application of simple syntactic rules.

If you create the home text file using XML rules, the resulting file would be known as XML document. Logically, the document is composed of declarations, XML elements, comments, character references and processing instructions, all of which are indicated in the document by explicit markup. Now let’s go over these rules of XML document and rest assured again I’ll explain everything once the formal stuff stops making sense to you.

So a text file is a valid XML document if

  • It contains one or more elements
  • The topmost element is called the root or document element
  • For all other elements if start tag is in content of another element, the end tag is in the content of the same element. In other words, the elements delimited by start and end tags nest properly within each other.

In case you are feeling lost and wondering what does this all mean, let’s return to plain English now and understand elements and tags better.

XML Elements

So you might have understood by now, ‘elements’ are the basic building blocks of XML. They consist of three parts,

  • A start tag, which looks like tag name enclosed in left and right arrows like section or kitchen,
  • Content such as I’m content of section element
  • And lastly the end tag indicating end of element same as star tag but with a forward slash after the left Arrow.

Another example closer to home element from the text file you created, and this is a complete element now.

<Home>
This is my home where I eat sleep and sometimes work
</home>

This home element example is a perfectly valid XML document, where home element is a root element describing your home. This document contains only one element. Note if there were no line breaks and entire xml document was on a single line, it would still be a valid XML document.

Tags

We used the word tags, so let’s explore tags a little bit more.

Each element has a start and end tag. The opening tag of an element is written between < and >. The ending tag in comparison is written between <, followed by a forward slash / sign, and lastly the right arrow (>) sign. Data between starting and ending tags of an element are its contents. Do note that tags are always case sensitive.

I hope you understand what XML elements are now, and we are halfway through being functional with XML if you got that far.

An XML document contains a hierarchy of elements which means content of an element can be other elements. The topmost element in XML document is called the root element, and contains all other elements which are called child elements.

Below we have a more detailed example of what an element with nested hierarchy looks like.

<home>
<garage>A room in home where cars are parked</garage>
<kitchen>A room where food is prepared </kitchen>
This is my home where I eat, sleep and sometimes work!!!
</home>

As you can see, both single line and multi-line home elements are perfectly valid XML. Have a close look at the example and understand it. In our example, the ‘home’ element is the top level or root element, and there can be only one root element in XML.

It is also a parent element having two child elements garage and kitchen, and also has its own text content. In other words, its content is composed of two child elements and a description. As for terminology, the home element is not only the root element, it is also the parent element for garage and kitchen elements. Conversely, garage and kitchen elements are children or child elements of Home element. Garage and kitchen also are siblings of each other since they share a common parent.

Except for the root part which is reserved for top level element, all the other rules remain the same for all elements in XML. This means kitchen can have its own hierarchy where it would be the parent element of its child element like stove or microwave elements. Do you remember the rule and formal definition which said start and end tags must be in the same element? It means the element, in its entirety, must fit within content of its parent element content. That is between its starting and end tags, and it must not overlap with any of its siblings.

To clarify this point, below is example of an ‘invalid xml’

<home>
<garage>
A room in home where cars are parked
<kitchen>
</garage>
A room where food is prepared
</kitchen>
This is my home where I eat, sleep and sometimes work!!!
</home>

Note it is invalid as starting tag of kitchen is in content of garage which is not allowed, so repeating this is example of an ‘invalid’ XML. Also note that content of an element is optional, but start and end tags are not. So <kitchen></kitchen> is a perfectly valid XML and such elements are called empty elements.

Attributes

Let’s now talk about another extremely important Concept in XML called attributes. Attributes are like adjectives for the elements, that is they describe the element or provide information about the elements a sort of metadata about elements you can say as it probably makes sense they are set on the element in the start tag and start tag only as name value pairs. Before going further an example would be good so have a look at the movie element.

<movie genre="action" rating="R" language="english">Die Hard</movie>

Here genre rating and language are attributes of a movie being represented by the movie element, and they are all defined as name value pairs in the start tag. Note the double quotation marks as they are strictly necessary. As you might have guessed by earlier content, attributes themselves are optional, and can only appear once within an element.

Entities

Another less talked about but very important feature of XML particularly in context of web development are entities. Entities are Escape characters or placeholders for reserved words of XML. A horrible choice of word I must say as entity intuitively means something concrete, and a better word would be escaping but, oh well let’s move on with it.

As an example the less than (<) and greater than (>) characters are reserved characters for XML, so if your content contains these characters say for example you are making an XML about math and some equation containing less than greater than or ampersand appears in content, it would mess up the XML. Not too big of an issue for you as a human maybe since you can use common sense, but for non-human readers like XML parsers they ARE a big deal. So you need to store them a bit differently in XML file form to get around that problem.

Get Ali Aslam’s stories in your inbox

Join Medium for free to get updates from this writer.

There are five predefined entities in XML and note the semicolon is mandatory so

  • &lt; represents the left Arrow or less than sign (<)
  • &gt; represents the greater than sign (>)
  • &amp; represents ampersand
  • &apos; represents a semicolon
  • &quot; represents double quote

Note that these five entities are defined at XML level. If you are evaluating XML in context of web development, note that HTML which is made up of XML has a ton of more entities/escape characters for all kind of stuff like dollar sign, up down arrows, colon, asterisk, Latin characters and whatnot. I won’t go over that list here but you can Google HTML entity reference list to know about these. Lastly do remember the purpose of entities is to avoid confusion regarding what XML syntax is, compared to content.

Again coming up with an example so ‘he said I think 5 > 2 is the correct answer’ in plain English, is stored as ‘he said I think 5 &gt’ 2 is the correct answer’ in an XML document.

Comments

Comments are another useful feature of XML, and if you have ever done any coding its importance is hard to ignore. Sometimes you want to add comments to XML but don’t want the parser to read it as part of XML. Luckily sense prevailed and XML standard has support for proper comment sections via special tags. These are start tag being `<! —` (Left arrow, exclamation mark and two dashes) and end tag as — → (Two dashes and right arrow). Anything in between would be considered a comment. Note that comments cannot appear within XML markup itself, that is you can’t have comments in a tag. Furthermore since content of comments are totally ignored by parsers, the rules of XML do not apply to what can appear in content of comment.

A good example of this can be the comment below

<home>
<!-- No escaping or entity necessary to escape <section> or & in this comment-->
This is my home where I eat, sleep and sometimes work!!!
</home>

Finally we have perfectly valid example of a comment in which you can clearly see we are using arrows and and ampersand signs and all, and this is totally valid XML.

CDATA

You would surely come across CDATA sections in XML if you work with it. It is a piece of element content that is marked up to be interpreted literally as textual data, and not as marked up content. In other words, the content of CDATA can be interpreted as XML, but it should not be. This is useful if you are saving draft of an article about XML, containing numerous examples of XML usage, as XML. How is it different from comment you might rightfully ask, again emphasizing, comment is ‘NOT A PART OF XML’, while CDATA ‘is’.

so I have provided an example here


<home>
<![CDATA[
Here i can use - , <, &, ', and " and pretty much any XML markup with impunity. Only example being the double closing square brackets end tag.
]]>
This is my home where I eat, sleep and sometimes work!!!
</home>

As you read in the example you can use whatever you like in CDATA except its ending tag, which would result in error. There is no escape character for this, so the only remedy is to close the CDATA section at this point, use the double square bracket closing as text content, and then start another CDATA section.

XML Processing Instructions

XML is supposed to be read by both humans and non-humans for non-humans, and you can include processing instructions which are used to pass information to applications which use this information to execute special tasks. The syntax use is <??> so giving an example

<?xml version ="1.0" encoding="utf-8"?>

Here information for XML parser includes the version and encoding of that XML document. From web develop perspective, a good example would be directive XML stylesheet

<?xml-stylesheet type="text/xsl" href="mystylesheet.xsl"?>

This lets the parser know that there exists a stylesheet for this XML document, and while showing it in a browser the XML content should be shown as per styling given in the stylesheet file. Just something to be aware of at this stage that this is possible. You guys need to either look up xslt and XML style sheets yourself or let me know if you would like me to write an article on the topic.

Basically stylesheets allow rendering an XML like a web page, where you can style each element individually. As an example an XML would be rendered as a table where rooms row would be a light blue background and each item would be rendered as a list. You can sort items conditionally, show or hide elements, and even add or remove elements and attributes during such rendering. This is a very powerful feature called extensible stylesheet language Transformations or short XSLT.

Do note that XML itself is not supposed to contain any styling information, and is primarily a data store. However styling information can be augmented via style sheets to do it robustly. I’ll stop here on the topic now otherwise it is gonna take rest of the article.

XML Schema Definitions

I would not conclude coverage of XML without mentioning the exciting feature of XML schema or XSDs, also known as XML schema definitions. They are kind of a type definition of XML enforcing constraints on what the XML must look like. Here’s an example for you to see how an xsd looks like from w3schools.com website

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>

Sadly we cannot go in details of XSD beyond this point in the article as this is a whole new topic. If interested, again let me know and I’ll happily write an article about that. But for now, being aware of the options existing is enough.

XML Validations

Another associated concept is XML validations, which take your XML along with an XSD or DTD (which stands for document type definitions), and validate that XML is well formed. Meaning that it follows all rules of XML as well as your data type definitions or XML schema. Any errors identified are highlighted. Really cool feature to ensure your data is correctly stored as XML.

XPath and XQuery

Then there are XPath and xquery Concepts. As I mentioned, XML files allow you to store data in text form readable by both humans and machines. So far we have seen how to create XML properly but once an XML is created the purpose is to be able to read and consume that data efficiently. XPath and XQuery are to XML as SQL is to database. They allow you to query data from XML in various forms. Examples are worth a few hundred words at least, so let’s look at XPath expressions below


/home/room[1] Selects the first room element that is child of the home element

/home/room[position()<3] Select the first two room elements that are children of home element

/home/room[color="blue"] Select all rooms which have color attribute containing blue value

/home/room[color="blue"]/cupboard Select all cupboard elements of the room elements with blue paint

As you can see there are multiple ways of querying data. Pretty cool right? You can query XML in all sort of ways and we just gave a few beginner examples only. XQuery is even more powerful. Just have a look at this example

for $x in doc("bookshop.xml")/bookstore/book
where $x/prince > 30
order by $x/title
return $x/title

Pretty self-explanatory and amazingly powerful. Note that XQuery uses XPath under the hood.

XML has been around for a while, and before JSON, it was kind of a defao standard for data transmission as well. No wonder there are so many cool features built around it since we can do all this stuff with XPath and XQuery.

Document Object Model

Another related concept is XML document object model, and its successor HTML document object model commonly referred to as DOM. Both allow an XML document to be parsed into an in-memory object, and manipulated/queried via object syntax. In languages like JavaScript there are standard functions to get, change, add and delete elements, attributes and content, and it is easy as XML is accessible via properties and methods. If you haven’t worked with JavaScript objects yet no worries. You most probably will rub by document object model or DOM at some stage.

Since an XML can be parsed into DOM, the next inevitable concept is XML parser which makes it happen. XML parser comes into play before DOM, but since it creates the DOM, we can’t discuss it before DOM right? Anyway, the job of parser is to read the XML in text form interpret it, like identify elements, attributes and their relationships, and then create an in-memory object called DOM from that information, allowing you, the user to manipulate it much more conveniently than manipulating a regular text file through code. All major browsers have built-in XML parsers for that reason.

Alright this brings us to end of discussion on XML if you feel fuzzy with so much terminology it’s probably okay. My intention was to generally introduce you to XML in detail but also scratch the surface of its usage ecosystem. Even having an intuition about what is possible is useful when you are presented with a challenging user situation, where you can explore more on the topic and find the most efficient way to solve the problem instead of crafting your own solution. If you liked, please clap/share and subscribe to our YouTube channel to be notified of exciting content like this. Please also check out my article series on prompt engineering, web development and internet security.

Thank you.

Press enter or click to view image in full size

--

--

Ali Aslam
Ali Aslam

Written by Ali Aslam

Avid Learner | Principal Software Engineer | Full Stack Developer