Extensible Stylesheet Language - XSL


Definition

An XML language that is used in XSL Stylesheets and tells Transformers how to output and format the data found in a xml file

Transformers use data found in a XML document and the formatting logic found in an XSL stylesheet to output formatted data


Components

There are 2 kinds of components in an XSL Stylesheet: Instructions and Literals

Instructions describe how the cource codument will be transformed

Literals - usually html code and text - are output just as they appear in the style sheet

XPATH

Very similar to file path location

Slashes '/' denote next child element in the XML source document. For example, the foloowing XML source file:


<sait>
  <courses>
    <course>
      ...

To get to the course element in the XSL stylesheet using XPATH, you would use:

'sait/courses/course'

To traverse back up the element tree from course, use '../course' to get tothe parent element


Syntax

Always start with <xsl:template match="/">


<xsl:stylesheet>  
  <xsl:template match="/">
  
  <html>
    <head>
      <title>Untitled</title>
    </head>
    <body>
    
      // Display data in the body tag
      <xsl:value-of select=""/>
    
    </body>
  </html>  
  
  </xsl:template>
</xsl:stylesheet>

Outputing data

To output the text (or body) of an element use:

<xsl:value-of select="element-parent/element-name"/>

To output the text of an element's attribute use:

<xsl:value-of select="element-parent/element-name/@attribute-name"/>


Processing Logic

XSL has many typical processing features that programming languages share


<xsl:for-each></xsl:for-each>         // foreach loop

<xsl:if test="blah == '0'"></xsl:if>  // if statement

<xsl:sort select="blah"               // sort statement (ascending order implied)
    datatype="number"
    order="descending">
</xsl:sort>    

<xsl:choose>                          // choose statement
  <xsl:when test="blah == '0'"></xsl:when>
  
  // and optionally
  <xsl:otherwise></xsl:otherwise>

</xsl:choose>    

Other Features

Templates:


<xsl:stylesheet>
  
  // Use a template
  <xsl:call-template name="myTemplate"/>

  // Define a template
  <xsl:template name="myTemplate">
    <xsl:value-of select="@name"/>
    <xsl:value-of select="language/>
    <xsl:value-of select="province"/>
  </xsl:template>
  
</xsl:stylesheet>

Assign values to html attributes using:

<xsl:attribute name="attr-name">value</xsl:attribute> nested inside an HTML element:


<a>Google.com
  <xsl:attribute name="href">https://www.google.com</xsl:attribute>
</a>