1. What is XML Schema (XSD)?
XML Schema defines the structure, content, and data types of an XML document. It is more
powerful than DTD because:
Specifies data types (string, integer, date, etc.).
Allows complex structures (nested elements, sequences, choices).
Supports namespaces.
Helps in validation of XML documents.
Short notes :-
🧩 1. XML Schema (XSD – XML Schema Definition)
🔹 Definition
XML Schema defines the structure, content, and data types of XML documents.
It is written in XML syntax and provides a more powerful way to describe XML data than DTD.
🔹 Purpose
To define the structure of XML documents.
To validate XML data against defined rules.
To specify data types for elements and attributes.
To support namespaces, inheritance, and reusability.
🔹 Advantages of XML Schema
Written in XML format (both human- and machine-readable).
Supports data types (string, integer, date, etc.).
Allows reusability and modular design.
Supports namespaces for avoiding naming conflicts.
Provides stronger validation than DTD.
🔹 Main Components of XML Schema
1. Elements – Define tags used in XML documents.
2. Attributes – Define properties of elements.
3. Data Types – Define the type of data allowed in elements and attributes.
4. Simple Types – Contain only text values (e.g., strings, numbers).
5. Complex Types – Contain elements, attributes, or both.
6. Facets and Constraints – Used to restrict values (e.g., minLength, maxLength, pattern).
🔹 Advantages over DTD
Feature DTD XML Schema
Syntax Not XML-based XML-based
Data Types Not supported Strongly typed
Namespaces Not supported Supported
Extensibility Limited High
Validation Structural only Structural + Data type
🔹 Limitations of XML Schema
More complex to write and understand.
Larger in size compared to DTD.
🔹 Applications
Used in web services (SOAP, WSDL).
Data exchange and validation in enterprise applications.
Configuration and metadata representation.
📜 2. XML DTD (Document Type Definition)
🔹 Definition
DTD defines the structure and the legal building blocks of an XML document.
It specifies which elements, attributes, and entities are allowed and how they are related.
🔹 Purpose
To ensure XML documents follow a predefined structure.
To verify the order, nesting, and occurrence of elements.
To provide a grammar or rule set for XML documents.
🔹 Features of DTD
Defines elements, attributes, and entities.
Specifies relationships and hierarchy among elements.
Can be internal (inside XML file) or external (in a separate file).
Used for structural validation.
🔹 Types of DTD
1. Internal DTD: Defined within the XML document.
2. External DTD: Defined in a separate file and linked to XML.
🔹 Advantages of DTD
Easy to create and understand for small XML documents.
Provides structure validation.
Useful for simple XML-based applications.
🔹 Limitations of DTD
Does not support data types (everything is text).
No namespace support.
Different syntax (not XML-based).
Limited reusability and extensibility.
🔹 Applications
Used for defining structure in simple XML documents.
Common in early XML-based systems and legacy applications.
⚙️3. XML Parser
🔹 Definition
An XML Parser is a software component that reads XML documents, checks for well-
formedness, and optionally validates them against DTD or Schema.
🔹 Purpose
To read and process XML documents.
To verify the syntax of XML.
To validate XML against its DTD or Schema.
To provide access to XML data for applications.
🔹 Types of Parsers
1. DOM Parser (Document Object Model)
o Loads the entire XML document into memory as a tree structure.
o Allows random access and manipulation of elements.
o Suitable for small XML files.
2. SAX Parser (Simple API for XML)
o Reads XML sequentially (event-based).
o Does not load the full document into memory.
o Efficient for large XML files.
3. Pull Parser
o Parsing is controlled by the application (pulls data when needed).
o Combines advantages of DOM and SAX.
o Commonly used in Android applications.
🔹 Parser Validation Categories
1. Well-Formed Parser:
o Checks if the XML document follows correct syntax (properly nested tags, case
sensitivity, closing tags).
2. Validating Parser:
o Ensures XML follows the structure defined by its DTD or Schema.
🔹 Advantages of XML Parsers
Ensure XML data integrity and correctness.
Facilitate data sharing across applications.
Help detect errors during XML document creation.
🔹 Applications
Web services and APIs.
Data exchange between systems.
Configuration files in software applications.
XML-based databases.
🧾 Summary Comparison
Feature DTD XML Schema XML Parser
Definition Defines structure of Defines structure Reads and validates XML
XML document and data types documents
Syntax Non-XML XML-based Programming interface
Validation Structure only Structure + Data Performs well-formedness
types and validation
Data Type No Yes Uses DTD/Schema for
Support validation
Namespace No Yes Dependent on validation
Support type
Reusability Limited High Not applicable
🧠 Key Takeaways
DTD defines structure but lacks data type support.
XML Schema defines structure and data types, offering stronger validation.
XML Parser reads and validates XML documents for well-formedness and correctness.
2. Sample XML Document
Suppose we have a simple XML file for students:
<students>
<student>
<id>101</id>
<name>Fowmila</name>
<age>25</age>
<email>[email protected]</email>
</student>
<student>
<id>102</id>
<name>Anu</name>
<age>24</age>
<email>[email protected]</email>
</student>
</students>
<students> → Root element
<student> → Repeating element
<id>, <name>, <age>, <email> → Child elements of <student>
3. XML Schema (XSD) for the above XML
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Define student element as a complex type -->
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<!-- student element can appear multiple times -->
<xs:element name="student" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="email" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
4. Explanation of Each Line
1. <?xml version="1.0" encoding="UTF-8"?>
o Standard XML declaration.
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
o Declares this is an XSD schema using the xs namespace.
3. <xs:element name="students">
o Defines a root element called students.
4. <xs:complexType>
o Indicates that <students> has nested elements (not just text).
5. <xs:sequence>
o Specifies that child elements must appear in the order defined.
6. <xs:element name="student" maxOccurs="unbounded">
o Defines a student element that can appear any number of times.
7. <xs:complexType> inside student
o student has multiple nested elements: id, name, age, email.
8. <xs:element name="id" type="xs:integer"/>
o id must be an integer.
9. <xs:element name="name" type="xs:string"/>
o name must be a string.
10. <xs:element name="age" type="xs:integer"/>
o age must be an integer.
11. <xs:element name="email" type="xs:string"/>
o email must be a string.
12. Closing tags </xs:sequence>, </xs:complexType>, </xs:element>
o Close the open tags in the correct hierarchy.
5. How Validation Works
The XML document is checked against the XSD.
If an element is missing, has the wrong type, or appears out of order, the validator will
show an error.
1. What are Attributes in XML?
Attributes provide additional information about elements in XML.
Example:
<student id="101" grade="A">
<name>reshma</name>
</student>
Here:
id and grade are attributes of <student>.
<name> is a child element.
2. Rules for Declaring Attributes in XSD
1. Attribute Names Must Be Unique inside an element.
2. Attribute Types must be specified (xs:string, xs:integer, etc.).
3. Use use Attribute to define if it is required or optional:
o use="required" → attribute must be present.
o use="optional" → attribute may or may not appear.
4. Attributes cannot contain child elements; they are always simple data.
3. Syntax to Declare Attributes in XSD
Inside an element:
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:integer" use="required"/>
<xs:attribute name="grade" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
4. Full XSD Example with Explanation
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Root element -->
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<!-- student element can repeat -->
<xs:element name="student" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
</xs:sequence>
<!-- Attributes -->
<xs:attribute name="id" type="xs:integer" use="required"/>
<xs:attribute name="grade" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
5. Explanation of Each Line (Attributes Part)
1. <xs:attribute name="id" type="xs:integer" use="required"/>
o Declares an attribute id of type integer.
o use="required" → must appear in <student> element.
2. <xs:attribute name="grade" type="xs:string" use="optional"/>
o Declares an attribute grade of type string.
o use="optional" → may or may not appear in <student>.
1. Element vs Attribute
Feature Element Attribute
Data location Inside the element Inside the start tag
Can have child Yes No
Can repeat Yes No
Syntax example <name>Fowmila</name> <student id="101"/>
2. Hierarchy Diagram for Example
students (root element)
│
└── student (complex element, can repeat)
├── name (child element)
├── age (child element)
├── id (attribute, required)
└── grade(attribute, optional)
Root element: students
Repeating element: student
Child elements: name, age
Attributes: id (required), grade (optional)
3. Key Rules to Remember
1. Attributes cannot have child elements.
2. Attributes are always simple types (string, integer, etc.).
3. Use use="required" for mandatory attributes, use="optional" for optional.
4. Elements can repeat using maxOccurs. Attributes cannot repeat.
1. What is an XML Namespace?
An XML Namespace is a way to avoid element or attribute name conflicts when
combining XML documents from different sources.
It provides a unique identifier (URI) for elements and attributes.
Think of it like a label that tells which “domain” an element belongs to.
2. Why Namespaces Are Needed
Imagine two XML files:
File 1:
<book>
<title>XML Basics</title>
</book>
File 2:
<book>
<title>Cooking 101</title>
</book>
If we combine these, the <book> element could conflict.
Using namespaces, we can differentiate them.
3. Syntax of XML Namespaces
1. Default Namespace – applies to elements without prefix:
<book xmlns="http://example.com/books">
<title>XML Basics</title>
</book>
2. Prefixed Namespace – use a prefix for elements:
<bk:book xmlns:bk="http://example.com/books">
<bk:title>XML Basics</bk:title>
</bk:book>
xmlns:bk="URI" → declares a namespace with prefix bk
All elements with bk: belong to that namespace.
4. Rules of XML Namespaces
1. URIs are unique identifiers, not necessarily web addresses.
2. Prefixes are just labels; the URI is what matters.
3. You can declare multiple namespaces in a single XML.
4. Attributes can also belong to a namespace, but by default they don’t inherit the
element’s namespace.
5. Example with Multiple Namespaces
<library xmlns:bk="http://example.com/books"
xmlns:au="http://example.com/authors">
<bk:book>
<bk:title>XML Basics</bk:title>
<au:author>
<au:name>Fowmila</au:name>
</au:author>
</bk:book>
</library>
Explanation:
bk → namespace for book-related elements.
au → namespace for author-related elements.
Both can exist without conflicts.
Sure, Fowmila! Let’s go step by step to understand reusing schema components in XML
Schema (XSD) with examples and explanations.
1. What is Reusing Schema Components?
In XSD, you can define elements, types, or groups once and reuse them multiple times.
This avoids repetition and makes XML schemas modular and maintainable.
Components that can be reused:
1. Global elements
2. Global attributes
3. Complex types
4. Simple types
5. Groups (element or attribute groups)
Reusing Schema Components in XML Schema (XSD)
In XML Schema, reusable components allow us to define elements, attributes, or types once
and use them multiple times across the schema. This avoids redundancy, ensures consistency,
and makes the schema easier to maintain.
1. Global Elements
Definition: Elements defined at the top level of the schema.
Purpose: Can be referenced anywhere in the schema using ref.
Example:
<xs:element name="name" type="xs:string"/>
2. Global Attributes
Definition: Attributes defined at the top level of the schema.
Purpose: Can be referenced in multiple elements.
Example:
<xs:attribute name="id" type="xs:integer" use="required"/>
3. Complex Types
Definition: Types that define a structure of elements and/or attributes.
Purpose: Can be applied to multiple elements.
Example:
<xs:complexType name="PersonType">
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="student" type="PersonType"/>
<xs:element name="teacher" type="PersonType"/>
4. Simple Types
Definition: Types that restrict or define data types (string, integer, date) with rules.
Purpose: Can be reused in multiple elements or attributes.
Example:
<xs:simpleType name="AgeType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="18"/>
<xs:maxInclusive value="60"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="age" type="AgeType"/>
5. Groups (Element or Attribute Groups)
a) Element Group
Definition: A group of elements defined together.
Purpose: Can be reused in multiple complex types.
Example:
<xs:group name="personElements">
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
</xs:sequence>
</xs:group>
<xs:element name="teacher">
<xs:complexType>
<xs:sequence>
<xs:group ref="personElements"/>
</xs:sequence>
</xs:complexType>
</xs:element>
b) Attribute Group
Definition: A group of attributes defined together.
Purpose: Can be reused in multiple elements.
Example:
<xs:attributeGroup name="idAttrGroup">
<xs:attribute name="id" type="xs:integer" use="required"/>
<xs:attribute name="grade" type="xs:string" use="optional"/>
</xs:attributeGroup>
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
<xs:attributeGroup ref="idAttrGroup"/>
</xs:complexType>
</xs:element>
Key Points to Remember
1. Reusable components must be declared globally (at top-level).
2. They can be referenced multiple times across elements.
3. Helps maintain consistency and modularity.
4. Commonly reused components: elements, attributes, complex types, simple types,
groups.
Grouping Elements and Attributes in XML Schema (XSD)
In XML Schema, grouping allows us to combine multiple elements or attributes together so
that they can be reused in multiple places. This makes schemas modular, consistent, and easier
to maintain.
1. Element Group
Definition
A group of elements declared together in a sequence, choice, or all.
Can be reused in multiple complex types using the ref attribute.
Syntax
<xs:group name="groupName">
<xs:sequence>
<!-- List of elements -->
</xs:sequence>
</xs:group>
Example
<xs:group name="personElements">
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
</xs:sequence>
</xs:group>
<!-- Using the group in an element -->
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:group ref="personElements"/>
<xs:element name="grade" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Explanation:
personElements contains firstName, lastName, and age.
The student element reuses these elements using ref="personElements".
This avoids repeating the same element definitions in multiple places.
2. Attribute Group
Definition
A group of attributes declared together.
Can be reused in multiple elements using the ref attribute.
Syntax
<xs:attributeGroup name="attributeGroupName">
<xs:attribute name="attribute1" type="xs:type" use="required|optional"/>
<xs:attribute name="attribute2" type="xs:type" use="required|optional"/>
</xs:attributeGroup>
Example
<xs:attributeGroup name="idAttrGroup">
<xs:attribute name="id" type="xs:integer" use="required"/>
<xs:attribute name="grade" type="xs:string" use="optional"/>
</xs:attributeGroup>
<!-- Using the attribute group in an element -->
<xs:element name="teacher">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="subject" type="xs:string"/>
</xs:sequence>
<xs:attributeGroup ref="idAttrGroup"/>
</xs:complexType>
</xs:element>
Explanation:
idAttrGroup contains id (required) and grade (optional) attributes.
The teacher element reuses these attributes using ref="idAttrGroup".
3. Key Points
1. Element groups are used for reusable sets of child elements.
2. Attribute groups are used for reusable sets of attributes.
3. Both must be declared globally in the schema.
4. Reusing groups reduces redundancy and improves schema maintainability.
5. Use <xs:group ref="groupName"/> for elements, <xs:attributeGroup ref="groupName"/>
for attributes.
4. Diagram (Conceptual)
student (element)
│
├─ firstName (element)
├─ lastName (element)
├─ age (element)
└─ grade (element)
│
Attributes (from idAttrGroup)
├─ id (required)
└─ grade (optional)
Shows how element groups define child elements and attribute groups define attributes
for an element.
XML Stylesheets Introduction
1. What is an XML Stylesheet?
An XML Stylesheet defines how an XML document should be displayed or
transformed.
It is similar to CSS for HTML but more powerful, as it can transform XML data into
different formats (HTML, text, or another XML).
Common types:
1. XSL (eXtensible Stylesheet Language)
2. XSLT (XSL Transformations) – a language for transforming XML data.
2. Purpose of XML Stylesheets
1. Presentation: Style XML data for display in browsers.
2. Transformation: Convert XML data into HTML, plain text, or another XML format.
3. Separation of content and style: XML stores data, stylesheet defines presentation.
3. Linking XML to Stylesheet
Syntax
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
type="text/xsl" → specifies the stylesheet type.
href="style.xsl" → the XSL file to use.
Example XML linked to stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<students>
<student>
<name>reshma</name>
<age>25</age>
</student>
<student>
<name>Anu</name>
<age>24</age>
</student>
</students>
4. Basic XSLT Example
students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Template to match root element -->
<xsl:template match="/students">
<html>
<body>
<h2>Student List</h2>
<table border="1">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<!-- Loop through student elements -->
<xsl:for-each select="student">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="age"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Explanation:
<xsl:stylesheet> → Root of XSLT document.
match="/students" → Template matches the root <students> element.
<xsl:for-each select="student"> → Iterates over each <student> element.
<xsl:value-of select="name"/> → Displays the value of <name>.
5. Key Points
1. XML stores data, XSL/XSLT defines how data is displayed or transformed.
2. A single XML document can be linked to multiple stylesheets.
3. XSLT allows conditional formatting, loops, sorting, and transforming XML into
other formats.
4. Browsers like Chrome, Firefox, and Edge can render XML with XSLT stylesheets.
XML Stylesheet (XSLT) Transformation Diagram
+----------------+ +-------------------+ +----------------+
| | | | | |
| XML Data | -----> |XSLT Stylesheet | -----> | Output |
| (students) | | (students.xsl) | | (HTML Page) |
| | | | | |
+----------------+ +-------------------+ +-----------------------+
Example:
XML Data:
<students>
<student>
<name>Fowmila</name>
<age>25</age>
</student>
</students>
XSLT Template:
<xsl:template match="/students">
<html>
<body>
<h2>Student List</h2>
<table border="1">
<tr><th>Name</th><th>Age</th></tr>
<xsl:for-each select="student">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="age"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
Output (HTML):
<html>
<body>
<h2>Student List</h2>
<table border="1">
<tr><th>Name</th><th>Age</th></tr>
<tr><td>Fowmila</td><td>25</td></tr>
</table>
</body>
</html>
Explanation of Flow
1. XML Data – Contains raw information (students’ names, age, etc.).
2. XSLT Stylesheet – Defines how to display or transform the XML data.
3. Output – Browser or processor applies the XSLT to XML and produces formatted
HTML (or text/another XML).
CSS and Extensible Stylesheet Language for XML
1. CSS (Cascading Style Sheets)
Definition: CSS is a style sheet language used to describe the presentation of HTML
or XML documents.
Purpose: Controls how elements look (color, font, layout) without changing the data
itself.
Limitation: CSS can style XML visually but cannot transform or manipulate XML
structure.
Example XML with CSS
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="style.css"?>
<students>
<student>
<name>reshma</name>
<age>25</age>
</student>
</students>
style.css
student { color: blue; }
name { font-weight: bold; }
age { color: green; }
<?xml-stylesheet?> links the XML file to CSS.
Browser applies visual styles to elements like student, name, age.
2. XSL (Extensible Stylesheet Language)
Definition: XSL is a powerful stylesheet language for transforming XML documents.
Purpose: Not only styles XML but also transforms it into HTML, text, or other XML
formats.
Components of XSL:
1. XSLT (Transformations): Defines rules to convert XML into another
format.
2. XPath: Used to navigate XML elements.
3. XSL-FO (Formatting Objects): Used for print-ready output like PDF.
Here are short, clear notes on the components of XSL — perfect for
teaching or quick revision 👇
🧩 Components of XSL (Extensible Stylesheet Language)
1. XSLT (Extensible Stylesheet Language Transformations)
Used to transform XML documents into other formats like HTML, text, or another
XML.
Defines rules and templates for converting XML data into a desired structure.
Helps separate data (XML) from presentation (HTML).
Key point: Converts XML into readable or displayable forms.
2. XPath (XML Path Language)
Used to navigate and select specific parts (elements, attributes, nodes) of an XML
document.
Works like a path expression to locate data within the XML tree.
Commonly used inside XSLT to find and process data.
Key point: Helps locate elements and attributes in XML.
3. XSL-FO (XSL Formatting Objects)
Used to format XML data for high-quality print or PDF output.
Controls page layout, fonts, colors, and formatting details.
Often used in publishing or report generation systems.
Key point: Provides print-ready formatting for XML data.
🧠 Summary Table
Component Full Form Main Function
XSLT XSL Transformations Converts XML into HTML/text/XML
XPath XML Path Language Selects and navigates XML data
XSL-FO XSL Formatting Objects Formats XML for print or PDF output
Example XML with XSLT
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<students>
<student>
<name>reshma</name>
<age>25</age>
</student>
</students>
students.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/students">
<html>
<body>
<h2>Student List</h2>
<xsl:for-each select="student">
<p>Name: <xsl:value-of select="name"/></p>
<p>Age: <xsl:value-of select="age"/></p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
XSLT transforms XML into an HTML page.
Can handle loops, conditions, sorting, and complex transformations.
3. Key Differences Between CSS and XSL
Feature CSS XSL/XSLT
Purpose Styling XML/HTML Transforming and styling XML
Can transform ❌ No ✅ Yes
XML?
Complexity Simple Advanced
Output Visual styling only HTML, XML, Text, PDF
Syntax CSS rules (selector XSLT templates with XPath
{ property:value; }) expressions
4. Summary
1. CSS → Best for simple styling, visual formatting of XML.
2. XSL/XSLT → Best for transforming XML into other formats and applying advanced
styling.
3. XML + CSS → Looks nice in browsers but cannot change data.
4. XML + XSLT → Can change structure, style, and produce multiple outputs.
Formatting XML Data Based on Controls
Definition:
Formatting controls are instructions in XSL/XSLT that allow you to display XML data
in a particular format depending on conditions, loops, or rules.
Controls help in conditional display, sorting, or choosing data.
1. Common Formatting Controls in XSLT
a) <xsl:for-each> – Looping
Repeats formatting for a set of elements.
Example:
<xsl:for-each select="student">
<p><xsl:value-of select="name"/></p>
</xsl:for-each>
Loops through all <student> elements and displays their <name>.
b) <xsl:if> – Conditional Formatting
Applies formatting if a condition is true.
Syntax:
<xsl:if test="age > 20">
<p><xsl:value-of select="name"/> is older than 20</p>
</xsl:if>
Displays only students whose age > 20.
c) <xsl:choose> / <xsl:when> / <xsl:otherwise> – Multiple Conditions
Similar to if-else-if in programming.
Example:
<xsl:choose>
<xsl:when test="grade='A'">
<p><xsl:value-of select="name"/> got grade A</p>
</xsl:when>
<xsl:otherwise>
<p><xsl:value-of select="name"/> did not get grade A</p>
</xsl:otherwise>
</xsl:choose>
Formats output based on the value of <grade>.
d) <xsl:sort> – Sorting Data
Sort elements while displaying.
Example:
<xsl:for-each select="student">
<xsl:sort select="age" data-type="number" order="ascending"/>
<p><xsl:value-of select="name"/> - <xsl:value-of select="age"/></p>
</xsl:for-each>
Displays students sorted by age.
e) <xsl:value-of> – Extracting Data
Retrieves text value of an element.
Example: <xsl:value-of select="name"/>
2. Full Example – Using Multiple Controls
XML:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<students>
<student>
<name>Fowmila</name>
<age>25</age>
<grade>A</grade>
</student>
<student>
<name>Anu</name>
<age>19</age>
<grade>B</grade>
</student>
</students>
students.xsl:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/students">
<html>
<body>
<h2>Student List</h2>
<xsl:for-each select="student">
<xsl:sort select="age" data-type="number" order="ascending"/>
<xsl:if test="age > 20">
<p>
<xsl:value-of select="name"/> - <xsl:value-of select="age"/>
<xsl:choose>
<xsl:when test="grade='A'">
(Excellent)
</xsl:when>
<xsl:otherwise>
(Good)
</xsl:otherwise>
</xsl:choose>
</p>
</xsl:if>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Explanation:
1. <xsl:for-each> → Loops through all students.
2. <xsl:sort> → Sorts students by age ascending.
3. <xsl:if> → Only displays students older than 20.
4. <xsl:choose> → Adds text based on the grade.
3. Key Points
1. Controls in XSLT allow dynamic formatting based on data values.
2. Common controls:
o Looping: <xsl:for-each>
o Conditional display: <xsl:if>, <xsl:choose>
o Sorting: <xsl:sort>
o Extracting values: <xsl:value-of>
3. Combining controls helps produce customized output for XML data.
Displaying XML Data in a Table Using XSLT
1. Purpose
To present XML data neatly in a table format (HTML table).
Useful for web display and report generation.
Combines loops, conditional formatting, and table structure.
2. XML Example
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
<students>
<student>
<name>Fowmila</name>
<age>25</age>
<grade>A</grade>
</student>
<student>
<name>Anu</name>
<age>19</age>
<grade>B</grade>
</student>
</students>
3. XSLT Example for Table
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Template matching root element -->
<xsl:template match="/students">
<html>
<body>
<h2>Student Table</h2>
<table border="1" cellpadding="5" cellspacing="0">
<tr bgcolor="#CCCCCC">
<th>Name</th>
<th>Age</th>
<th>Grade</th>
</tr>
<!-- Loop through each student -->
<xsl:for-each select="student">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="age"/></td>
<td><xsl:value-of select="grade"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
4. Explanation
1. <xsl:template match="/students"> → Matches the root element <students>.
2. <table> → HTML table is created.
3. <tr> → Table row; <th> → Table header.
4. <xsl:for-each select="student"> → Loops through each <student> element.
5. <td><xsl:value-of select="name"/></td> → Displays the value of <name> inside a table
cell.
6. Result: A neat table showing all student data.
5. Sample Output (HTML Table)
Name Age Grad
e
Fowmila 25 A
Anu 19 B
6. Optional Enhancements
Sort data: <xsl:sort select="age" data-type="number"/>
Conditional row colors: <xsl:if> inside <tr> to apply bgcolor
Alternate row shading for better readability.