[xsl] XSLT Templates in a Web Browser

Subject: [xsl] XSLT Templates in a Web Browser
From: "Thomas Stone" <stonethomasjames@xxxxxxxxx>
Date: Sun, 31 Dec 2006 01:36:52 -0800
   This is a post of one way to test multiple XSLT templates producing web
browser output against a single source XML document.

   I am relatively new to XSLT, though I have been programming since the
Commodore 64.  While testing the intricacies of the language, I primarily used
it to transform XML data into HTML output.  The XML document referenced an
XSTL stylesheet.  When I double-clicked on the .xml file, the web browser
found its associated XSLT stylesheet and displayed the HTML output.  This
really helped in getting "hands-on" experience with instant feed-back.

   The least satifactory part of this process was having to edit my XML
document every time I wanted to display the data differently - to change the
<?xml-stylesheet type="text/xsl" href="Transform.xml"?> line.  I an old
control freak when it comes to data files, and storing the desired "program"
to run against a data file INSIDE the data file just didn't feel right.  The
solution I came up with uses the document() function.

   The trick I found was to rename my XSLT stylesheet to .xml so the browser
will open it and look for an associated stylesheet instead of opening the
source XML document.  The following is an example of my stylesheet.

<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="Transform.xml"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:variable name="transform_document">Test.xml</xsl:variable>

  <xsl:template match="/">
    <xsl:apply-templates select="document($transform_document)"
mode="document"/>
  </xsl:template>

  <xsl:template match="/" mode="document">
    <xsl:apply-templates />
  </xsl:template>
</xsl:stylesheet>


   This makes my stylesheet its own stylesheet.  The document I want it to
transform is stored in a variablee near the top of the sheet - in this case it
is called "Test.xml".  If I make a copy of this stylesheet, and change the
<?xml-stylesheet type="text/xsl" href="Transform.xml"?> line to match the
copy's name, I can make changes in the second stylesheet and immediately
compare the changes in resulting HTML output against my original.  I did not
have to edit my XML document.

   A second benefit of this is that the source XML document does not have to
be named as .xml.  Virtual Reality Modeling Language text content in an X3D
file could be referenced by changing the transform_document variable
declaration to state "HelloWorld.x3d".  An Excel document that has been saved
in XML format or the Open Office documents could be source documents just as
easily, showing the data in a web browser instead of their native
application.

--
___________________________________________________
Search for products and services at:
http://search.mail.com

Current Thread