Solved

XSLT transformation

Posted on 2010-11-25
2
438 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
ID: 34215460
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
ID: 34215854
thank you! you are a life saver!!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to make a moving character 3 57
PHP Form Calculate Total Price 10 42
Jquery 10 20
How can I make a javascript to show drop down 4 13
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

770 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