支持命名空间中的XML AJAX库空间、XML、AJAX

2023-09-11 01:14:21 作者:违心人

我要寻找一个支持名称空间的XML的AJAX JavaScript库。

I am looking for an AJAX JavaScript library that supports namespaced xml.

我在网络上阅读几十个职位(包括计算器),但没有找到一个很好的答案。有许多AJAX的例子,但他们只要命名空间来发挥作用的突破(这是例如用jQuery选择器的情况下)。

I read dozens of posts on the web (including on stackoverflow) but didn't find a good answer. There are many AJAX examples, but they break as soon as namespaces come into play (it is the case for example with jQuery selectors).

推荐答案

我不知道到底是什么,你在呼唤一个AJAX JavaScript库 - 进行HTTP请求的是一个问题的不同的的访问节点在一个文档树。

I do not know exactly what you are calling an "AJAX JavaScript library" – making HTTP requests is a problem different from accessing nodes in a document tree.

如果你理解了库资源的集合,用于开发软件(维基百科),那么,作为 JSX 的图书馆,我已经写了,而compatible¹包装的 XMLHtt prequest 部分,namespace-了解 DOM Level 3的XPath的:的 http.js xpath.js

If you understand a library as "a collection of resources used to develop software" (Wikipedia), then, as part of the JSX library, I have written rather compatible¹ wrappers for XMLHttpRequest and namespace-aware DOM Level 3 XPath: http.js and xpath.js.

http.js 支持同步和异步处理以同样的方式,甚至可以访问本地文件系统(如果请求被批准)。由于JSX资格作为一个库,然后,你可以使用 http.js xpath.js 或者单独,与国外code到补充任何一个,或在一起。

http.js supports synchronous and asynchronous processing in the same way, and can even access the local filesystem (if permission is granted). Because JSX qualifies as a library then, you can use http.js and xpath.js either separately, with foreign code to complement either one, or together.

您会一起使用它们,例如,如下所述。假设你与资源名称的XML文档的test.xml 喜欢

You would use them together, for example, as follows. Suppose you had an XML document with resource name test.xml like

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body>
    <res:Response
        xmlns:res="http://domain.example/Response">
      <res:OUTPUT>
        <res:UNDO_COUNT>1.0</res:UNDO_COUNT>
        <res:MSG>Undo complete (No more to undo)</res:MSG>
      </res:OUTPUT>
    </res:Response>
    <res:OUTPUT
        xmlns:res="http://domain.example/Response">
      foo
    </res:OUTPUT>
  </soap:Body>
</soap:Envelope>

和你想提取 1.0 RES:UNDO_COUNT 元素,你可以写:

and you wanted to extract the 1.0 from its res:UNDO_COUNT element, you could write:

<!-- 1. Include prerequisites and dependencies using Resource Builder (recommended) -->
<script type="text/javascript" src="builder.php?src=object,string,http,xpath"></script>

<script type="text/javascript">
  /*
   * 2. Construct the HTTP request wrapper; 
   *    the default is a GET request with asynchronous handling
   */
  var request = new jsx.net.http.Request("test.xml");

  /* 3. Prepare processing of the HTTP response */
  request.setSuccessListener(function (response) {
    /* 5. Get the reference to the XMLDocument object */
    var doc = response.responseXML;

    /* 6. Create the namespace resolver that fits your query best */
    var nsResolver = jsx.xpath.createFullNSResolver(null, doc);

    /* 7. Make the XPath query */
    var nodes = jsx.xpath.evaluate("//res:UNDO_COUNT/text()", doc, nsResolver);

    /*
     * 8. Process the result.  jsx.xpath.evaluate() returns a reference
     * to an Array instance if you do not specify the result type.
     */

    /* "1.0" */
    console.log(nodes[0].data);
  });

  /* 4. Make the HTTP request */
  request.send();
</script>

另请参见:Parsing使用Java脚本 URL XML / RSS

See also: Parsing XML / RSS from URL using Java Script

¹JSX的组合: object.js http.js xpath.js 已通过测试阳性Gecko-,WebCore-,MSHTML型和Opera浏览器。然而,JSX是的大多实验的code现在。

¹ A combination of JSX:object.js, http.js, and xpath.js has been tested positive in Gecko-, WebCore-, MSHTML-based and Opera browsers. However, JSX is mostly experimental code for now.

测试用例,看剧本控制台

建设性的反馈意见是值得欢迎的。另外,JSX是免费软件。 (你不能做一个结算还没有,但我的工作就可以了。)

Constructive feedback is welcome. In addition, JSX is free software. (You cannot do a checkout yet, but I am working on it.)