XML Tutorial – a Beginner’s XML Guide

arp14 Tutorial, XML Tags: , , , , , 0

In this XML Tutorial, we are going to learn what is XML, XML Declaration, CDATA, Processing Instructions, Entity Reference, XML Elements, XPath, XSLT, DTD, XML Tags & Attributes and XML Schema. If you are a beginner at XML, then this tutorial is perfect for you to get started with XML.

XML Tutorial - a Beginner's XML Guide

XML stands for Extensible Markup Language. XML was designed to transport and store data. XML is important to know, and very easy to learn. see more on wikipedia… Before learn XML, you should have basic knowledge about HTML and JavaScript. You can See other tutorials on HTML and JavaScript from links below –

What is XML?

W3C says:

  • XML stands for EXtensible Markup Language
  • XML is a markup language much like HTML
  • XML was designed to carry data, not to display data
  • XML tags are not predefined. You must define your own tags
  • XML is designed to be self-descriptive
  • XML is a W3C Recommendation

Contents of XML Files

XML declaration

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

Character Data Section aKa CDATA

<![CDATA[Here is some text and data...]]>

Processing Instructions

<?SpellcheckMode mode="en-english"?>

Entity References

Character (&#60;) and general (&conpyright;).


Valid Elements


Invalid Elements

  • XML Rules are specified in either DTD (Document Type Definition) files or XML Schema files.
  • Importing/Locating CSS to an XML File – (By passing instruction)
<?xml-stylesheet type="text/css" href="style.css"?>


Path ExpressionMeaning
/Find the root tag in the document
/root_tagFind the root tag, but only if it named “root_tag”
//element_aFind all element_a tags, whereever they available in the document
text()Selects the text content of the current NODE
@nameSelects the name attribute of the current NODE
/doc/chapter[5]/sec[2]Select the 2nd sec of 5th chapter of the document
body/p[last()]Selects the lasr “p” tag in the “body” tag
..Selects the parent of the current NODE
/html/body/p[@class=”a”]Selects all “p: tags in the body under HTML tag that have a class attribute whosevalue is “a”
//p[@class and @style]Selects every “p” tag in the document that has both a class attribute and a style attribute


XSLT is recommended Stylesheet Language for XML.

XSLT Header

<xsl:stylesheet version="1.0"
// or,
<xsl:transform version="1.0"

XSLT Footer


Some Tags

Some Useful Tags
<xsl:teemplate match=/tagName”>Template for matching tagName
<xsl:value-of select=””./>Select all from current document
<xsl:for-each select=”/tags/tags”>For each loop for XSLT
<xsl:attribute name=”src”>Pick the src attribute
<xsl:text>Generate some text via XSLT
<xsl:if test=”@attrName=’value'”>If statement conditioned by “test” attribute / “@” means attribute in short.
<xsl:when test=”@attrName=’value1′”>
<xsl:when test=”@attrName=’value2′”>
Similar to switch case statement
<xsl:sort select=”tagName” order”ascending”/>Sorting data via XSLT. This cannot be done with CSS

DTD – Document Type Definition

DTD stands for Document Type Definition. You can write or define bunch of rules for a XML document in DTD file. Example:

<!DOCTYPE businessCard [
	<!ELEMENT BusinessCard(Name,Phone+,email?)>
	<!ELEMENT phone(#PCDATA)>
	<!ELEMENT phone type(mobile | fax | work | home)#required>
	<!ELEMENT email(#PCDATA)>

DTDs can contains declaration for


Elements can contain

  • EMPTY – No children / ANY no content constraints.
  • ELEMENT -The listed child element, But thats it.
  • MIXED – Element and Character data.

Declaring Attributes

<em>Required:</em> <!ATTLIST PACKAGE ontime (yes|no) #REQUIRED>
<em>Implied:</em> <!ATTLIST PACKAGE ontime (yes|no) #IMPLIED>
<em>Default Value:</em> <!ATTLIST PACKAGE ontime (yes|no) "yes">
<em>Fixed:</em> <!ATTLIST PACKAGE ontime (yes|no) #FIXED "yes">

Specifnig Element Population

“?” => Zero or one of the indicated element.
“+” => One or more of the indicated element.
“*” => Zero or more of the indicated element.

External DTD

On XML file:
<!DOCTYPE TgaName SYSTEM "file.dtd">
On DTD file: 
<?xml version="1.0" encoding="utf-8"?>
	<!-- Here is all DTD contents -->

XML Schema

An XML Schema describes the structure of an XML document. XML schema is Advanced Complex Ruler of XML. XML Schema extension is “.xsd”.

XML Schema Header for a XSD file:

<xsd:schema xmlns:xsd="http://www.w3c.org/2001/XMLSchema">

Some Tags and Declarations

<xsd:element> : Element Declaration having simple on complex Elements can also have mixed, empty or element content Min-Max time allowed to occur / can be restricted to having certain values.

Built-In Simple Type

xsd:string; xsd:boolean; xsd:decimal; xsd:integer; xsd:positiveInteger; xsd:negativeInteger; {xsd:date; xsd:time(I.e. 1972-11-14, 12:14:00.00/18:00:00.00(6PM) Respectively)}; xsd:dateTime(CCYY-MM-DDhh:mm:ss.ss); xsd:gMonth(–MM–); xsd:gYear(CCYY); xsd:gDay(—-DD); xsd:gYearMonth(CCYY-MM); xsd:anyURI(A valid URI as http://cberster.net). Example:

<xsd:element name="TagName" type="xsd:string"/>
<xsd:element name="TagName" type="xsd:string"/>

Name: This is looking for tag / element name.
Type: This is restricting it's type as "string", "date" and so on...

<xsd:element name="friend" type="xsd:string" minoccurs="1" maxoccurs="unbounded"/>
<xsd:element name="phone" type="xsd:integer" maxoccurs="5"/>
Elements population can be limited using the "minoccurs" 
and "maxoccurs" attributes. Default value for each is 1; each is optional. 
"Unbounded" means can be occurs as many times as required.

	<xsd:restriction base="xsd:positiveInteger">
		<xsd:minInclusive value="0"/>
		<xsd:maxInclusive value="50"/>

Above code not only restricts to positive integer, but also restricts in between 0 to 50.
Restricting content to allowable choices:
<xsd:enumeration value="value"/> => goes in restriction tag.
<xsd:pattern value="\d{3}-\d{2}-\d{4}"/> => goes in restriction tag.
Regular Expression Define writing string in a pattern of characters
that allowes to peer for given value.

Declaring elements with complex type

Use "<xsd:anyType"> value for the type attribute.
Use the "<xsd:complexType>" tag in the definition.
Use "<xsd:complexType mixed='true'>" to allow Mixed content.
Use "<xsd:sequence>" tag before type tag for sorting.
[!] Al bottom level elements goes to in "<xsd:sqequence>" tags.
<xsd:choice> => in before type tag alternative to sequence tag. But 
elements must be (resultant) one of them.
<xsd:all> => for all, unlike above, - can be append anywhere

Declaring attributes

<xsd:attributes name="tag" type="xsd:positiveInteger"/>
- In additional to "name" and "type" can have "use", 
"optional", "required", "prohibited".
Use = "optional", default = "20" (default || fixed).

I hope this XML Tutorial help you. If you need help – having problems with XML? Just comment below. I will be back to you.

Thank you !

Leave a Reply

Note: Your email address will not be published. Required fields are marked *