XML Concepts and Syntax

In this lesson we’re going to be taking a look at XML concepts and doing a deeper dive into XML. How it’s formatted, the types of things it uses to define its structure, and its schema. We’re going to talk about the XML schema definitions and how we create those.

This way we can speak intelligently about the XML API, and the commands that we’re putting in and information we’re getting out.

XML is used to communicate to Junos infrastructure by automation tools through the XML API

First up, XML stands for eXtensible Markup Language. XML is used for both on-box and off-box automation. Using XSLT, SLAX or Python on-box. We saw previously that the XML API is really core and key to the Junos automation stack. That XML is really what it’s using in the background. Even when we’re at the CLI, typing in commands, Junos is converting those to and from XML for us. So that’s really what it’s interacting with natively on the box.

XML is a standards based markup language, it is a World Wide Web Consortium standard, W3C. You can take a look on their website for more information from W3C about XML to get some more details about how it’s structured and used, and some more advanced features of XML if you like. In this lesson we’re really just going to be taking a brief look at the syntax, namespaces and schema definitions so that we can be able to work with the Junos XML API intelligently.

XML Syntax

XML syntax

XML has opening and closing tags a lot like HTML. If you’ve ever taken a look at HTML web pages, the structure of XML is actually very similar. Tags are indicated by opening and closing angle brackets, or a < and > signs. In the example above, it has an opening tag with some name in the middle and then a closing tag. The closing tag has an opening bracket, and then a forward slash, and then the name. This name must correspond to the opening tag name.

Empty XML elements can be represented with the slash on the right

In between these two tags we have some data. This can be empty, as we see in the example above we have empty tags, where we have this “name” tag that opens and closes with no data in the middle. Now, if you have no data and it is empty, you can shorten this by using this shorthand specifying that this is just an empty tag. These two in the example above are equivalent. This is the fundamental structure of XML.

We have tags, and this is an element. We have some data within that element.

XML Hierarchy

Moving forward a little bit here, XML is hierarchical in nature. Hierarchy in XML is represented by nesting elements.

XML hierarchy example

In this case we are describing a video using XML. Within this video tag, we have a name element, and the name is “XML Concepts and Syntax”, and then we are closing that name element and closing the video element.

I want to mention that XML does not use indentation to specify hierarchy, it is not white space sensitive. This indentation I have just put for easier readability through our presentation. If you are getting an xml output from a device, it may not have any indentation for the different sections of the hierarchy, so it could all just be in one long line of text and have these opening and closing tags, which is completely valid XML.

XML multiple hierarchy example

Multiple hierarchical levels can exist, with multiple elements at each level. Here we’re describing our video once again. We have the opening tag for our video, the name of this presentation is our “XML Concepts and Syntax”, then we are describing each of the slides within the video. The first slide has a “name” element with the text “XML”. The second slide’s name element contains “XML Syntax”.

There are a few terms that we want to be sure to know, one is an XML element node. An element node is everything in between a particular opening and matching closing tags.

Another term to know is the document node. The document node describes the entire XML document.

Where everything between the opening <video> and closing </video> tag is an element node, everything in the document is the document node.

XML Namespace

Example of using XML namespaces

Let’s talk about how we structure XML documents in a consistent fashion so that all of our applications are able to read them in a consistent manner. First up, we use namespaces. So, namespaces ensure that XML element nodes have unique names. We can have multiple element nodes of the same name though.

We can use as many namespaces as we would like. In our example XML above we see xmlns this is namespace. We are defining some URL for our namespace, and we can use the xmlns illustrate namespace by using the colon as shown. This allows us to define a separate namespace.

XML Schema

Finally we have our XML schema definition files. These are .xsd files, XML Schema Definition files. They define the XML file data structure.

Example XSD on the left and compliant XML file on the right

These are actually really important files. They will determine what nodes are allowed to have what kind of data, and whether those nodes are required or not. They provide a set of rules to be able to define whether we have valid XML file syntax or not. XML schema files will allow you to create XML files that are consistent in formatting and in content.

They define what elements are allowed in an XML document and their hierarchy. They ensure XML files for the same purpose are written in a standard format using standard tags.

The XSD files are actually themselves written in XML format. Above on the left side we have an XSD file, and on the right side we have an XML file that follows this XSD schema. We see our colored items which correspond to the definition and the corresponding XML element within the XML document.

Only subscribers can view the full content. Join Now!

Scroll to top

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

CiscoLessons will use the information you provide on this form to send occasional (less than 1/wk) updates and marketing.