XML Standards: XSD, XSLT, and DTD Implementation

Posted by Anonymous and classified in Technology

Written on in English with a size of 7.69 KB

XML and XSD Fundamentals

XML is used for:

  • Data storage and transport
  • Platform-independent data exchange
  • Web services communication (SOAP, API)
  • Configuration files (Android apps, software settings)
  • Structured document representation

XML File Example

This example includes a complex type, attributes, and specific data types:

<university>
  <student id="S101">
    <name>Rajan Pokhrel</name>
    <age>22</age>
    <dob>2003-05-10</dob>
    <address>Kathmandu</address>
  </student>
</university>

XSD (XML Schema Definition)

The following snippet defines the structure for the student element:

<xs:element name="student">
  <xs:complexType>
    <xs:sequence>

XML to HTML Transformation using XSLT

XSLT (Extensible Stylesheet Language Transformations) is used to transform XML documents into other formats such as HTML, text, or XML.

Process of XML to HTML using XSLT

  • XML document is created
  • XSLT stylesheet is defined
  • XSLT processor reads the XML
  • Transformation is applied
  • Output HTML is generated

XML File Example

<students>
  <student>
    <name>Raj</name>
    <age>22</age>
    <course>BSc CSIT</course>
  </student>
  <student>
    <name>Sita</name>
    <age>21</age>
    <course>IT</course>
  </student>
</students>

XSLT File (Transformation Logic)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
  <h2>Student Information</h2>
  <table border="1">
    <tr>
      <th>Name</th>
      <th>Age</th>
      <th>Course</th>
    </tr>
    <xsl:for-each select="students/student">
    <tr>
      <td><xsl:value-of select="name"/></td>
      <td><xsl:value-of select="age"/></td>
      <td><xsl:value-of select="course"/></td>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Output (HTML Result)

Student Information

NameAgeCourse
Raj22BSc CSIT
Sita21IT

Rules for Creating an XML Document

  • XML Declaration is Mandatory: <?xml version="1.0" encoding="UTF-8"?>
  • Only One Root Element: All elements must be inside a single root element.
  • Proper Nesting of Tags: Tags must be properly nested and not overlap.
  • Case Sensitivity: <Name> and <name> are different.
  • Closing Tags: Every opening tag must have a corresponding closing tag.
  • Attributes in Quotes: id="101"
  • Self-Closing Empty Elements: <br/> or <img/>

XML File with Nested Elements

<student id="S101">
  <name>Rajan Pokhrel</name>
  <age>22</age>
  <course>BSc CSIT</course>
  <address>
    <city>Kathmandu</city>
    <country>Nepal</country>
  </address>
</student>

Equivalent XSD (XML Schema Definition)

(Schema definition details to be implemented based on the structure above.)

XML (eXtensible Markup Language) Definition

XML is a markup language used to store, organize, and transport data in a structured and readable format for both humans and machines.

Why XML is called "Extensible"?

  • Users can create their own custom tags.
  • There are no predefined tags like in HTML.
  • It can be extended according to application needs.

Uses of XML

  1. Data Storage: Used to store structured data.
  2. Data Exchange: Used for transferring data between systems.
  3. Web Services: Used in SOAP and API communication.
  4. Configuration Files: Used in software settings (Android apps, servers).
  5. Database Integration: Helps in exchanging database data between platforms.
  6. Web Applications: Used to represent dynamic data in websites.

Advantages of XML

  • Platform independent
  • Easy to read and understand
  • Self-descriptive format
  • Supports data sharing between systems

Disadvantages of XML

  • Verbose (too many tags)
  • Complex for large data
  • Slower compared to JSON
  • Requires parsing

XML vs. HTML Comparison

FeatureXMLHTML
PurposeData storage & transferWeb page display
TagsUser-definedPredefined
Case SensitiveYesNo
FocusData structurePresentation
FlexibilityHighly flexibleLess flexible

XML Script and XSD Restrictions

XML Script:

<Person>
  <Name>Ram</Name>
  <Age>15</Age>
  <Pincode>123</Pincode>
</Person>

XSD (With Restrictions): (Schema restrictions to be defined.)

XML Namespaces

An XML Namespace is a method used to uniquely identify element and attribute names in an XML document to avoid name conflicts when combining documents from different sources.

Why XML Namespace is used?

  • Avoid element name conflicts
  • Combine XML documents from different applications
  • Provide unique identification to elements
  • Prevent ambiguity in large XML systems

How Namespaces Avoid Conflict

  • It uses a prefix (alias) with a URL reference.
  • Each prefix represents a different domain.
  • The same element name can exist with different prefixes.

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:stu="http://school.com" xmlns:emp="http://company.com">
  <stu:name>Rajan</stu:name>
  <emp:name>Shyam Enterprises</emp:name>
</root>

XML File with DTD

XML File:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE student SYSTEM "student.dtd">
<student>
  <name>Rajan</name>
  <age>22</age>
  <address>
    <city>Kathmandu</city>
    <country>Nepal</country>
  </address>
</student>

DTD: (Document Type Definition logic follows.)

Related entries: