XML Basics

XML stands for Extensible Markup Language, which is widely used Message Exchange pattern along with JSON. XML format is similar to HTML, but no predefined tags.

In this post I’ll focus on explaining the concepts for real time usage, instead giving text book definitions.

First let’s see below sample XML code:

2.png

Now let’s see another example where we write employee details for two different organizations.

3.png

In the above example, we have two employee records belong to two different organizations. So we can not use same element name like <employee> for both the organizations. So to overcome this we use the Namespace concept. Here in the example we’ve used two different namespaces for two organizations with the namespace prefixes as ‘orgA’, ‘orgB’ and declared these name spaces at the root element. XML Namespaces provide a method to avoid element name conflicts in an XML document.

If you observe the namespace declaration it seems like an URL, But actually it is  NOT an URL and you can’t open it in browser. You may doubt then why a URL notation is followed for namespace declaration when it is actually not an URL, fact is that it is just a standard that we follow. (If you don’t follow standards you can even declare like xmlns:orgA=“orgA/schemas/Employee”) but is not recommended. 

Now let’s jump into XSD (XML schema document) : 

An XSD document describes the structure of XML document. For the above example we’ll write an XSD document will be as given below.

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.emp.org" 
 targetNamespace="http://www.emp.org" elementFormDefault="qualified">
 <xsd:element name="Employee">
 <xsd:complexType>
 <xsd:sequence>
 <xsd:element name="employee" maxOccurs="unbounded">
 <xsd:complexType>
 <xsd:sequence>
 <xsd:element name="name" minOccurs="0" type="xsd:string"/>
 <xsd:element name="id" type="xsd:integer"/>
 <xsd:element name="email" type="xsd:string"/>
 <xsd:element name="phNum" type="xsd:string"/>
 </xsd:sequence>
 </xsd:complexType>
 </xsd:element>
 </xsd:sequence>
 </xsd:complexType>
 </xsd:element>
</xsd:schema>

In the above example if you observe, elements defined as ‘complexType’ are meant that they have child elements. Element <employee> in xml has child elements under it, so its child elements are wrapped in a compleType tag followed by a sequence tag.

4.png

Note: Here in the above image we have element ‘Employee’ and another element ’employee’ under it and then child elements under it. You can directly give all the child elements under element ‘Employee’ instead under element ’employee’.

If you have observed in XSD file there is ‘targetNamespace’ given. Don’t confuse with the Namespace.

  • TargetNamespace is nothing but defining the namespace for all the elements that you define in XML schema.
  • Any element that you define within the schema would be associated with the target namespace. You you’ve defined prefix for the TargetNamesapce then all the elements in the XSD file are associated with the TargetNamesapce using the prefix defined (all element names start with prefixName: followed by element name).
  • If you don’t define target namespace to the Schema, it results in conflicts. So it is advised to give TargetNamesapce always.
  • Each element can have a different namesapce associated with it, but all those elements defined with in the schema should be aligned to a single target namespace. This is just a declaration and make sure you do not use predefined namespace (soap, soap12 etc) for the targetNamespace.

Now let’s see more examples to understand more about XSD:

For the below XML, there are two elements with same name but with different namespace. If you generate XSD out of this XML document using JDeveloper you’ll get an XSD in which you can see only one element is created for both the elements. But this is not correct to use in real time. Better to have a separate declaration for two different <employee> elements in XSD file also.

5.png

You can create an XSD for the above XML as given below also, where two separate elements created though having same name for the elements in XML. This is recommended over above.

6.PNG

In the above example, there are two element with same name (employee), so it is difficult for you to work with this kind of XSD file in SOA. So it is always advisable to have different names. such as employee_orgA, employee_orgB.

7.PNG

 

Now assume if the two element with same name ’employee’ have sub elements with same name and one element has one more extra field, then you can prepare your XSD as given below.

8.png

The source code for the above will be as given below.

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.emp.org" targetNamespace="http://www.emp.org"
 elementFormDefault="qualified">
 <xsd:element name="Employee">
 <xsd:complexType>
 <xsd:sequence>
 <xsd:element name="employee_orgA" maxOccurs="unbounded" type="emp_commonType"/>
 <xsd:element name="employee_orgB">
 <xsd:complexType>
 <xsd:sequence>
 <xsd:element name="empCommon" type="emp_commonType"/>
 <xsd:element name="address"/>
 </xsd:sequence>
 </xsd:complexType>
 </xsd:element>
 </xsd:sequence>
 </xsd:complexType>
 </xsd:element>
 <xsd:complexType name="emp_commonType">
 <xsd:sequence>
 <xsd:element name="name" type="xsd:string"/>
 <xsd:element name="id" type="xsd:integer"/>
 <xsd:element name="email" type="xsd:string"/>
 <xsd:element name="phNum" type="xsd:string"/>
 </xsd:sequence>
 </xsd:complexType>
</xsd:schema>

 

Another XSD document for a sample XML document can be created as given below.

9

The above XSD can also be written as given below.

10

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s