Apprentice XML

Begin your journey to the XML world with Root, Attributes, Namespaces and many more.

Posted by Rishi Raj Gujadhur 1/17/2016


Table of content

 

Prerequisites

     

A. What is XML?

 

 
  • XML stands for eXtensible Markup Language.
 
  • XML is a tag-based syntax like HTML <h1> </h1>, however you can create your own tags.
 
  • XML is used to structure and describe information.
 

B. XML Trees


A simple XML tree has a root and an element.

Example of a simple XML document.

1. In Notepad ++, type:

<store>
<product>

PS4
</product>
</store>

 
<store> ... </store> ----> Root element
<product> ...</product> ----> Element within the root element

2. Save the document as Product.xml.

3. Run the document in the web browser. 

Result: 

Lets at a slightly more complex XML tree.

1. Type:

<Food>

<Fruits>

<Fruit1>
Coconut
</Fruit1>


<Fruit2>
PineApple
</Fruit2>


</Fruits>

</Food>
 
<Food> ... </Food> ----> Root element; enclosing all other elements

<Fruits>...</Fruits> ----> Child of root element Food

<Fruit1> ... </Fruit1> ----> Subchild of the root element Food

<Fruit2> ... </Fruit2> ----> Subchild of the root element Food

2. Save as Food.xml

2. Run in the web browser.

Result: 

Now lets look at a XML tree having attributes:
 
  • When coding in HTML we can add attributes to tags to style them. However in XML attributes are used to differentiate information as shown in the example below. 
 
  • Also XML attributes are often use as metadata (information describing other information); for example order IDs.

1. Type:

<store>

<Game category="PS4">
<Title>
Driveclub
</Title>
</Game>


<Game category="Xbox One">
<Title>
Froza Horizon 2
</Title>
</Game>


</store>
 
category="PS4" ----> attribute
category="Xbox One" ----> attribute

2. Save as Store.xml.

2. Run in the web browser.

Result: 
 

C. Rules 


XML elements:
 

  • requires an openning and a closing tag <...> </...>
 
  • are case sentitive. <A>...</A> is different from <a>...</a>
 
  • requires proper nesting as show below.

Correct nesting
<a>
<b>...</b> 
<a>


Incorrect nesting
<a>
<b>...</a>
</b>


XML attributes values requires quoting "PS4". <game category="PS4">...</game>
 

D. XML Comments


Creating XML comments is fairly simple as shown below.

<!-- Hello World -->
 
<!-- -----> Start of the comment.

Hello World ----> Comment

--> ----> End of the comment.
 

E. Namespaces

 
  • Conflicts occur when your using 2 XML document with similar tags. To avoid conflicts we use prefixes. In order to use a prefix in XML, we need to define a namespace for the prefix.
 
  • The xmlns attribute is utilize to define a prefix as shown below.
 
<root>

<f:table xmlns:f="FruitsURI">
  <f:tr>
    <f:td>Coconuts</f:td>
  </f:tr>
</f:table>

 
<f: ----> Applying the prefix.

xmlns:f="Fruitsurl" ----> Defining a unique namespace; example a specific url for that prefix "f".

<v:table xmlns:v="Vegitablesurl">
  <v:name>Carrots</v:name>
</v:table>
 
xmlns:v="Vegitablesurl" ----> A different prefix used for a different kind of information.

</root>
 

F. Next Step


Feel free to practice all the concepts you learned in this tutorial using Notepad ++.