XSL Notes Page 1

  1. XSL Consists of 3 parts
    1. XSLT (Language for transforming XML documents)
    2. XPath (Language for defining parts of XML documents)
    3. XSL (Vocabulary for formatting XML documents)
  2. XSL Languages
  3. Browsers
  4. XSL - Transformation (XSLT)
  5. The <xsl:template> Element
    XSL style sheets contain template elements that hold the rules to apply
    when a specified node is matched
  6. The <xsl:value-of> Element
    Used to Select the Value of an XML Element and add it
    To the OutPut Stream of the Transformation
    Example:
                <?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 = "/">
                    <html>
                      <body>
                        <h2>My CD Colletion</h2>
                        <table border = "1">
                          <tr bgcolor = "blue">
                            <th align = "left>Title</th>
                            <th align = "left>Artist</th>
                          <tr>
                          <tr>
                            <td><xsl:value-of select = "catalog/cd/title" /></td>
                            <td><xsl:value-of select = "catalog/cd/artist" /></td>
                          </tr>
                        </table>
                      </body>
                    </html>
                  </xsl:template>
                </xsl:stylesheet>
              
    The value of the required select attribute contains an XPath experession
    which resembles navaigating a file system where "/" selects a subdirectory"
  7. The XSL <for-each> Element
    Used to Select every XML Element of a node set
    Example:
                <?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 = "/">
                    <html>
                      <body>
                        <h2>My CD Colletion</h2>
                        <table border = "1">
                          <tr bgcolor = "blue">
                            <th align = "left>Title</th>
                            <th align = "left>Artist</th>
                          <tr>
                          <xsl:for-each select = "catalog/cd">
                            <tr>
                              <td><xsl:value-of select = "title" /></td>
                              <td><xsl:value-of select = "artist" /></td>
                            </tr>
                          </xsl:for-each>
                        </table>
                      </body>
                    </html>
                  </xsl:template>
                </xsl:stylesheet>
              
    The value of the required select attribute contains an XPath experession
    which resembles navaigating a file system where "/" selects a subdirectory"
  8. The XSL <sort> Element
    Used to Sort the XML Elements of a node set
    Example:
              <?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 = "/">
                  <html>
                    <body>
                      <h2>My CD Colletion</h2>
                      <table border = "1">
                        <tr bgcolor = "blue">
                          <th align = "left>Title</th>
                          <th align = "left>Artist</th>
                        <tr>
                        <xsl:for-each select = "catalog/cd">
                        <xsl:sort select = "artist" />
                          <tr>
                            <td><xsl:value-of select = "title" /></td>
                            <td><xsl:value-of select = "artist" /></td>
                          </tr>
                        </xsl:for-each>
                      </table>
                    </body>
                  </html>
                </xsl:template>
              </xsl:stylesheet>
            
    The value of the required select attribute contains an XPath experession
    which resembles navaigating a file system where "/" selects a subdirectory"
  9. If: The <xsl:if> Element
    Used to Select the XML Elements of a node set
    Only if a specified condition is true
    Watch out for "&gt" to make ">" in the xsl file!
    Example:
              <?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 = "/">
                  <html>
                    <body>
                      <h2>My CD Colletion</h2>
                      <table border = "1">
                        <tr bgcolor = "blue">
                          <th align = "left>Title</th>
                          <th align = "left>Artist</th>
                        <tr>
                        <xsl:for-each select = "catalog/cd">
                          <xsl:if test = "price &gt; 10">
                            <tr>
                              <td><xsl:value-of select = "title" /></td>
                              <td><xsl:value-of select = "artist" /></td>
                            </tr>
                          </xsl:if>
                        </xsl:for-each>
                      </table>
                    </body>
                  </html>
                </xsl:template>
              </xsl:stylesheet>
            
    If the price > 10 then select the titles and artists
  10. Choose: The <xsl:choose> Element
    Used to express multiple conditional tests Using
    <xsl:choose>
    <xsl:when>
    <xsl:otherwise>
    Example:
              <?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 = "/">
                  <html>
                    <body>
                      <h2>My CD Colletion</h2>
                      <table border = "1">
                        <tr bgcolor = "blue">
                          <th align = "left>Title</th>
                          <th align = "left>Artist</th>
                        <tr>
                        <xsl:for-each select = "catalog/cd">
                            <tr>
                              <td><xsl:value-of select = "title" /></td>
                                <xsl:choose>
                                  <xsl:when test = "price &gt; 10">
                                    <td bgcolor = "pink"><xsl:value-of select = "artist" /></td>
                                  </xsl:when>
                                  <xsl:otherwise>
                                    <td><xsl:value-of select = "artist" /></td>
                                  </xsl:otherwise>
                                </xsl:choose>
                            </tr>
                        </xsl:for-each>
                      </table>
                    </body>
                  </html>
                </xsl:template>
              </xsl:stylesheet>
            
    The above code will add a pink background to the artist column when price > 10
  11. Apply-Templates: The <xsl:apply-templates> Element
    Applies a template rule to the current element or it's childNodes
    Add a select attribute to the <xsl:apply-templates> element to process
    Example:
              <?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 = "/">
                  <html>
                    <body>
                      <h2>My CD Colletion</h2>
                      </xsl:apply-templates>
                    </body>
                  </html>
                </xsl:template>
    
                  <xsl:template match = "cd">
                    <p>
                      <xsl:apply-templates select = "title" />
                      <xsl:apply-templates select = "artist" />
                    </p>
                  </xsl:template>
    
                  <xsl:template match = "title">
                    Title: <span style = "color:red"><xsl:value-of select = "," /></span><br />
                  </xsl:template>
    
                  <xsl:template match = "artist">
                    Artist: <span style = "color:blue"><xsl:value-of select = "," /></span><br />
                  </xsl:template>
    
              </xsl:stylesheet>
            
  12. On the Client
    1. Use JavaScript to do browser specific testing for different style sheets
    2. Leave out the XML reference to the XSL file, then load it on the broswer
    Example:
              <html>
                <body>
                  <script type = "text/javascript">
    
                    // Load XML
                       var xml = new ActiveXObject("Microsoft.XMLDOM")
                       xml.async = false
                       xml.load("filename.xml")
    
                    // Load XSL
                       var xsl = new ActiveXObject("Microsoft.XMLDOM")
                       xsl.async = false
                       xsl.load("filenae.xsl")
    
                    // Transform
                       document.write(xml.transformNode(xsl))
    
                  </script>
                </body>
              </html>
            
  13. On the Server
    Transform the XML to XHTML on the Server
    Example: ASP
                  <$
                   ' Load XML
                     set xml = Server.CreateObject("Microsoft.XMLDOM")
                     xml.async = false
                     xml.load(Server.MapPath("filename.xml"))
    
                    ' Load XSL
                      set xsl = Server.CreateObject("Microsoft.XMLDOM")
                      xsl.async = false
                      xsl.load(Server.MapPath("filename.xsl"))
    
                    ' Transform
                      Response.Write(xml.transformNode(xsl))
                  %>
                
  14. XSLT Elements
    Element Description
    xsl:apply-imports Applies a template rule from an imported style sheet
    xsl:apply-templates Applies a template rule to the current element
    or to the current element's childNodes
    xsl:attribute Adds an attribute
    xsl:attribute-set Defines a named set of Attributes
    xsl:call-template Calls a Named Template
    xsl:choose Expresses multiple condition tests
    xsl:comment Creates a comment node in the result tree
    xsl:copy Creates a copy of the current node
    with childNodes and attributes
    xsl:copy-of Creates a copy of the current node
    with childNodes and attributes
    xsl:decimal-format Defines the characters and symbols
    to be used when converting numbers into strings
    with the format-number() function
    xsl:element Creates an element node in the output document
    xsl:fallback Specifies an alternate code to run
    if the XSL processor doesn't support an element
    xsl:for-each for loop
    xsl:if If statement
    xsl:import Imports the contents of one style sheet into another
    xsl:include Includes the contents of one style sheet into another
    xsl:key Declares a Named Key that can be used in the style sheet
    with the key() function
    xsl:message Writes a message to the output (error reporting)
    xsl:namespace-alias Replaces a namespace in the style sheet
    to a different NameSpace in the output
    xsl:number Determines the integer position of the current node
    and formats a number
    xsl:otherwise The otherwise part of choose
    xsl:output Defines the format of the output document
    xsl:param Declares a local or global parameter
    xsl:preserve-space Defines the elements for which white space
    should be preserved
    xsl:processing-instruction Writes a processing instruction to the output
    xsl:sort Sort Statement
    xsl:strip-space Defines the elements for which white space
    should be removed
    xsl:stylesheet Defines the root element of a style sheet
    xsl:template Rules to apply when a specified node is matched
    xsl:text Writes literal text to the output
    xsl:transform Defines the root element of a style sheet
    xsl:value-of Extracts the value of a selected node
    xsl:variable Declares a local or global variable
    xsl:when Specifies an action for the choose element
    xsl:with-param Defines the value of a parameter to be passed
    to a template
  15. XSLT and Inherited XPath Functions
Hosted by www.Geocities.ws

1