applying 2 xslt to an xml via script?

Discussion in 'XML' started by toby989@hotpop.com, Sep 20, 2006.

  1. Guest

    Hi All

    I am completely new to this, but I was wondering if I can apply 2 xslt's
    subsequently to an xml, via the (client side) scripting method:

    http://www.w3schools.com/xsl/xsl_client.asp

    Like maybe:

    <html>
    <body>

    <script type="text/javascript">

    // Load XML
    var xml = new ActiveXObject("Microsoft.XMLDOM")
    xml.async = false
    xml.load("cdcatalog.xml")

    // Load XSL ---- 1 ----
    var xsl = new ActiveXObject("Microsoft.XMLDOM")
    xsl.async = false
    xsl.load("cdcatalog.xsl")

    // Transform ---- 1 ----
    document.write(xml.transformNode(xsl))

    // Load XSL ---- 2 -----
    var xsl2 = new ActiveXObject("Microsoft.XMLDOM")
    xsl.async = false
    xsl.load("cdcatalog2.xsl")

    // Transform ---- 2 -----
    document.write(xml.transformNode(xsl2))

    </script>

    </body>
    </html>

    Thaks for letting me know some scripting hints if anyone came across similar
    situation.

    Toby
     
    , Sep 20, 2006
    #1
    1. Advertising

  2. wrote:


    > I am completely new to this, but I was wondering if I can apply 2 xslt's
    > subsequently to an xml, via the (client side) scripting method:



    > <html>
    > <body>
    >
    > <script type="text/javascript">
    >
    > // Load XML
    > var xml = new ActiveXObject("Microsoft.XMLDOM")
    > xml.async = false
    > xml.load("cdcatalog.xml")
    >
    > // Load XSL ---- 1 ----
    > var xsl = new ActiveXObject("Microsoft.XMLDOM")
    > xsl.async = false
    > xsl.load("cdcatalog.xsl")
    >
    > // Transform ---- 1 ----
    > document.write(xml.transformNode(xsl))
    >
    > // Load XSL ---- 2 -----
    > var xsl2 = new ActiveXObject("Microsoft.XMLDOM")
    > xsl.async = false
    > xsl.load("cdcatalog2.xsl")
    >
    > // Transform ---- 2 -----
    > document.write(xml.transformNode(xsl2))
    >
    > </script>
    >
    > </body>
    > </html>


    Well new ActiveXObject('Microsoft.XMLDOM') works with IE/Win if ActiveX
    is enabled. And even with IE whether the object created with new
    ActiveXObject('Microsoft.XMLDOM') supports XSLT 1.0 depends on the IE
    version (IE 6 and later will do, IE 5 or IE 5.5 require MSXML 3 to be
    installed in replace mode). Other browsers like Mozilla or Opera do not
    support script creation of ActiveX objects.

    transformNode gives a string, document.write expects a string thus if
    you need/want to do two document.write calls with a string result of
    transformNode passed in then you can do. Those stylesheets hopefully
    generate HTML snippets that belong inside the body of an HTML document.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 20, 2006
    #2
    1. Advertising

  3. Guest

    Martin Honnen wrote:
    >
    >
    > wrote:
    >
    >
    >> I am completely new to this, but I was wondering if I can apply 2
    >> xslt's subsequently to an xml, via the (client side) scripting method:

    >
    >
    >
    >> <html>
    >> <body>
    >>
    >> <script type="text/javascript">
    >>
    >> // Load XML
    >> var xml = new ActiveXObject("Microsoft.XMLDOM")
    >> xml.async = false
    >> xml.load("cdcatalog.xml")
    >>
    >> // Load XSL ---- 1 ----
    >> var xsl = new ActiveXObject("Microsoft.XMLDOM")
    >> xsl.async = false
    >> xsl.load("cdcatalog.xsl")
    >>
    >> // Transform ---- 1 ----
    >> document.write(xml.transformNode(xsl))
    >>
    >> // Load XSL ---- 2 -----
    >> var xsl2 = new ActiveXObject("Microsoft.XMLDOM")
    >> xsl.async = false
    >> xsl.load("cdcatalog2.xsl")
    >>
    >> // Transform ---- 2 -----
    >> document.write(xml.transformNode(xsl2))
    >>
    >> </script>
    >>
    >> </body>
    >> </html>

    >
    >
    > Well new ActiveXObject('Microsoft.XMLDOM') works with IE/Win if ActiveX
    > is enabled. And even with IE whether the object created with new
    > ActiveXObject('Microsoft.XMLDOM') supports XSLT 1.0 depends on the IE
    > version (IE 6 and later will do, IE 5 or IE 5.5 require MSXML 3 to be
    > installed in replace mode). Other browsers like Mozilla or Opera do not
    > support script creation of ActiveX objects.

    Is there a type of scripting, one that does not need activex, that works for
    different browsers, at the least ie5 and up and firefox?


    >
    > transformNode gives a string, document.write expects a string thus if
    > you need/want to do two document.write calls with a string result of
    > transformNode passed in then you can do. Those stylesheets hopefully
    > generate HTML snippets that belong inside the body of an HTML document.

    The second one produces html from xml, the first one produces XML from a special
    form of XML.

    I figured out how to merge the 2 xsl into one, but the initial question would
    still be interesting how to do it.

    >
     
    , Sep 20, 2006
    #3
  4. Andy Dingley Guest

    wrote:

    > I am completely new to this, but I was wondering if I can apply 2 xslt's
    > subsequently to an xml, via the (client side) scripting method:
    > [Explictly coded client-side JavaScript transforms via MSXML]


    Yes, this is dead easy. Use two XSL documents and apply them one after
    the other.

    Remember to get the _results_ of the first transform as returned by the
    ..transformNode() method into a variable, rather than writing it back
    directly. Then apply the second transform to _this_, not to your
    original XML source.
     
    Andy Dingley, Sep 21, 2006
    #4
  5. Guest

    Andy Dingley wrote:
    > wrote:
    >
    >
    >>I am completely new to this, but I was wondering if I can apply 2 xslt's
    >>subsequently to an xml, via the (client side) scripting method:
    >>[Explictly coded client-side JavaScript transforms via MSXML]

    >
    >
    > Yes, this is dead easy. Use two XSL documents and apply them one after
    > the other.
    >
    > Remember to get the _results_ of the first transform as returned by the
    > .transformNode() method into a variable, rather than writing it back
    > directly. Then apply the second transform to _this_, not to your
    > original XML source.
    >

    OK, I think I will be able to do this. However, from what I know, the script
    would work only for MS IE and not for Firefox. Actually I think the entire
    approach is useless, if it doesnt work for Firefox.
     
    , Sep 21, 2006
    #5
  6. wrote:


    > Is there a type of scripting, one that does not need activex, that works
    > for different browsers, at the least ie5 and up and firefox?


    Firefox has its own API exposed to script to run XSLT transformations,
    it is documented here:
    <http://developer.mozilla.org/en/docs/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations>
    Opera 9 also supports that API.

    Note that this API integrates well with the W3C DOM API in allowing you
    to get a document fragment node as the transformation result and insert
    it into a target document. Using document.write with that API is not
    really a good idea in my view, as you would first serialize the result
    nodes to a string to have document.write feed it to a parser again to
    create nodes.


    > The second one produces html from xml, the first one produces XML from a
    > special form of XML.


    Then you don't want the approach taken in your script example, instead
    you want to chain the transformations, with MSXML you would use
    transformNodeToObject to transform the original XML with the first
    stylesheet into a second XML document on which you could then do
    transformNode to get the HTML result.

    With Mozilla/Firefox you would use transformToDocument first to
    transform XML to XML and then in my view transformToFragment to
    transform the XML to a HTML document fragment which you could then
    insert with e.g. appendChild as needed.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 21, 2006
    #6
  7. Andy Dingley Guest

    wrote:

    > However, from what I know, the script
    > would work only for MS IE and not for Firefox. Actually I think the entire
    > approach is useless, if it doesnt work for Firefox.


    If you read the right tutorials (recent ones, probably mentioning AJAX)
    then some slightly more sophisticated code will work on either.

    It's not entirely web-portable, but it's usefully so.
     
    Andy Dingley, Sep 21, 2006
    #7
  8. Guest

    Martin Honnen wrote:
    >
    >
    > wrote:
    >
    >
    >> Is there a type of scripting, one that does not need activex, that
    >> works for different browsers, at the least ie5 and up and firefox?

    >
    >
    > Firefox has its own API exposed to script to run XSLT transformations,
    > it is documented here:
    > <http://developer.mozilla.org/en/docs/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations>
    >

    This page www.w3schools.com/xsl/xsl_client.asp shows the example for IE. Sorry,
    I am so new to all this stuff. Do you have a link to a page that shows me how to
    code this for Firefox?


    > Opera 9 also supports that API.
    >
    > Note that this API integrates well with the W3C DOM API in allowing you
    > to get a document fragment node as the transformation result and insert
    > it into a target document. Using document.write with that API is not
    > really a good idea in my view, as you would first serialize the result
    > nodes to a string to have document.write feed it to a parser again to
    > create nodes.
    >
    >
    >> The second one produces html from xml, the first one produces XML from
    >> a special form of XML.

    >
    >
    > Then you don't want the approach taken in your script example, instead
    > you want to chain the transformations, with MSXML you would use
    > transformNodeToObject to transform the original XML with the first
    > stylesheet into a second XML document on which you could then do
    > transformNode to get the HTML result.
    >


    I think I can do this, thanks. I also was able to merge the 2 XSLs into a single
    one. Dont know what would be preferable.

    > With Mozilla/Firefox you would use transformToDocument first to
    > transform XML to XML and then in my view transformToFragment to
    > transform the XML to a HTML document fragment which you could then
    > insert with e.g. appendChild as needed.
    >
    >
     
    , Sep 21, 2006
    #8
  9. > I think I can do this, thanks. I also was able to merge the 2 XSLs into
    > a single one. Dont know what would be preferable.


    A single combined transformation will generally be more efficient than
    two in succession.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
     
    Joseph Kesselman, Sep 21, 2006
    #9
    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. Replies:
    3
    Views:
    382
    Raymond DeCampo
    Aug 27, 2005
  2. tuka
    Replies:
    3
    Views:
    710
    Joe Kesselman
    Jun 22, 2007
  3. philjhanna
    Replies:
    0
    Views:
    118
    philjhanna
    Aug 5, 2005
  4. Stephen

    ajax, xml and applying xslt

    Stephen, Oct 13, 2005, in forum: Javascript
    Replies:
    4
    Views:
    135
    Stephen
    Oct 13, 2005
  5. jackpod
    Replies:
    1
    Views:
    170
    Martin Honnen
    Nov 5, 2009
Loading...

Share This Page