Can AJAX dealing with XSLT instead DOM?

Discussion in 'XML' started by RC, Nov 14, 2006.

  1. RC

    RC Guest

    Let's say:

    if (XMLHttpRequestObject.readyState == 4 &&
    XMLHttpRequestObject.status == 200)
    {
    // Now I got an XML object here
    var xmlDocument = XMLHttpRequestObject.responseXML;
    // next I have dealing with XML file tags used DOM.
    var options = xmlDocument.getElementsByTagNameNS(null,"myXMLTag");
    doMyFunctionWithHTMLDOM(options);
    }

    The problem is DOM requires you are an excellent JavaScript programmer
    to deal with firstChild, lastChild, nextSibling, etc.
    And 2nd problem is different browsers will have different results.
    Sometime work in Firefox/Netscape, but not work in IE, etc.

    In my XML file has a line
    <?xml-stylesheet type="text/xsl" href="myXSLTFile.xsl" ?>

    I would like use a very simple XSLT file do the work, no DOM programming
    deal with those "children".


    So back to my question, How to use AJAX to deal with XSLT instead deal
    with DOM?
    Thank you very much in advance!

    Here is an example of XML and XSLT files, they are working fine.

    ------------XML----------------------------------------
    <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
    <?xml-stylesheet type="text/xsl" href="test1.xsl" ?>

    <states>
    <state_code>NY</state_code>
    <state_code>NJ</state_code>
    <state_code>NM</state_code>
    <state_code>NC</state_code>
    <state_code>NH</state_code>
    </states>

    ------------XSLT-----------------------------------------------
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:eek:utput method="html" />

    <xsl:template match="states">
    <form>
    <select>
    <xsl:apply-templates select="state_code" />
    </select>
    </form>
    </xsl:template>

    <xsl:template match="state_code">
    <option value="{text()}">
    <xsl:value-of select="text()" />
    </option>
    </xsl:template>

    </xsl:stylesheet>
     
    RC, Nov 14, 2006
    #1
    1. Advertising

  2. RC wrote:

    > // Now I got an XML object here
    > var xmlDocument = XMLHttpRequestObject.responseXML;



    > The problem is DOM requires you are an excellent JavaScript programmer
    > to deal with firstChild, lastChild, nextSibling, etc.


    Your pure (or core) JavaScript skills don't matter much to manipulate
    the DOM, you need to know the document object model and the differences
    in the browser implementations.

    > And 2nd problem is different browsers will have different results.
    > Sometime work in Firefox/Netscape, but not work in IE, etc.
    >
    > In my XML file has a line
    > <?xml-stylesheet type="text/xsl" href="myXSLTFile.xsl" ?>
    >
    > I would like use a very simple XSLT file do the work, no DOM programming
    > deal with those "children".
    >
    >
    > So back to my question, How to use AJAX to deal with XSLT instead deal
    > with DOM?


    I don't think you gain much by moving to XSLT as you still need script
    to run the transformation and as the differences in XSLT processor APIs
    are worse than the DOM differences. And finally you need to insert the
    result of the XSLT transformation into the HTML DOM document.

    Mozilla's XSLT processor API is documented here:
    <http://developer.mozilla.org/en/docs/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations>
    Opera 9 supports the same API.

    IE 6 and later use MSXML 3 to support XSLT 1.0, there are different APIs
    you can use with MSXML (e.g. transformNode, transformNodeToObject, and
    an XSLT processor API to set parameters).

    Opera 8 does not support client side XSLT 1.0. Nor do IE 5 and 5.5
    unless MSXML 3 is installed in addition to IE.

    Safari/Konqueror I think support client-side XSLT but have no API
    exposed to JavaScript.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Nov 14, 2006
    #2
    1. Advertising

  3. RC schrieb:
    > The problem is DOM requires you are an excellent JavaScript programmer


    Take a look at the http://jquery.com/ lib. Very handy.

    > I would like use a very simple XSLT file do the work, no DOM programming
    > deal with those "children".


    Why not apply the XSLT on the server side before sending it to the client?

    -Thomas
     
    Thomas Wittek, Nov 14, 2006
    #3
  4. RC wrote:
    > Let's say:
    >
    > if (XMLHttpRequestObject.readyState == 4 &&
    > XMLHttpRequestObject.status == 200)
    > {
    > // Now I got an XML object here
    > var xmlDocument = XMLHttpRequestObject.responseXML;
    > // next I have dealing with XML file tags used DOM.
    > var options = xmlDocument.getElementsByTagNameNS(null,"myXMLTag");
    > doMyFunctionWithHTMLDOM(options);
    > }
    >
    > The problem is DOM requires you are an excellent JavaScript programmer
    > to deal with firstChild, lastChild, nextSibling, etc.


    That doesn't require extensive Javascript knowledge. It does require
    knowledge of the DOM--which isn't more complex than XSLT and XPATH.
    Besides that, you have to use Javascript to apply the XSLT
    transformation anyway, as well as to accomplish whatever update of the
    current page needs to occur after the transformation is complete. *That*
    requires you to program with the HTML DOM--and if you know how to do
    that, then you pretty much know how to use the pertinent features of the
    XML DOM too.
     
    Harlan Messinger, Nov 14, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thorsten Meininger
    Replies:
    0
    Views:
    448
    Thorsten Meininger
    Jul 28, 2004
  2. Tom Corcoran

    Xquery instead of xslt

    Tom Corcoran, Sep 18, 2003, in forum: XML
    Replies:
    2
    Views:
    872
    Xavier Franc
    Sep 18, 2003
  3. Thorsten Meininger
    Replies:
    0
    Views:
    517
    Thorsten Meininger
    Jul 28, 2004
  4. minnie
    Replies:
    1
    Views:
    727
    Andrew Thompson
    Dec 13, 2006
  5. Giggle Girl

    Using DOM to change styles instead of IDs...

    Giggle Girl, Mar 23, 2006, in forum: Javascript
    Replies:
    14
    Views:
    156
    Thomas 'PointedEars' Lahn
    Mar 24, 2006
Loading...

Share This Page