5️⃣ 🎨 XSLT Tutorial
Estimated reading: 5 minutes 29 views

🧪 XSLT Examples – Real-World XML Transformation Scenarios

🧲 Introduction – Why Learn XSLT with Examples?

Reading about XSLT is great—but seeing it in action is where the magic happens. These XSLT examples walk you through practical transformations like generating HTML, converting XML schemas, filtering elements, and modifying data dynamically. They’re ideal for developers, integrators, and content managers working with XML workflows.

🎯 In this guide, you’ll learn:

  • How to use XSLT to transform XML into HTML
  • How to rename, filter, and restructure XML nodes
  • How to use loops, conditions, and sorting
  • End-to-end examples for real-life applications

📄 Sample XML for Examples

<catalog>
  <book id="101">
    <title>Learn XSLT</title>
    <author>Jane Doe</author>
    <price>499</price>
  </book>
  <book id="102">
    <title>Advanced XML</title>
    <author>John Smith</author>
    <price>699</price>
  </book>
</catalog>

🌐 Example 1 – XML to HTML Conversion

🔹 XSLT

<xsl:template match="/catalog">
  <html>
    <body>
      <h2>Book Catalog</h2>
      <xsl:for-each select="book">
        <div>
          <h3><xsl:value-of select="title"/></h3>
          <p>Author: <xsl:value-of select="author"/></p>
          <p>Price: ₹<xsl:value-of select="price"/></p>
        </div>
      </xsl:for-each>
    </body>
  </html>
</xsl:template>

✅ Converts the XML into a styled HTML page.


✏️ Example 2 – Rename and Reformat XML Tags

🔹 Output: <item> instead of <book>, with new structure

<xsl:template match="book">
  <item>
    <name><xsl:value-of select="title"/></name>
    <writer><xsl:value-of select="author"/></writer>
    <cost><xsl:value-of select="price"/></cost>
  </item>
</xsl:template>

✅ Useful for transforming to a new XML schema.


🔃 Example 3 – Sort Books by Price

<xsl:for-each select="catalog/book">
  <xsl:sort select="price" data-type="number" order="ascending"/>
  <p><xsl:value-of select="title"/> – ₹<xsl:value-of select="price"/></p>
</xsl:for-each>

✅ Sorts books from cheapest to most expensive.


✅ Example 4 – Filter Books with Price > 500

<xsl:for-each select="catalog/book[price &gt; 500]">
  <p><xsl:value-of select="title"/> – ₹<xsl:value-of select="price"/></p>
</xsl:for-each>

✅ Only displays books with price greater than 500.


🔁 Example 5 – Add Conditional Labels

<xsl:for-each select="catalog/book">
  <h3><xsl:value-of select="title"/></h3>
  <xsl:choose>
    <xsl:when test="price &lt; 500">
      <p><em>Budget Book</em></p>
    </xsl:when>
    <xsl:otherwise>
      <p><em>Premium Book</em></p>
    </xsl:otherwise>
  </xsl:choose>
</xsl:for-each>

✅ Applies conditional content using <xsl:choose>.


🧬 Example 6 – Nested Templates with apply-templates

<xsl:template match="/catalog">
  <xsl:apply-templates select="book"/>
</xsl:template>

<xsl:template match="book">
  <div>
    <xsl:apply-templates select="title"/>
    <xsl:apply-templates select="author"/>
  </div>
</xsl:template>

<xsl:template match="title">
  <h3><xsl:value-of select="."/></h3>
</xsl:template>

<xsl:template match="author">
  <p>Author: <xsl:value-of select="."/></p>
</xsl:template>

✅ Enables modular, reusable transformations.


✅ Best Practices for Building XSLT Solutions

  • ✔️ Use xsl:for-each for structured iteration
  • ✔️ Use xsl:sort and xsl:choose for dynamic content
  • ✔️ Modularize logic with <xsl:apply-templates>
  • ✔️ Format clean output using <xsl:output method="html" indent="yes"/>
  • ❌ Don’t mix presentation and logic—keep templates clean

📌 Summary – Recap & Next Steps

These XSLT examples demonstrate how to transform XML into clean, structured, and readable outputs. Whether you’re generating HTML pages, modifying XML schemas, or filtering data, XSLT gives you the power to handle any transformation task declaratively.

🔍 Key Takeaways:

  • Transform XML to HTML or XML with clean structure
  • Use loops, conditions, and sorting to customize output
  • Apply templates for modular, recursive processing

⚙️ Real-world relevance: Used in publishing pipelines, e-commerce catalogs, configuration file updates, CMS engines, and API data transformers.


❓ FAQs – XSLT Examples

❓ Can I combine sorting, filtering, and formatting in one stylesheet?
✅ Absolutely. XSLT is designed to support all these features together.

❓ What tools can I use to test XSLT?
✅ Use browser DevTools, Oxygen XML, XMLSpy, or online tools like xsltplayground.xmlprime.com.

❓ Can I use JavaScript with XSLT?
✅ Yes, via the XSLTProcessor in browsers. But core logic should stay in the stylesheet.

❓ Can I transform XML to CSV or plain text with XSLT?
✅ Yes. Use <xsl:output method="text"/>.

❓ Is XSLT fast enough for real-time use?
✅ Yes, especially when used server-side or cached.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

XSLT Examples

Or Copy Link

CONTENTS
Scroll to Top