Last one: Java learning – XML – a rant

I thought of write a post for each hours I learn Java for web development, but that I could not maintain. I hope regular visitors may have found the posting useful. As I cannot post more possibly of my Java learning for Web development so thought to end the series. On the last session it was about XML. In this session, it was about DTD and basic of Schema.

Define Entity:


<?xml version="1.0" encoding="utf-8" ?>
<!-- Entity -->
<!DOCTYPE Employee
[
  <!ENTITY Address1 "RajajiNagar">
  <!ENTITY Address2 "VijayaNagar">
]>
<Employee>
  <Details>
    <Eno>1</Eno>
    <Ename>Sathya</Ename>
    <Address>&Address1;</Address>
  </Details>
  <Details>
    <Eno>2</Eno>
    <Ename>Dhananjaya</Ename>
    <Address>&Address2;</Address>
  </Details>
</Employee>

Define elements and their possible numbers in XML file:


<?xml version="1.0" encoding="utf-8" ?>
<!--   * -> Many
       + -> Atleast One / One or Many
       ? -> 0 or 1
-->
<!DOCTYPE Employee [
  <!ELEMENT Employee (Details*)>
  <!ELEMENT Details (Eno, Ename, Age, Address+)>
  <!ELEMENT Eno (#PCDATA)>
  <!ELEMENT Ename (#PCDATA)>
  <!ELEMENT Age (#PCDATA)>
  <!ELEMENT Address (#PCDATA)>
  <!ENTITY Address1 "RajajaiNagar">
  <!ENTITY Address2 "VijayaNagar">
  <!ENTITY Address3 "Malleshwaram">
]>
<Employee>
  <Details>
    <Eno>1</Eno>
    <Ename>Sathya</Ename>
    <Age>21</Age>
    <Address>&Address1;</Address>
    <Address>&Address2;</Address>
  </Details>
  <Details>
    <Eno>2</Eno>
    <Ename>Dhanu</Ename>
    <Age>22</Age>
    <Address>&Address3;</Address>
  </Details>
</Employee>

Define Attributes and default attributes value:


<?xml version="1.0" encoding="utf-8" ?>
<!-- Default Attributes -->
<!DOCTYPE Student [
  <!ELEMENT Student (Details*)>
  <!ELEMENT Details (Rno, Name, Age)>
  <!ELEMENT Rno (#PCDATA)>
  <!ELEMENT Name (#PCDATA)>
  <!ELEMENT Age (#PCDATA)>
  <!ATTLIST Name Gender  (Male|Female) "Male">
]>
<Student>
  <Details>
    <Rno>1</Rno>
    <Name Gender="Male">Sathya</Name>
    <Age>21</Age>
</Details>
  <Details>
    <Rno>2</Rno>
    <Name Gender="Female">Sadhya</Name>
    <Age>22</Age>
  </Details>
</Student>

Fixed Attributes. Here value Aptech is fixed for element Rno and attributes CollName.


<?xml version="1.0" encoding="utf-8" ?>
<!--Fixed & Default Attributes-->
<!DOCTYPE Student [
  <!ELEMENT Student (Details*)>
  <!ELEMENT Details (Rno, Name, Dept, Age)>
  <!ATTLIST Rno CollName CDATA #FIXED "Aptech">
  <!ATTLIST Name Gender (Male|Female) "Male">
  <!ATTLIST Dept department (Computer|Electronics|Automobile) "Computer">
  <!ELEMENT Rno (#PCDATA)>
  <!ELEMENT Name (#PCDATA)>
  <!ELEMENT Dept (#PCDATA)>
  <!ELEMENT Age (#PCDATA)>
]>
<Student>
  <Details>
    <Rno CollName="Aptech"></Rno>
    <Name Gender="Male">Sathya</Name>
    <Dept department="Computer"/>
    <Age>21</Age>
  </Details>
  <Details>
    <Rno>2</Rno>
    <Name>Deepa</Name>
    <Dept department="Electronics"/>
    <Age>22</Age>
  </Details>
</Student>

External DTD: Just using the defined attributes in different file.

XML file using external DTD:


<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE Employee SYSTEM "XMLFile8.dtd">
<Employee>
  <Details>
    <Eno>1</Eno>
    <Ename>Sathya</Ename>
  </Details>
</Employee>

External DTD (XMLFile8.dtd) used by XML:


<?xml version="1.0" encoding="utf-8" ?>
<!-- External DTD File Normal File-->
<!ELEMENT Employee (Details)>
<!ELEMENT Details (Eno, Ename, Age)>
<!ELEMENT Eno (#PCDATA)>
<!ELEMENT Ename (#PCDATA)>
<!ELEMENT Age (#PCDATA)>

Little about Schema also:


<?xml version="1.0" encoding="utf-8" ?>
<!-- Normal XML File with Schema Connection-->
<Employee xmlns="http://tempuri.org/XMLSchema.xsd">
  <Eno>1</Eno>
  <Ename>Sathya</Ename>
  <Salary>1234.56</Salary>
</Employee>

Above file Schema file (XMLSchema.xsd):


<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema" targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Employee">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Eno" />
        <xs:element ref="Ename" />
        <xs:element ref="Salary" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Eno" type="xs:int">
  </xs:element>
  <xs:element name="Ename" type="xs:string">
  </xs:element>
  <xs:element name="Salary" type="xs:float">
  </xs:element>
</xs:schema>


<?xml version="1.0" encoding="utf-8" ?>
<Employee xmlns="http://tempuri.org/XMLSchema2.xsd">
  <Details>
    <Eno>1</Eno>
    <Ename a:Gender="Male" xmlns:a="http://tempuri.org/XMLSchema2.xsd">Sathya</Ename>
    <Dept a:Department="Programmer" xmlns:a="http://tempuri.org/XMLSchema2.xsd"/>
  </Details>
</Employee>

Schema file (XMLSchema2.xsd) for above XML:


<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema2" targetNamespace="http://tempuri.org/XMLSchema2.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema2.xsd" xmlns:mstns="http://tempuri.org/XMLSchema2.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Employee">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Details" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Details">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="Eno" />
        <xs:element ref="Ename" />
        <xs:element ref="Dept" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="Eno" type="xs:int">
  </xs:element>
  <xs:attribute name="Gender">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:enumeration value="Male" />
        <xs:enumeration value="Female" />
      </xs:restriction>
    </xs:simpleType>
  </xs:attribute>
  <xs:element name="Ename">
    <xs:complexType>
      <xs:sequence />
      <xs:attribute ref="Gender" />
    </xs:complexType>
  </xs:element>
  <xs:attribute name="Department">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:enumeration value="Programmer" />
        <xs:enumeration value="Assistant" />
        <xs:enumeration value="Accountant" />
        <xs:enumeration value="Sales" />
      </xs:restriction>
    </xs:simpleType>
  </xs:attribute>
  <xs:element name="Dept">
    <xs:complexType>
      <xs:sequence />
      <xs:attribute ref="Department" />
    </xs:complexType>
  </xs:element>
</xs:schema>
Comments are open for an year period. Please, write here on Facebook page.