Learning XPath with XSLT using ASP.NET 2.0 - Setting up your environment: XML Schema and XML
(Page 2 of 6 )
In this article, we need to work with nested hierarchies in XML. None of my XML samples in previous articles will really help us. So I started defining my own XML schema and sample XML document for this article.
In this new design, I included the concept of departments and employees. Every department can contain any number of employees and an employee always belongs to a table. The following is the XML schema to achieve the same:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="EmployeeDetails" targetNamespace=
"http://tempuri.org/EmployeeDetails.xsd"
elementFormDefault="qualified" xmlns=
"http://tempuri.org/EmployeeDetails.xsd" xmlns:mstns=
"http://tempuri.org/EmployeeDetails.xsd" xmlns:xs=
"http://www.w3.org/2001/XMLSchema">
<xs:element name="EmployeeDetails">
<xs:complexType>
<xs:sequence>
<xs:element name="Department" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Employee" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Empno" type="xs:int" />
<xs:element name="Ename" type="xs:string" />
<xs:element name="Sal" type="xs:float" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Deptno" type="xs:int" />
<xs:attribute name="Dname" type="xs:string" />
<xs:attribute name="Location" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I designed the above schema using Visual Studio 2005. From the above you can understand that the root of the XML document must start with “EmployeeDetails” and can contain any number of “Departments.” Details (deptno, dname and location) of each and every “Department” will be available as attributes and every “Department” can have any number of “Employees” with details (empno, ename and sal) underneath them.
The following is the sample XML document which complements the above schema.
<?xml version="1.0" encoding="utf-8"?>
<EmployeeDetails>
<Department Deptno="10" Dname="Accounting" Location="New York">
<Employee>
<Empno>1001</Empno>
<Ename>Jagadish</Ename>
<Sal>3400</Sal>
</Employee>
<Employee>
<Empno>1002</Empno>
<Ename>Chatarji</Ename>
<Sal>2500</Sal>
</Employee>
<Employee>
<Empno>1003</Empno>
<Ename>Winner</Ename>
<Sal>4300</Sal>
</Employee>
</Department>
<Department Deptno="20" Dname="Sales" Location="Dallas">
<Employee>
<Empno>2001</Empno>
<Ename>Dhanam</Ename>
<Sal>4500</Sal>
</Employee>
<Employee>
<Empno>2002</Empno>
<Ename>Chinna</Ename>
<Sal>3400</Sal>
</Employee>
<Employee>
<Empno>2003</Empno>
<Ename>Pedda</Ename>
<Sal>3200</Sal>
</Employee>
</Department>
<Department Deptno="30" Dname="Research" Location="Washington">
<Employee>
<Empno>3001</Empno>
<Ename>Ram</Ename>
<Sal>5600</Sal>
</Employee>
<Employee>
<Empno>3002</Empno>
<Ename>Robert</Ename>
<Sal>5600</Sal>
</Employee>
<Employee>
<Empno>3003</Empno>
<Ename>Rahim</Ename>
<Sal>4300</Sal>
</Employee>
</Department>
</EmployeeDetails>
Next: Setting up your environment: ASP.NET 2.0 code and XSL >>
More ASP.NET Articles
More By Jagadish Chaterjee