English 中文(简体)
如何分析 xml, 并用 javarint 提取 xslt 元素 。
原标题:How to parse an xml and extract the xslt element from it in javascript

I am sending an xml response from my servlet to my html page. I receive it via the xmlresponse object of the xmlhttprequest object. My xml document contains a xsl:stylesheet as an element I want to extract this element and execute that xslt code in my 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>


Once I get this xml string from the ajax response, I want to convert it into xml, extract the xslt part and execute it and show the output in a text area.
EDIT2
What is 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 output(xmltext1) for the xslt transformation is -

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

Testing : 1212
</transformiix:result>

但如果您在 xslt 代码中看到, 输出方法设置为“ 文本 ” 。 那么为什么 xml 标签包含在输出中?

Answer
This gives the exlpanation for edit2. Thanks for the answers:)

最佳回答

我只能用最新的染色体来测试它:

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);
问题回答

这是我从 xmlHttpRequest (在您的情况下, ns=xsl, 未测试) 获得命名空间 xml 时所用的方法 :

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

请注意,如果服务器返回完善的 xml, 您只能使用响应XML。 否则, 您需要剖析 xhr. refuseText 。

I m using this ATM, works great! Its a parser written on jQuery

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





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签