Anna University Regional Campus, Coimbatore

Department of Computer Science and Engineering

Experiment 7 - XML Schema for Student Details



AIM:

To write a program for implementing student information using XML & XSL.

ALGORITHM:

  1. Create an XML document and reference the XSL document.
  2. Create the student information inside the <student> tag and insert the student details.
  3. Close all opened tags in the XML file.
  4. In the XSL document, create a HTML structure to display the student information in a table format.
  5. Close the necessary tags in the XSL document.

PROGRAM:

student.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="students.xsd">
    <student>
        <id>S101</id>
        <name>Nithish</name>
        <age>19</age>
        <email>nithish@example.com</email>
        <course>Computer Science</course>
        <grade>A</grade>
    </student>
    <student>
        <id>S102</id>
        <name>Puvanesh</name>
        <age>20</age>
        <email>puvanesh@example.com</email>
        <course>Mechanical Engineering</course>
        <grade>B+</grade>
    </student>
    <student>
        <id>S103</id>
        <name>Chandru</name>
        <age>21</age>
        <email>chandru@example.com</email>
        <course>Electrical Engineering</course>
        <grade>A-</grade>
    </student>
    <student>
        <id>S104</id>
        <name>Madhan</name>
        <age>20</age>
        <email>madhan@example.com</email>
        <course>Civil Engineering</course>
        <grade>B</grade>
    </student>
</students>
            

students.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="students">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="student" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="id" type="xs:string"/>
                            <xs:element name="name" type="xs:string"/>
                            <xs:element name="age" type="xs:int"/>
                            <xs:element name="email" type="xs:string"/>
                            <xs:element name="course" type="xs:string"/>
                            <xs:element name="grade" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
            

students.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <html>
            <head>
                <title>Students List</title>
                <style>
                    table {
                        width: 80%;
                        border-collapse: collapse;
                        margin: 20px auto;
                        font-family: Arial, sans-serif;
                    }
                    th, td {
                        padding: 10px;
                        text-align: left;
                        border: 1px solid #ddd;
                    }
                    th {
                        background-color: #4CAF50;
                        color: white;
                    }
                    tr:nth-child(even) {
                        background-color: #f2f2f2;
                    }
                </style>
            </head>
            <body>
                <h2 style="text-align: center;">Student Information</h2>
                <table>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Age</th>
                        <th>Email</th>
                        <th>Course</th>
                        <th>Grade</th>
                    </tr>
                    <xsl:for-each select="students/student">
                        <tr>
                            <td><xsl:value-of select="id"/></td>
                            <td><xsl:value-of select="name"/></td>
                            <td><xsl:value-of select="age"/></td>
                            <td><xsl:value-of select="email"/></td>
                            <td><xsl:value-of select="course"/></td>
                            <td><xsl:value-of select="grade"/></td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>

</xsl:stylesheet>
            

SAMPLE OUTPUT:

Output 1
Output 2

RESULT:

Thus, the program for implementing student information using XML & XSL has been verified successfully.