Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Tuesday, July 15, 2014

Validating XML

Hi...

Today, we will discuss further more about XML.

Validating XML

We can use either schemas or DTD (Document Type Definition) to validate an XML file. DTD is an obsolete way of validating XML. Schema is widely used now and has better features than in DTD and they are more powerful in may ways.
  • The strongest point of schema is, it supports data types. Therefore the inputs of the XML document can be validated against the data types.
  • Also schema can have restrictions to input data. Therefore it is secured to use schema.


Writing XSD (XML Schema Definition)

First we need to write an XML schema to validate the XML document.

We will create an XSD for the following XML document.

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/home/dinithi/workspace/PhoneShop/src/StyleSheetXML.xsl"?>
<mobilephones>
 <mobilephone IMEI="78909875433455">
  <model>D6563</model>
  <name>Xperia Z2a</name>
  <brand>Sony</brand>
  <dimensions>
   <height>137mm</height>
   <width>72mm</width>
   <depth>11mm</depth>
  </dimensions>
  <weight>163</weight>
  <feature>
   <os>Android v4.4.2</os>
   <CPU>Quad-core 2.3 GHz</CPU>
   <browser>HTML 5</browser>
  </feature>
 </mobilephone>

 <mobilephone IMEI="0876666339988">
  <model>VS876</model>
  <name>Lucid 3</name>
  <brand>LG</brand>
  <dimensions>
   <height>131.6mm</height>
   <width>66mm</width>
   <depth>9.9mm</depth>
  </dimensions>
  <weight>123.9</weight>
  <feature>
   <os>Android v4.4.2</os>
   <CPU>Quad-core 1.2 GHz</CPU>
   <browser>HTML</browser>
  </feature>
 </mobilephone>
</mobilephones>

Let's start writing XSD.

XSD elements

There are two types of element types.

  • Simple elements

Simple element is an element with no child elements and it does not have attributes. It can be defined in XSD as follows.

<xs:element name="model" type="xs:string"/>

The available data types in XSD are,
  • xs:string
  • xs:integer
  • xs:boolean
  • xs:decimal
  • xs:date
  • xs:time
Simple elements can have default values or fixed values.

<xs:element name="model" type="xs:string" fixed="D6563"/>

<xs:element name="model" type="xs:string" default="D6563"/>

  • Complex elements

XSD complex types can have child elements and attribute values.
In our XML example, <dimensions> tag has its child elements. So it is a complex element.


<xs:element name="dimensions">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="width" type="xs:string"/>
      <xs:element name="height" type="xs:string"/>
      <xs:element name="depth" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

XSD attributes

XSD attributes syntax is similar to simple type.

<xs:attribute name="IMEI" type="integer"/>

Additionally it can have a value for required field. If the attribute is mandatory required field must contain the value 'yes'.

<xs:attribute name="IMEI" type="integer" required="yes"/>

Restrictions for elements

Now, you can write an XML schema with simple and complex elements. Let's see how we can add restrictions for elements.

We can use <xs: restriction > tag to restrict our element. We can have minimum value, maximum value


<xs:element name="weight">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="50"/>
      <xs:maxInclusive value="500"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>


Also you can use the following restriction methods.


<xs:pattern value="[a-zA-Z]"/>

<xs:minLength value="5"/>
<xs:maxLength value="8"/>


Hope you got an idea about writing XML schema for a given small XML document.


See you with more in the next post....

Wednesday, July 9, 2014

XML Basics

XML

Basic XML

Hi,
I would like to represent you about the basics of XML which I came across by Googling and participating the WSO2 Boot Camp.

Let's start with the most important question.

What is XML?

  • XML stands for eXtensible Markup Language.
  • Unlike HTML(Hyper Text Markup Language), XML is for carrying data and not for displaying data.
  • XML is not a database, but it can store data.
A good definition I found from w3schools about XML is, XML is a software- and hardware-independent tool for carrying information.

XML does not have any predefined tags. Users can define their own tags. If you just double click on a XML document, your browser just display the text. 

Sample XML code


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="UTF-8"?>
<mobilephones>
    <mobilephone>
        <model>D6563</model>
        <name>Xperia Z2a</name>
        <brand>Sony</brand>
        <released-year>2014</released-year>
        <dimensions>
            <height>137mm</height>
            <width>72mm</width>
            <depth>11mm</depth>
        </dimensions>
        <weight>163</weight>
    </mobilephone>

    <mobilephone>
        <model>VS876</model>
        <name>Lucid 3</name>
        <brand>LG</brand>
        <released-year>2013</released-year>
        <dimensions>
            <height>131.6mm</height>
            <width>66mm</width>
            <depth>9.9mm</depth>
        </dimensions>
        <weight>123.9</weight>
    </mobilephone>
</mobilephones>


You can notice the first line of this document. Actually the XML document itself does not need it. That line is for the XML parser to identify this document as an XML file. We will discuss about 'parser' in the next post.

A well formatted XML document

  • Every staring must have a matching ending tag.
  • The elements must be properly nested. (The last opened tag must close first.)
  • None of the special characters such as '<' and '&' must not be in the values.
  • A single root element must contain all the elements.
  • Tags are case sensitive, so we need to use correct format for all starting and ending tags.
  • The attribute values must be quoted. 

Comments in XML

The syntax is similar to HTML comments.

<!-- This is a comment -->

Empty elements in XML

<empty_tag></empty_tag> can be replaced by <empty_tag/>

Attributes 

Elements can have attributes. Mostly they are for representing meta data.

Eg: 



<student name="Anne" age="29" mobile="071xxxxxxx">
</student>

Bur it is encouraged to avoid using attributes. Because,

  • it gives a flat structure 
  • cannot have multiple values for a single attribute.
  • hard to expand. 

Assume we need to insert another mobile number for the student 'Anne'. Using attributes it is not possible. Here is the solution for that.



1
2
3
4
5
6
7
8
<student>
 <name>Anne</name>
 <age>29</age>
 <mobiles>
  <mobile>071xxxxxxx</mobile>
  <mobile>078xxxxxxx</mobile>
 </mobiles>
</student>

In this example you can see '<mobiles>' tag has been introduced. In XML those elements must be wrapped with a single sub root element.


Namespaces in XML

XML namespaces are used to provide uniquely identified names and attributes in an XML document.


1
<e:element xmlns:e="http://www.element.org/element">

And also we can use default namespace to avoid using the prefix every time in sub elements.



1
2
<element xmlns="http://www.w3.org/TR/html4/">
</element>



Hope to see you with the next XML session...