XSL Languages

Understanding the XSL family of languages

🌐 What is XSL?

XSL (Extensible Stylesheet Language) is a family of languages for styling and transforming XML documents. It consists of three main parts: XSLT for transformations, XPath for navigation, and XSL-FO for formatting.


<!-- XSL Family -->
XSL
├── XSLT (Transformations)
├── XPath (Navigation)
└── XSL-FO (Formatting)
                                    

XSL Components

🔄

XSLT

Transform XML to other formats

<xsl:template match="/">
🧭

XPath

Navigate and select XML nodes

//book[@price < 30]
📄

XSL-FO

Format XML for print and PDF

<fo:block>Content</fo:block>
🔗

XLink

Create links in XML documents

xlink:href="page.xml"

🔹 XSLT - Transformations

XSLT is the transformation language that converts XML documents into HTML, text, or other XML formats. It uses templates and XPath expressions to define transformation rules for different elements.

<!-- Input XML -->
<products>
    <item>Laptop</item>
    <item>Phone</item>
</products>

<!-- XSLT Transformation -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <ul>
                    <xsl:for-each select="products/item">
                        <li><xsl:value-of select="."/></li>
                    </xsl:for-each>
                </ul>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

HTML Output:

  • Laptop
  • Phone

🔹 XPath - Navigation

XPath is a query language for selecting nodes from XML documents. It provides a path-like syntax to navigate through elements and attributes, similar to file system paths.

<!-- Sample XML -->
<library>
    <book category="fiction">
        <title>The Novel</title>
        <price>25</price>
    </book>
    <book category="tech">
        <title>XML Guide</title>
        <price>35</price>
    </book>
</library>

<!-- XPath Examples -->
/library/book                    <!-- All books -->
//title                          <!-- All titles -->
//book[@category='fiction']      <!-- Fiction books -->
//book[price < 30]/title         <!-- Titles under $30 -->

Results:

Fiction books: The Novel

Under $30: The Novel

🔹 XSL-FO - Formatting Objects

XSL-FO is a markup language for formatting XML documents for print, PDF, and other paginated media. It defines page layouts, fonts, spacing, and other presentation details for professional document output.

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <fo:layout-master-set>
        <fo:simple-page-master master-name="A4">
            <fo:region-body margin="1in"/>
        </fo:simple-page-master>
    </fo:layout-master-set>
    
    <fo:page-sequence master-reference="A4">
        <fo:flow flow-name="xsl-region-body">
            <fo:block font-size="18pt" font-weight="bold">
                Document Title
            </fo:block>
            <fo:block font-size="12pt">
                This is the document content.
            </fo:block>
        </fo:flow>
    </fo:page-sequence>
</fo:root>

Output (PDF/Print):

Document Title

This is the document content.

🔹 XLink - Linking

XLink provides a standard way to create hyperlinks in XML documents. It supports simple links (like HTML) and extended links with multiple resources and complex relationships.

<!-- Simple XLink -->
<document xmlns:xlink="http://www.w3.org/1999/xlink">
    <reference 
        xlink:type="simple"
        xlink:href="http://example.com/page.xml"
        xlink:show="new">
        Click here for more info
    </reference>
</document>

<!-- Extended XLink -->
<resources xlink:type="extended">
    <resource xlink:type="locator" 
              xlink:href="doc1.xml" 
              xlink:label="doc1"/>
    <resource xlink:type="locator" 
              xlink:href="doc2.xml" 
              xlink:label="doc2"/>
</resources>

Link Types:

Simple: Basic hyperlinks

Extended: Multi-resource links

🔹 How XSL Languages Work Together

The XSL family of languages work together to provide complete XML processing capabilities:

Integration Example:

  1. XPath selects nodes from XML: //book[@price < 30]
  2. XSLT transforms selected nodes to HTML using templates
  3. XSL-FO formats the result for PDF output with styling
  4. XLink adds hyperlinks between related documents
<!-- XSLT using XPath -->
<xsl:template match="/">
    <xsl:for-each select="//book[@price < 30]">
        <xsl:value-of select="title"/>
    </xsl:for-each>
</xsl:template>

🔹 Choosing the Right XSL Language

Select the appropriate XSL language based on your specific needs:

Use Cases:

  • XSLT: Converting XML to HTML for websites, data transformation
  • XPath: Querying XML data, selecting specific nodes in XSLT
  • XSL-FO: Generating PDFs, creating print-ready documents
  • XLink: Creating complex document relationships, navigation systems

🧠 Test Your Knowledge

Which XSL language is used for transforming XML to HTML?