XSLT

From virtualMV2015wiki
Jump to: navigation, search
 Home  Home <

Introduction

eXtensible Stylesheet Language (XSLT) Based on a presentation by Daniel Rutten[1]


Icon Objective.png

Objective XSLT

By the end of this page you will be able to:

  • Understand eXtensible Stylesheet Language (XSLT)

Brief history

  • Derived from SGML DSSSL(Document Style Semantics and Specification Language or “Dissel”)
  • Describes how to display XML documents
  • Consists of three parts
    • Transforming XML documents (XSLT)
    • Navigating in XML documents (XPath)
    • Formatting XML documents (XSL-FO)

What is XSLT

  • XSLT is the most important part of XSL
  • XSLT transforms an XML document into another XML document
  • XSLT uses XPath to navigate in XML documents
  • XSLT is a W3C Recommendation

An example XSLT file

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/addressbook">
        <xsl:for-each select="card">
            <xsl:value-of select="name" />
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Two Golden rules for XSLT

  • All XSLT files are XML files
  • All XSLT files have this root element:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Six useful elements of XSLT

  • <xsl:template> is used to build templates
  • <xsl:value-of> is used to extract values
  • <xsl:for-each> is used to select every element
  • <xsl:sort> is used to sort the output
  • <xsl:if> is used to express conditional test
  • <xsl:choose> is used to express multiple conditional tests (in conjunction with <xsl:when> and <xsl:otherwise>)

Example: Address book

Define XML structure

<?xml version="1.0" encoding="ISO-8859-1"?>
<addressbook>
    <card>
        <name>Clicker Software Solutions</name>
        <web>www.clicker.co.nz</web>
        <email>office@clicker.co.nz</email>
        <phone>+64 876 9003</phone>
        <address>
            <street>709 Maxville Drive</street>
            <city>Hastings</city>
            <zipcode>4201</zipcode>
            <country>New Zealand</country>
        </address>
    </card>
    <card>
        <name>4TEC</name>
        <web>www.4tec.nl</web></card>
    <card>
        <name>Real-Time Innovations</name>
        <web>www.rti.co</web></card>
</addressbook>

Describe XSL transformation

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/addressbook">
        <html>
            <body>
                <xsl:for-each select="card">
                    <h1>
                        <xsl:value-of select="name" />
                    </h1>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Linking XSLT to XML

Add the xml-stylesheet link to the XML file

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="address.xsl"?><addressbook>
    <card>
        <name>Clicker Software Solutions</name>
        <web>www.clicker.co.nz</web>
...

When shown in a web browser will display

Clicker Software Solutions

4TEC

Real-Time Innovations

Transformation of XML to HTML

Sorting cards

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/addressbook">
        <html>
            <body>
                <xsl:for-each select="card">
                    <xsl:sort select="name" />                    <h1><xsl:value-of select="name" /></h1>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>


4TEC

Clicker Software Solutions

Real-Time Innovatons

Sample output for sorting cards

Adding contact details

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/addressbook">
        <html>
            <body>
                <xsl:for-each select="card">
                    <h1><xsl:value-of select="name" /></h1>
                    <p>                        Web: <xsl:value-of select="web" /><br />                        Email: <xsl:value-of select="email" /><br />                        Phone: <xsl:value-of select="phone" /><br />                        Fax: <xsl:value-of select="fax" />                    </p>                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>


4TEC

Web: www.4tec.nl
Email: office@4tec.nl
Phone: +31 6 10 88 89 02
Fax: +31 70 449 0539

Clicker Software Solutions

Web: www.clicker.co.nz
Email: office@clicker.co.nz
Phone: +64 876 9003
Fax:

Real-Time Innovatons

Web: www.rti.com
Email:
Phone:
Fax:

Sample output

Conditional display

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/addressbook">
        <html>
            <body>
                <xsl:for-each select="card">
                    <h1><xsl:value-of select="name" /></h1>
                    <p>
                        <xsl:if test="web">                            Web: <xsl:value-of select="web" /><br />
                        </xsl:if>                        <xsl:if test="email">                            Email: <xsl:value-of select="email" /><br />
                        </xsl:if>                        <xsl:if test="phone">                            Phone: <xsl:value-of select="phone" /><br />
                        </xsl:if>                        <xsl:if test="fax">                            Fax: <xsl:value-of select="fax" />                        </xsl:if>                    </p>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>


4TEC

Web: www.4tec.nl
Email: office@4tec.nl
Phone: +31 6 10 88 89 02
Fax: +31 70 449 0539

Clicker Software Solutions

Web: www.clicker.co.nz
Email: office@clicker.co.nz
Phone: +64 876 9003

Real-Time Innovatons

Web: www.rti.com

Sample output

Add an address

<xsl:value-of select="address/street" />
<xsl:value-of select="address/city" />
<xsl:value-of select="address/zipcode" />
<xsl:value-of select="address/country" />


4TEC

Web: www.4tec.nl
Email: office@4tec.nl
Phone: +31 6 10 88 89 02
Fax: +31 70 449 0539
Bankaplein 2
Den Haag
2585 EV
The Netherlands

Clicker Software Solutions

Web: www.clicker.co.nz
Email: office@clicker.co.nz
Phone: +64 876 9003
709 Maxwell Drive
Hastings
4201
New Zealand

Real-Time Innovatons

Web: www.rti.com

Sample output

Filter

You can filter what is returned as follows

  <xsl:for-each select="card[address/city='Napier']">

Adding spaces

As HTML has issues with spaces, you can use &#160; to add a space

     Address: <xsl:value-of select="street" />,&#160;<xsl:value-of select="city" />

will add street and city together separated by comma and space

Bankaplein 2, Den Haag
Sample output


Icon References.png References

  1. Rutten, D. (2009) An introduction to XML and XSLT. Clickwise Smart Software Solutions. To Dot Net User Group, Napier, New Zealand [PPT]

XSLT. (2017). In virtualMV's ( Michael Verhaart ) wiki. Retrieved June 23, 2017, from http://www.virtualmv.com/wiki/index.php?title=XSLT    (zotero)