XSLT transformation

Hi,

Thank you for taking the time to read my question.  Essentially I want to do a transformation.

I have the following functions etc.

function stateChangedSD(http_request)
{
  var fragment = "";
  window.status = "XML data transfer status: " + http_request.readyState;
  if (http_request.readyState==4 || http_request.readyState=="complete")

  {
    if(http_request.overrideMimeType)
    {
      http_request.overrideMimeType('text/xml');
    }
   
      xmlDoc = http_request.responseXML;
    window.status = "Loading XSL processor";
    if (typeof XSLTProcessor != "undefined")
    {
      /* declare global xslt processors, main and detail */
      var myXMLHTTPRequest = new XMLHttpRequest();
      var xsltProcessor = new XSLTProcessor();
      myXMLHTTPRequest.open("get", stepdowntransurl, false);
      myXMLHTTPRequest.send(null);
      xslDoc = myXMLHTTPRequest.responseXML;
      xsltProcessor.importStylesheet(xslDoc);
      var ex = document.getElementById('stepdowngrid1');
      ex.innerHTML = "";

      try
      {
        fragment = xsltProcessor.transformToFragment(http_request.responseXML, document);
      }
      catch (e)
      {
        fragment = "XSL parse error : " + e.text;
      }
      ex.appendChild(fragment);
    }
    else if( window.ActiveXObject && /Win/.test(navigator.userAgent) )
    {
      xslDoc  = new ActiveXObject("Microsoft.XMLDOM");
      window.status = "Loading XSL file";
      xslDoc.async = false;
      xslDoc.load(stepdowntransurl);
      window.status = "Call xmlDoc.transformNode(xslDoc) ...";    

      try
      {
        fragment = xmlDoc.transformNode(xslDoc);
      }
      catch (e)
      {
        fragment = "XSL parse error : " + e.text;
      }    

      var ex = document.getElementById('stepdowngrid1');
      ex.innerHTML = "";
        ex.innerHTML = fragment;
      }
  //  var msg = "Success! Thank you for using Collect!";
    window.status = msg;
    document.style="cursor: default";
  }
}



 function alertContents() {
      if (http_request.readyState == 4) {
                             
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('stepdowngrid1').innerHTML = result;
                  stateChangedsd(http_request);
         } else {
            alert('There was a problem with the request.');
         }
      }
   }


function stepdparser( xml ) //for stepdown data
{
  var result = new ActiveXObject( "Microsoft.XMLDOM" );
  alert ("Line of of parser done");
  result.async = false;
  alert ("line 2 of parser done");
  result.loadXML( xml );
  alert ("line 3 of parser successful");
  return result;
}

function displaystepdowndata() //to display the stepdown data
{
 var request = new XMLHttpRequest();  
  alert ("line one done");
  request.open( "GET", stepdowntransurl, false );
  alert ("line two done");
  request.send( null );
  alert ("line 3 done");
  xslsd = request.responseXML;
  alert ("line 4 done");
 var xmlsd = stepdparser( stepdowndata1 );
 alert ("line 5 done");
  var transform = xmlsd.transformNode( xslsd );
  alert ("running stepdown");
  document.getElementById( "stepdowngrid1" ).innerHTML = transform;
}


I call the displaystepdowndata function to hopfully bring the data onto the screen.  I have a variable stepdowntransurl (this variable = transform/stepdown.xslt) which is basically a link to the xsl file containing the following:

<?xml version='1.0' encoding='ISO-8859-1'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/'>
  <table border='1'>
    <tr>
      <th align='left'>Listed</th>
      <th align='left'>Amount</th>
      <th align='left'>Paid</th>
      <th align='left'>Date</th>
    </tr>
    <xsl:for-each select='query/clients/client'>
    <tr>
      <td><xsl:value-of select='Listeddate' /></td>
      <td><xsl:value-of select='amount' /></td>
      <td><xsl:value-of select='paid' /></td>
      <td><xsl:value-of select='paymentdate' /></td>
    </tr>
    </xsl:for-each>
  </table>
</xsl:template>
</xsl:stylesheet>

and the variable that contains the XML data contains the following:
<?xml version='1.0' encoding='UTF-8'?>
<query><connection><host>LCL</host><database>CDB</database><user>admin</user><searchdate>1091100</searchdate><months>1</months><thismonth>11</thismonth><thisyear>109</thisyear></connection>
<clients>
<client><Listeddate>20101103</Listeddate><amount>0</amount><paid>5200</paid><paymentdate>20101103</paymentdate></client>
</clients>
</query>

COuld someone pls help me work out where I have gone wrong?  The two functions up the top, were ones I took from other projects I have done, and thought I might need.  at the moment it appears to be falling over after the line four alert in the displaystepdowndata function.

many thanks for any assist you can give me.
mavmanauNetwork Engineer/SysadminAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Justin MathewsConnect With a Mentor Commented:
Modify your xsl as:

<?xml version='1.0' encoding='ISO-8859-1'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output
  method="html"
  omit-xml-declaration="yes"
  indent="yes"
  version="1.0"
  encoding="utf-8"
  media-type="string"/>
<xsl:template match='/'>
  <table border='1'>
    <tr>
      <th align='left'>Listed</th>
      <th align='left'>Amount</th>
      <th align='left'>Paid</th>
      <th align='left'>Date</th>
    </tr>
    <xsl:for-each select='query/clients/client'>
    <tr>
      <td><xsl:value-of select='Listeddate' /></td>
      <td><xsl:value-of select='amount' /></td>
      <td><xsl:value-of select='paid' /></td>
      <td><xsl:value-of select='paymentdate' /></td>
    </tr>
    </xsl:for-each>
  </table>
</xsl:template>
</xsl:stylesheet>


Try the JS code below:


<HTML>
<HEAD>
<script>
var stepdowntransurl="test.xsl"; //set this to your xsl url
var stepdowndata1="test.xml";    //set this to your xml url
var request;
if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
{
  request=new XMLHttpRequest();
}
else // code for IE6, IE5
{
  request=new ActiveXObject("Microsoft.XMLHTTP");
}



function stepdparser( xml ) //for stepdown data
{
  if (window.DOMParser) //Mozilla
  {
   result = document.implementation.createDocument("", "", null);
  }
  else // Internet Explorer
  {
   result=new ActiveXObject("Microsoft.XMLDOM");
  }

  result.async=false;
  result.load(xml);
  return result;
}

function displaystepdowndata() //to display the stepdown data
{
  var xmlsd = stepdparser( stepdowndata1 );
  if (window.DOMParser)//Mozilla
  {
   xslsd = document.implementation.createDocument("", "", null);
   xslsd.async=false
   xslsd.load(stepdowntransurl);
   var oProcessor = new XSLTProcessor();
   oProcessor.importStylesheet(xslsd);
   transform = new XMLSerializer().serializeToString(oProcessor.transformToDocument(xmlsd));
  }
  else // Internet Explorer
  {
   xslsd=new ActiveXObject("Microsoft.XMLDOM");
   xslsd.async=false;
   xslsd.load(stepdowntransurl);
   transform = xmlsd.transformNode( xslsd );
  }

  alert ("running stepdown");
  document.getElementById( "stepdowngrid1" ).innerHTML = transform;

}

window.onload = displaystepdowndata;

</script>
</HEAD>
<BODY>
<div id='stepdowngrid1'>
</div>
</BODY>
</HTML>

Open in new window

0
 
mavmanauNetwork Engineer/SysadminAuthor Commented:
thank you! you are a life saver!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.