ThePlace

Home ] Search ] Resources ] Site Map ] Contact Me ]
Dave's Information Technology Resource

Up ]

XSL and Transformation ] XSL and the Server ] XSL and the Client ] [ XSL Functions ]

--- XSL Functions ---

This page provides some examples of programming via XSL via JavaScript:

XSL Sort

bulletTake a look at the XML document that you have seen previously (or open it with IE5/6):

 

<?xml version="1.0" encoding="ISO8859-1" ?>
<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
.
.
.

 

bulletTo output this XML file as an ordinary HTML file, and sort it at the same time, simply add an order-by attribute to your for-each element like this:

<xsl:for-each select="CATALOG/CD" order-by="+ ARTIST">

bulletThe order-by attributes takes a plus (+) or minus (-) sign, to define an ascending or descending sort order, and an element name to define the sort element.
bulletNow take a look at your slightly adjusted XSL stylesheet (or open it with IE5/6):

 

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
  <html>
  <body>
    <table border="2" bgcolor="yellow">
      <tr>
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="CATALOG/CD" order-by="+ 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>

 

bullet Transforming it on the Client
bulletHere is the simple source code needed transform the XML file to HTML on the client (try it yourself):

 

<html>
<body>
<script language="javascript">
// Load XML 
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cd_catalog.xml")

// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cd_catalog_sort.xsl")

// Transform
document.write(xml.transformNode(xsl))
</script>

</body>
</html>

 


XSL Filter Query

bulletTake a new look at the following XML document (or open it with IE5/6):

 

<?xml version="1.0" encoding="ISO8859-1" ?>
<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
.
.
.

 

bulletTo filter the XML file, simply add filter to the select attribute in your for-each element like this:

<xsl:for-each select="CATALOG/CD[ARTIST='Bob Dylan']">

bulletLegal filter operators are:
bullet=  (equal)
bullet=! (not equal)
bullet&LT& less than
bullet&GT& greater than
bulletNow take a look at your slightly adjusted XSL stylesheet (or open it with IE5):

 

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
  <html>
  <body>
    <table border="2" bgcolor="yellow">
      <tr>
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="CATALOG/CD[ARTIST='Bob Dylan']">
      <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>

 

bullet Transforming it on the Client
bulletHere is the simple source code needed transform the XML file to HTML on the client (try it yourself):

 

<html>
<body>
<script language="javascript">
// Load XML 
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cd_catalog.xml")

// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cd_catalog_filter.xsl")

// Transform
document.write(xml.transformNode(xsl))
</script>

</body>
</html>


XSL Conditional IF

bulletTake a new look at the following XML document  (or open it with IE5):
<?xml version="1.0" encoding="ISO8859-1" ?>
<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
.
.
.

 

bulletTo put an conditional if test against the content of the file, simply add an xsl:if element to your XSL document like this:

<xsl:if match=".[ARTIST='Bob Dylan']">
... some output ...
</xsl:if>

bulletNow take a look at your slightly adjusted XSL stylesheet (or open it with IE5):

 

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
  <html>
  <body>
    <table border="2" bgcolor="yellow">
      <tr>
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="CATALOG/CD">
      	<xsl:if match=".[ARTIST='Bob Dylan']">
      	  <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>

 

bullet Transforming it on the Client
bulletHere is the simple source code needed transform the XML file to HTML on the client (try it yourself):

 

<html>
<body>
<script language="javascript">
// Load XML 
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cd_catalog.xml")

// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cd_catalog_filter.xsl")

// Transform
document.write(xml.transformNode(xsl))
</script>

</body>
</html>


XSL Conditional Choose

bulletTake a new look at the following XML document  (or open it with IE5):

 

<?xml version="1.0" encoding="ISO8859-1" ?>
<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
.
.
.

 

bulletTo insert an conditional choose test against the content of the file, simply add an xsl:choose, xsl:when and xsl:otherwise elements to your XSL document like this:

<xsl:choose>
   <xsl:when match=".[ARTIST='Bob Dylan']">
      ... some code ...
   </xsl:when>
   <xsl:otherwise>
      ... some code ....
   </xsl:otherwise>
</xsl:choose>

bulletNow take a look at your slightly adjusted XSL stylesheet (or open it with IE5):

 

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
  <html>
  <body>
    <table border="2" bgcolor="yellow">
      <tr>
        <th>Title</th>
        <th>Artist</th>
      </tr>
      <xsl:for-each select="CATALOG/CD">
      	<tr>
        <td><xsl:value-of select="TITLE"/></td>
      	<xsl:choose>
          <xsl:when match=".[ARTIST='Bob Dylan']">
            <td bgcolor="#ff0000"><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>

 

bullet Transforming it on the Client
bulletHere is the simple source code needed transform the XML file to HTML on the client (try it yourself):

 

<html>
<body>
<script language="javascript">
// Load XML 
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cd_catalog.xml")

// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("cd_catalog_filter.xsl")

// Transform
document.write(xml.transformNode(xsl))
</script>

</body>
</html>

Home ] Up ] Computer Architecture ] Programming Bootcamp ] Database Bootcamp ] Visual BasicS ] Web Basics ] Web Multimedia ] Web Programming ] Advanced Web Topics ] Developing Web Sites ] XML Technology ] Web Glossary ]

Copyright © 1999 - 2005 
ThePlace - Written and Sponsored by Dave Hillman