How to parse xml and extract xslt element from it in javascript

I am sending an xml response from my servlet to my html page. I get it through the xmlresponse object of the xmlhttprequest object. My XML document contains the xsl: stylesheet as the element I want to extract from this element and execute this xslt code in a java script.
Is it possible? This is my xml code:

<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
Version="2.0" IssueInstant="2012-05-22T13:40:52:390" ProtocolBinding="urn:oasis:na
mes:tc:SAML:2.0:bindings:HTTP-POST" AssertionConsumerServiceURL="localhos
t:8080/consumer.jsp">
<UserID>
   xyz
</UserID>
<testing>
   text
</testing>
<saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
   http://localhost:8080/saml/SProvider.jsp
</saml:Issuer>
<xslt>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="text" />
<xsl:template match="/">
UserID : <xsl:copy-of select="//UserID"/>
testing : <xsl:copy-of select="//testing"/>
</xsl:template>
</xsl:stylesheet>
</xslt>
</samlp:AuthnRequest>


As soon as I get this xml string from the ajax response, I want to convert it to xml, extract the xslt part and execute it and show the output in the text area.
EDIT2
What's wrong with this code:

    var xmlDoc=xmlhttp.responseXML;
     //var xmltext=new XMLSerializer().serializeToString(xmlDoc);
     var xsltProcessor = new XSLTProcessor();
var element=xmlDoc.getElementsByTagNameNS("http://www.w3.org/1999/XSL/Transform","stylesheet");//
 //document.forms['my']['signature'].value=xmltext;
var stylesheet=xsltProcessor.importStylesheet(element[0]);
var result=xsltProcessor.transformToDocument(xmlDoc);
 var xmltext1=new XMLSerializer().serializeToString(result);
document.forms['my']['signature2'].value = xmltext1;


The result (xmltext1) for xslt conversion is

<transformiix:result xmlns:transformiix="http://www.mozilla.org/TransforMiix">
UserID : 1212

Testing : 1212
</transformiix:result>

But if you see xslt in the code, the output method is set to "text". then why are xml tags included in the output?


exlpanation edit2. :)

+3
3

, , :

var getNsResolver = function (element) {
  var ns = {
    samlp: 'urn:oasis:names:tc:SAML:2.0:protocol',
    xsl: 'http://www.w3.org/1999/XSL/Transform'
  };

  return function (prefix) {
    return ns[prefix] || null;
  };
};

var handleResponse = function (xhr) {
  var
    doc = xhr.responseXML,
    xsl = doc.evaluate('/samlp:AuthnRequest/xslt/xsl:stylesheet', doc, getNsResolver(doc.documentElement), XPathResult.ANY_TYPE, null).iterateNext(),
    processor = new XSLTProcessor(),
    result;

  processor.importStylesheet(xsl);
  result = processor.transformToFragment(doc, document);

  document.getElementById('foo').value = result.textContent;
};

window.addEventListener('load', function () {
  var request = new XMLHttpRequest();
  request.addEventListener('load', function (evt) {
    handleResponse(request);
  }, false);

  request.open('GET', 'sample.xml', true); // sample.xml contains the xml from the question
  request.send();
}, false);
+1

, xmlHttpRequest xmlHttpRequest ( ns = xsl, ):

var xml=xhr.responseXML;
var elts = (xml.getElementsByTagNameNS)?xml.getElementsByTagNameNS("ns","tag"):xml.getElementsByTagName("ns\:tag");

, responseXML , xml. , xhr.responseText.

+1

I use this ATM, it works great! Its a parser written in jQuery

http://www.jongma.org/webtools/jquery/xslt/

0
source

All Articles