xsl-list
[Top] [All Lists]

RE: [xsl] XSLT Templates in a Web Browser

2006-12-31 08:29:20
In general the relationship of source documents to stylesheets is
many-to-many, and naming your source document within the stylesheet is just
as unsatisfactory as naming the stylesheet within the source document. If
you're going to use client-side transformation, it's best to control the
process from Javascript within your HTML pages, using an API such as Sarissa
to achieve cross-browser portability.

Michael Kay
http://www.saxonica.com/


-----Original Message-----
From: Thomas Stone [mailto:stonethomasjames(_at_)email(_dot_)com] 
Sent: 31 December 2006 09:37
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] XSLT Templates in a Web Browser

   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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>