XSLT Basics: Templates, Match/Select, and HTML Output

Unit 4â€ĸCLO04

Learning Objectives

Course Learning Outcomes

CLO04

Course Outcomes

CO04
â„šī¸

Introduction

XSLT transforms XML into other formats such as HTML. XSLT is declarative: you describe output rules for parts of the source tree using templates. This topic covers templates, XPath selection, and producing a simple HTML report.

The Basics

XSLT idea

XSLT is a rule-based transformation language: when a node matches a rule, a template generates output.

Technical Details

Minimal XSLT structure

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <html><body>
      <h1>Report</h1>
      <xsl:apply-templates select="library/book"/>
    </body></html>
  </xsl:template>

  <xsl:template match="book">
    <p><xsl:value-of select="title"/></p>
  </xsl:template>

</xsl:stylesheet>

Examples

Input XML

<library>
  <book id="b1"><title>XML</title></book>
</library>

Real-World Use

Practical

  • Create XML input.
  • Write XSLT that generates an HTML table.
  • Add one sort.

📝 For exams

Exam

  • Define XSLT.
  • Explain match vs select.

✨ Key points

Takeaways

  • XSLT uses templates and XPath selection.
  • Start from the root template and delegate with apply-templates.