Solved

XSLT transformation

Posted on 2010-11-25
2
432 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:mavmanau
2 Comments
 
LVL 16

Accepted Solution

by:
jmatix earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:mavmanau
Comment Utility
thank you! you are a life saver!!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now