CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2004
    Posts
    7

    JS and XSL: change params

    Hi,

    I know this is really js related but I figured it is an xsl problem so I'd post it here and see how it went.

    I need to change the value of two <xsl:param>s. I have tried using selectSingleNode, and it worked for a while, then I went and changed the xsl and now it doesnt work.

    So I have to pass params to an xsl stylsheet using the following js

    <script language="javascript">
    var HTMLresults;
    var xml;
    var xsl;
    var sortParam;
    var dtParam;
    var column;
    var dt;
    var processor;

    function loadXML() {
    xml = new ActiveXObject("Microsoft.XMLDOM");
    xml.async = false;
    xml.load("xmlsorttest.xml");

    xsl = new ActiveXObject("MSXML2.FreeThreadedDomDocument.3.0");
    xsl.async = false;
    xsl.load("test12.xsl");

    var template = new ActiveXObject("MSXML2.XSLTemplate")
    >> template.stylesheet = xsl;
    processor = template.createProcessor();
    processor.input = xml;


    if(xsl.parseError.errorCode != 0) {
    showError();
    }//if

    if(document.forms[0].SortVal.value != 0) {
    column = document.forms[0].SortVal.value;
    if(column == "EnterBy"){
    dt = "text";
    }else{
    dt = "number";
    }//if
    processor.addParameter("sortby", column);
    processor.addParameter("datatype", dt);
    }//if
    //transform
    doTransform();
    }//loadxml

    function doTransform(){
    if (getReadyState()){
    //resulting = xml.transformNode(xsl);
    processor.transform();
    resulting = processor.output;
    document.all.item("HTMLresults").innerHTML = resulting;
    }//if
    }//doTransform

    function getReadyState(){
    if(xml.readyState == 4){
    return true;
    }//if
    setTimeout("getReadyState()", 100);
    }//getReadyState



    ShowError() is a js that prints out the error.

    SortVal is a hidden field in the HTML that contains the js.

    loadXML() is called in the <BODY> tag onload.

    The params exist in the stylesheet:

    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:param name="sortby">DateVal</xsl:param>
    <xsl:param name="datatype">number</xsl:param>
    ........
    ........
    ........
    My problem is that I get a run time error "The stylesheet does not contain a document element. Stylesheet is empty or not well formed" when I open the html page.

    The error occurs where I have indicated ">>" in the above js.

    I have checked the style in XMLSpy and it says it is well formed. Additonally I can open the xml file and the style works fine.

    So the style must be empty due to a coding error. Any ideas as to what is wrong with my code?

  2. #2
    Join Date
    May 2003
    Location
    Denmark
    Posts
    1,315
    I would suspect that the problem lies with MSXML2, it's pretty old not exactly standard compliant. I'am not an MSXML kind of guy, but I would think that, you might have more luck with MSXML3 or 4.
    The biggest problem encountered while trying to design a system that was completely foolproof,
    was, that people tended to underestimate the ingenuity of complete fools.
    Douglas Adams

  3. #3
    Join Date
    May 2004
    Posts
    7

    Talking Thanks

    I changed it to this

    <script language="javascript">
    var HTMLresults;
    var xml;
    var xsl;
    var sortParam;
    var dtParam;
    var column;
    var dt;

    function loadXML() {
    xml = new ActiveXObject("Microsoft.XMLDOM");
    xsl = new ActiveXObject("Microsoft.XMLDOM");
    xml.async = false;
    xml.load("xml.xml");

    xsl.async = false;
    xsl.load("xsl.xsl");


    if(xsl.parseError.errorCode != 0) {
    showError();
    }//if

    if(document.forms[0].SortVal.value != 0) {
    column = document.forms[0].SortVal.value;
    if(column == "EnterBy"){
    dt = 'text';
    }else{
    dt = 'number';
    }//if
    sortParam = xsl.selectSingleNode("*//xsl:param[@name='sortby']");
    sortParam.text = column;
    //alert("SORT: " + sortParam.text);
    dtParam = xsl.selectSingleNode("*//xsl:param[@name='datatype']");
    dtParam.text = dt;
    //alert("DT: " + dtParam.text);
    }//if
    doTransform();
    }//loadxml

    function doTransform(){
    if (getReadyState()){
    resulting = xml.transformNode(xsl);
    alert("Transforming...");
    document.all.item("HTMLresults").innerHTML = resulting;
    }//if
    }//doTransform


    function getReadyState(){
    if(xml.readyState == 4){
    return true;
    }//if
    setTimeout("getReadyState()", 100);
    }//getReadyState

    function showError() {
    var strError = new String;
    var err = xml.parseError;
    strError = 'Error!\n' +
    'file url: '+err.url +' \n'+
    'line no.:'+err.line +'\n'+
    'char: '+ err.linepos + '\n' +
    'source: '+err.srcText+'\n'+
    'code: '+err.errorCode+'\n'+
    'description: '+err.reason+'\n';
    document.all.item("HTMLresults").innerHTML = strError;
    }
    </script>

    So I use selectSingleNode(xpath) to change the params. Works great.

    Thanks for the advice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured