I'm trying to run a SOAP response from a web service through an xslt translation and place it in a web page via an Ajax call. The web service and javascript all works fine. I am having a problem accessing specific nodes inside the xslt file.

Specifically: when I run the SOAP response through <xsl:for-each select="//*"> I can get all the correct node names in the response and their associated values. However when I try to access via <xsl:for-each select="//getStringResult"> I get absolutely nothing.

Here's an example of the SOAP response

<?xml version="1.0" encoding="utf-i"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<getStringResponse xmlns="http://ramabahama.net/webservices">
<getStringResult>
<Salutation>String value as expected</Salutation>
<Id>Integer as expected</Id>
</getStringResult>
</getStringResponse>
</soap:Body>
</soap:Envelope>

Here's the xslt file

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html" indent="yes" media-type="text/html" />
	<xsl:template match="/">
		<table border="2px" cellspacing="10px">
			<xsl:for-each select="//*">
				<tr>
					<td style="border: solid thin green; padding: 10px; text-align: left; vertical-align: middle;">
						<xsl:value-of select="current()" />
					</td>
					<td style="border: solid thin green; padding: 10px; text-align: left; vertical-align: middle;">
						<xsl:value-of select="name()" />
					</td>
				</tr>
			</xsl:for-each>
		</table>
	</xsl:template>
</xsl:stylesheet>

Any idea why I'm having problems with my XPath usage? Thanks.

Okay, maybe if I show the supporting JavaScript:

function callService() {
  var _IE = (new RegExp('internet explorer', 'gi')).test(navigator.appName);
  if(_IE) {
    xmlReq = new ActiveXObject('Microsoft.XMLHTTP');
    xmlObj = new ActiveXObject('MSXML2.FreeThreadedDOMDocument.3.0');
    xslObj = new ActiveXObject('MSXML2.FreeThreadedDOMDocument.3.0');
  } else {
    xmlReq =new XMLHttpRequest();
    xslObjProcessor = new XSLTProcessor();
  }
  var soapReq = '<?xml version="1.0" encoding="utf-8"?>' +
  '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
  '	<soap:Body>' +
  '		<getString xmlns="http://ramabahama.net/webservices">' +
  '			<name_in>Bubbly Boobbah</name_in>' +
  '		</getString>' +
  '	</soap:Body>' +
  '</soap:Envelope>';
  xmlReq.open('POST', 'http://127.0.0.1/(S(<%=Session.SessionID%>))/webservices/test1/test1.cs.asmx', false);
  xmlReq.setRequestHeader('SOAPAction', 'http://ramabahama.net/webservices/getString');
  xmlReq.setRequestHeader('Content-Type', 'text/xml');
  xmlReq.send(soapReq);
  if(xmlReq.status == '200') {
    document.getElementById('ServiceDisplay').innerHTML = '';
    var xmlResponseXML = xmlReq.responseXML;
    var xmlResponseText = xmlReq.responseText;
    xmlReq.open('GET', 'http://127.0.0.1/ARIS/products/test1.xslt', false);
    xmlReq.send(null);
    if(xmlReq.status == '200') {
    var xmlHttp;
    var xmlReq;
    var xslObj;
    var xslObjProcessor;
    var xmlObj;
    if(_IE) {
      xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
      xmlObj = new ActiveXObject('MSXML2.FreeThreadedDOMDocument.3.0');
      xslObj = new ActiveXObject('MSXML2.FreeThreadedDOMDocument.3.0');
      xmlObj.async = false;
      xslObj.async = false;
      var xslTemplateObj = new ActiveXObject('MSXML2.XSLTemplate.3.0');
      xslObj.loadXML(xmlReq.responseText);
      xslTemplateObj.stylesheet = xslObj;
      xslObjProcessor = xslTemplateObj.createProcessor();
      xslObjProcessor.input = xmlResponseXML;
      xslObjProcessor.transform();
     document.getElementById('ServiceDisplay').innerHTML = xslObjProcessor.output;
    } else {
      var objParser = new DOMParser();
      xslObjProcessor = new XSLTProcessor();
      xmlObj = xmlResponseXML;
      xslObj = xmlReq.responseXML;
      xslObjProcessor.importStylesheet(xslObj);
     document.getElementById('ServiceDisplay').appendChild(xslObjProcessor.transformToFragment(xmlObj, document));
    }
    alert(xmlResponseText);
    alert(xmlReq.responseText);
    alert(document.getElementById('ServiceDisplay').innerHTML);
  } else {
    alert('Error: ' + xmlReq.status);
  }
 } else {
    document.getElementById('ServiceDisplay').innerHTML = '<span style="color: red; font-weight: bold;">There wuz an error.</span>';
 }
}

The only function that is called is 'callService'. That function makes the web service call and then retrieves the xslt file. Both of these are done via Ajax.

I'm sure I'm missing something pretty damned basic .. but I could really use some help.

Thanks.

Found the answer. I was not defining the namespace for 'soap:' and thus the xslt translator couldn't find 'soap:Envelope' or 'soap:Body' tags. On top of that I had to set up a namespace for the 'http://ramabahama.net/webservices' name space. I used 'roas' and once I included xmlns:roas=http://ramabahama.net/webservices' to the xsl:stylesheet tag I could reference the returned values as 'roas:[fill in tag name here]'.

This article has been dead for over six months. Start a new discussion instead.