Xml Basics

XML stands for eXtensible Markup Language. XML is designed to transport and store data.

XML namespaces – XML Namespaces provide a method to avoid element name conflicts. In XML, element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications.
This XML carries teacher  information:

<table>
<name>Teacher1</name>
<id>4</id>
<age>43</age>
</table>

This XML carries student information:

<table>
<name>Student 1</name>
<id>124</id>
<age>13</age>
</table>

If these XML fragments were added together, there would be a name conflict. Both contain a <table> element, but the elements have different content and meaning. A user or an XML application will not know how to handle these differences.

Name conflicts in XML can easily be avoided using a name prefix. When using prefixes in XML, a so-called namespace for the prefix must be defined. The namespace is defined by the xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix=”URI“.

</pre>
<root xmlns:t="https://djcodes.wordpress.com/teacher"
xmlns:ts="https://djcodes.wordpress.com/student">
<t:table>
 <t:name>Teacher1</t:name>
 <t:id>4</t:id>
 <t:age>43</t:age>
</t:table>
<s:table>
 <s:name>Student 1</s:name>
 <s:id>124</s:id>
 <s:age>13</s:age>
</s:table>
</root>

Note: The namespace URI is not used by the parser to look up information. The purpose is to give the namespace a unique name. However, often companies use the namespace as a pointer to a web page containing namespace information.

Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource. The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Universal Resource Name (URN).

An XML document with correct syntax is called “Well Formed”. A “Valid” XML document must also conform to a specified document type.  An XML document validated against a DTD is “Well Formed” and “Valid”.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE address SYSTEM "Address.dtd">
<address>
<street1>street address</street1>
<street2>street address</street2>
<city>SomeCity</city>
<state>SomeState</state>
</address>

The DOCTYPE declaration, in the example above, is a reference to an external DTD file.


<!DOCTYPE address
[
<!ELEMENT address (street1,street2,city,state)>
<!ELEMENT street1(#PCDATA)>
<!ELEMENT street2(#PCDATA)>
<!ELEMENT city(#PCDATA)>
<!ELEMENT state(#PCDATA)>
]>

The purpose of a DTD is to define the structure of an XML document. It defines the structure with a list of legal elements. #PCDATA means parse-able text data.

With a DTD, your XML files can carry a description of its own format. With a DTD, independent groups of people can agree on a standard for interchanging data. With a DTD, you can verify that the data you receive from the outside world is valid.

An XML Schema describes the structure of an XML document, just like a DTD. An XML document validated against an XML Schema is both “Well Formed” and “Valid”. XML Schemas are More Powerful than DTD. 

  • XML Schemas are written in XML
  • XML Schemas are extensible to additions
  • XML Schemas support data types
  • XML Schemas support namespaces

For the previous address xml , an xml schema might look like this:


<xs:element name="address ">
<xs:complexType>
 <xs:sequence>
 <xs:element name="street1" type="xs:string"/>
 <xs:element name="street2" type="xs:string"/>
 <xs:element name="city" type="xs:string"/>
 <xs:element name="state" type="xs:string"/>
 </xs:sequence>
</xs:complexType>
</xs:element>

 

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: