home mail me! syndication

overthinkings

by Ruben Daniels

Followup questions on Xpath/XSLT for Safari

So, my article got posted on Ajaxian :). I got 2 questions from Rui Lopes:

“This is really an excellent job! I read the post stating the library sizes - 12.2kb for XPath and 4.6kb for XSLT - these sizes are just plain sick.

Two questions: support for other browsers? It should be great to have a standard way to perform XPath and apply XSLT without tinkering between each browser’s features; second, is full XSLT 1.0 supported? if not, which features aren’t supported?

Thanks for your work!”

Of course that’s great to hear! I will answer those questions here:

Support for other browsers

For Javeline Platform I have created a way to use the selectSingleNode and selectNodes methods for all browsers. Of course there are people who are really hooked on the ’standard’ way that is implemented in Gecko and Opera, but I am a big fan of the simplicity of this interface.

For this to work several constructors have to be extended with the aforementioned methods. The nightly of Safari supports this, as well as Opera and Firefox. Safari 1.4 however, doesnt. To fix this I use the following ‘hack’.

if(IS_SAFARI_OLD){
   HTMLElement = HTMLDocument = XMLDocument = Element = {};
}

So now we are ready to extend constructors using the prototype construct:

if(IS_SAFARI){
   HTMLDocument.prototype.selectNodes =
   Element.prototype.selectNodes =
   XMLDocument.prototype.selectNodes = function(sExpr, contextNode){
      return XPath.selectNodes(sExpr, contextNode || this);
   }
   HTMLDocument.prototype.selectSingleNode =
   Element.prototype.selectSingleNode =
   XMLDocument.prototype.selectSingleNode = function(sExpr, contextNode){
      return XPath.selectNodes(sExpr, contextNode || this)[0];
   }
}

In the same way we can extend Opera and Firefox. A similar solution can be created to have XSLT support in a generic way. You can download the solution here.

Is full XSLT 1.0 supported?

No. I have implemented what I needed for the projects we’ve done. Currently the following is implemented:

  • value-of
  • copy-of
  • if
  • for-each
  • choose
  • apply-templates
  • when
  • otherwise

If anyone wants to help me to finish this, you are very welcome ;).

4 Comments »

  Keith Chadwick wrote @ March 2nd, 2007 at 9:46 pm

Thankyou thankyou thankyou Ruben. You just saved me dropping Safair from my every dwindling list of supported browsers.

You mentioned the commands supported on xslt. I see both apply-templates and for-each. Does this means it supports xsl:sort? If so I owe you several virtual beers :-)

Cheers
Keith Chadwick

  Ruben wrote @ June 7th, 2007 at 10:25 pm

Many people have requested for a working example of the XSLT lib. I’ve updated the XSLT implementation with several more tags as well as in a joined effort with Sarissa and Sarissa users created a working demo. For now you can download that here: http://developer.javeline.net/downloads.php

  Raj wrote @ November 27th, 2007 at 10:52 am

function SYS_Browser_SelectNodes( objDoc, query )
{
if ( g_objBrowser.IsIE() )
{
return (objDoc.selectNodes( query ));
}
else
{
var rgItemFields = new Array();
var j = 0;
if(objDoc)
{
//var pXPE = new XPathEvaluator();
var pXPE = objDoc.ownerDocument || objDoc;

var xPathResult = pXPE.evaluate( query, objDoc, null, 5, null );

if (xPathResult )
{

var curfield = xPathResult.iterateNext();
while (curfield)
{
rgItemFields[j] = curfield;
j++;
curfield = xPathResult.iterateNext();
}
}
}
return rgItemFields;
}
}

Please let me know the replace of the above code in safari browser

  Ruben wrote @ November 28th, 2007 at 11:48 am

Hi Raj,

function SYS_Browser_SelectNodes(objDoc, query){
return XPath.selectNodes(query, objDoc);
}

regards,

Ruben

Your comment

HTML-Tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>