XSLT v0.70
This is a jQuery Plugin for Google's AJAXSLT. It gives you the
ability to transform XML/XSL from JavaScript. AJAXSLT is a parser itself, the library does not depend on
your browser being able to do XSL transforms. The plugin also provides functionality to load XML/XSL through
$.ajax() calls and transform them.
Table of Contents
XSLT
Table of Contents
License
- Plugin
- AJAXSLT
Download
Demo
Reference
- $.xslt.version
- $.xslt.textToXML(text)
- $.xslt.xmlToText(xml)
- $.xslt(options)
- $(...).xslt(options)
Notes
License
This plugin is divided in two parts for licensing, even though it is a single file. It is clearly
marked in the file where my code ends and Google's code begins.
License - Plugin
The part of the code that I made is under MIT or Public Domain license. Whichever suits your needs.
License - AJAXSLT
The code for Google's AJAXSLT is incorporated in the file. This part of the code has the following license:
Copyright (c) 2005,2006 Google Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its contributors
may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Download
You can download the plugin below. The size of the file is about 115kb, but with JSMin it drops to 68kb,
and JSMin+GZ drops it to a mere 15kb.
Download
Demo
This is one of the lamest demo's ever, but you can see it in action in the simplest way
here.
Reference
Reference - $.xslt.version
Object with two members: plugin and ajaxslt.
Reference - $.xslt.textToXML(text)
Converts a text string containing XML to an XML node usable by AJAXSLT. Really just calls the xmlParse
function from AJAXSLT. It only exists to provide a jQuery-like function for a common operation.
Reference - $.xslt.xmlToText(xml)
Converts an XML node to a text string. Really just calls the xmlText function from AJAXSLT.
It only exists to provide a jQuery-like function for a common operation.
Reference - $.xslt(options)
This is the core of the XSLT plugin and together with the $(...).xslt(options) function probably
all you need to use and know about.
This function can take XML and XSL from text strings and XML nodes, if necessary retrieve them from the
server through $.ajax() calls both synchronously and asynchronously, use AJAXSLT to do the
transformation and provide that back as return value or to a callback or assign it to DOM elements. It
also provides caching.
Parameters
All parameters are passed through the options object. Members:
-
xml / xsl (null): A text string or XML/DOM node representing XML or XSL. Omit or set
to null to use the URL variants instead.
-
xmlUrl / xslUrl (null): URLs to retrieve the XML/XSL from. These are only used if the
xml or xsl members are set to null. Note that you can load one from an URL and provide
the other normally without issue.
-
xmlCache / xslCache (true): Use the internal cache? If set to true (default),
XML/XSL loaded externally through the xmlUrl and xslUrl members are saved to and loaded
from an internal cache. If set to false, this cache will not be used - note that the results will
also not be saved to the cache. Also note that if you set this to false, you still need to take measures
like sending the correct headers and maybe using a parameter in the URL to make sure the data is not
pulled from the browser cache.
-
callback (null): Function to call when the transformation is complete. The callback function
is passed one parameter: the transformed data (as a text string). The parameter will be false if
an error has occured.
-
target (null): A target to assign with the results of the transform. The assignment is done
by $(target).html(result), so you can set target to a DOM element, a jQuery object, a string,
etc.
-
dataTypeXML (false): Use dataType = 'xml' in $.ajax calls? 'html' is used by default. This
option is disabled by default. Advantages of using it is that the XML objects are created by the browser
natively, which is probably faster than the JavaScript parser in AJAXSLT. Disadvantages are that Internet
Explorer will choke if the correct headers are not sent, and ofcourse there may be differences in
the parsing by the different browsers.
Return values
The return value of the function can be either the transformed XML as a text string, true or false.
-
string: The operation was successful, and the value is the transformed XML.
-
true: The operation has gone async and is working in the background. The callback, if any, will be called
when the operation is complete and the target, if any, assigned with the result of the transform. See Async
vs. Sync below for more information.
-
false: An error has occured. Note that this may also be passed to the callback function.
Async vs. Sync
The function running Async or Sync depends on a number of factors.
By default, the function runs sync, and returns the transformation result. However, if any files need to be loaded
through $.ajax() calls (which means that they are not in cache or caching is disabled) the function may go
async. However, it will only go async if either a callback or target is provided. If no callback or target is
provided, the $.ajax() calls will be made sync as well (which can make the browser temporarily unresponsive).
Ofcourse, the callback function is called wether the function goes async or not.
Reference - $(...).xslt(options)
This function is practically the same as the $.xslt(options) function. The differences are that
the target option is automatically set to the jQuery object and the function returns this to
maintain chainability instead of true, false, or html.
Notes
AJAXSLT is a very nice library, but it does not implement XSLT in full. Most common things are supported
though. It also breaks fairly easily and it can be hard to find where the problem is at.
Ofcourse, the plugin has not changed the AJAXSLT code, so it's function names run the risk of conflicting
with your own.
Laters
Laters and have fun,
- Chainfire