XML syntax rules are very logical and simple. XML document must contain only one Root element that is the parent of all other elements in the document.  

<root>
    <child1>
         <subChild1>...</subChild1>
         <subChild2>...</subChild2>
    </child1>
</root>

There are five things to know about the XML syntax:

  • XML Prolog
  • Tags and Elements
  • Attributes
  • Text
  • References

XML Prolog

XML prolog is an optional line of code that if exists, must come first in the document.

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

It is pretty much self-explanatory - version is the XML version and encoding specifies the character rules. In the above example, XML version 1.0 is shown. There is XML version 1.1 that allows the use of scripts and characters absent from Unicode but it is rarely used and supported.

Note that the XML prolog does not have a closing tag. This is not an error. It is just not a part of the XML document at all.

XML Tags and Elements

A tag is a markup construct that begins with < and ends with >. There are three types of tags:

  • start-tag: It is placed before the content of an element and is typically called an opening tag. An example would be the opening tag <speed> shown in Figure 1.
  • end-tag: It is placed after the content of an element and is typically called a closing tag. An example would be the closing tag </speed> shown in Figure 1.
  • empty-element tag is a single line component such as <line-break /> that has a specials purpose and does not contain any value.

XML tags are also called XML Nodes or XML Elements. The names of XML Elements are enclosed in brackets <> and must have a closing tag as shown:

Figure 1. Example of an XML element

The value between the start and end-tags, if any, is the element's content and may contain additional XML markup including other elements called child elements. 

XML tags are case sensitive. The tag <Speed> is different from the tag <speed>. Opening and Closing tags must be written in the same case.

INCORRECT:
<Speed>1000</speed>

CORRECT:
<speed>1000</speed>

XML tags must be properly nested. XML elements must not overlap - meaning end tag of an element must have the same name as that of the most recent unmatched start tag.

INCORRECT:
<speed>
    <duplex> full
</speed>
    </duplex>

CORRECT:
<speed>1000</speed>
<duplex>full</duplex>

XML Attributes

XML elements can have attributes, just like HTML. Attributes contain data related to a specific element.

EXAMPLE 1 - Using Attributes
<interface name="GigabitEthernet0/0/0">
    <address>10.1.1.1</address>
    <mask>255.255.255.0</mask>
</interface>

EXAMPLE 2 - Using Elements
<interface>
    <name>GigabitEthernet0/0/0</name>
    <address>10.1.1.1</address>
    <mask>255.255.255.0</mask>
</interface>

There are no specific rules when to use attributes or when to use elements in XML. Both examples above provide the same information.

XML Text

In XML documents, all whitespace characters are ignored but are preserved. XML does not truncate whitespace like HTML. Some characters like (< > ' " & ) are reserved by the XML syntax itself. All XML files must be saved as Unicode UTF-8.

XML References

References allow you to include additional markup in an XML document. They always start with the reserved symbol "&" and end with a symbol ";". There are two types of references:

Character Reference - for example, &#65; refers to alphabet "A". 

Entity Reference - for example &gt; refers to '>'

 

Comments

RBNetEngr

Thu, 12/23/2021 - 21:30

Figure 1 is missing. And the Incorrect Nested example seems to be missing the '1000' for speed