使用 JQuery 提取名为“Title”的 xml 元素的文本值在 IE 8 中

发布于 2024-10-04 23:42:41 字数 3720 浏览 0 评论 0原文

我编写了一个基于 Solr 的搜索服务来索引称为剪辑的对象数据库。搜索服务返回使用 OpenSearch Atom 扩展格式格式化的搜索结果。剪辑具有各种属性,ClipID 和 Title 是与我的问题相关的两个属性。

我使用 jquery 编写了一个非常简单的 JavaScript 程序,该程序在后台异步调用搜索服务,并使用 ClipId 和 Title 值填充表。该程序在 Chrome、Safari 和 FF 上运行良好。但是,在 IE 上,它根本无法解析 Title 属性的值。就好像“Title”是一个保留的 XML 标记名称,而 IE 上的 jQuery 无法找到它。

以下是我的 JavaScript 程序的摘录:

   // Ajax call to the search service over HTTP.
   var doSearch = function(){
     var query = "Title:" + $("#searchQuery").val() + "*";

     $.ajax({
      url : "/quantel/search/select" ,
      data:{q:query},
      error:function(request,status,error){
       alert(request + "," + status + "," + error);
      },
      dataType: "text/xml",
      success:function(data,status,request){
       // Clear the data table.
       $("#searchResults").dataTable().fnClearTable();
       // Search for all clip entries in the XML document.
       $(data).find("entry").children("content").each(function(index,element) {

        var clipID= $(element).children("ClipID").text();
        var title = $(element).children("Title").text();

        // Add the clip id and title to the table.
        $("#searchResults").dataTable().fnAddData([clipID,title]);
       });    
      }
     });
   };

这是我尝试解析的搜索结果示例。正如您所看到的,内容标签包含标题标签,但 IE 根本找不到它。

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
 <title>ProjectFolders Search</title>
 <link href="http://localhost:8182/quantel/search/select?q=guillaume*&amp;rows=100&amp;" />
 <link rel="self" href="http://localhost:8182/quantel/search/select?q=guillaume*&amp;rows=100&amp;" />
 <link rel="first" href="http://localhost:8182/quantel/search/select?q=guillaume*&amp;rows=100&amp;start=0" />
 <link rel="last" href="http://localhost:8182/quantel/search/select?q=guillaume*&amp;rows=100&amp;start=0" />
 <link rel="previous" href="http://localhost:8182/quantel/search/select?q=guillaume*&amp;rows=100&amp;start=0" />
 <link rel="next" href="http://localhost:8182/quantel/search/select?q=guillaume*&amp;rows=100&amp;start=0" />
 <updated>2010-11-29T14:45:53.796Z</updated>
 <author>
  <name>Quantel</name>
 </author>
 <id>urn:uuid:cd9d3362-2159-4c27-a99e-9691dd4ff707</id>
 <opensearch:totalResults>6</opensearch:totalResults>
 <opensearch:startIndex>0</opensearch:startIndex>
 <opensearch:itemsPerPage>100</opensearch:itemsPerPage>
 <entry>
  <title type="html">Guillaume_clip</title>
  <updated>2010-10-25T11:10:17.000+01:00</updated>
  <id>urn:clipid:389685</id>
  <link href="http://localhost:8182/quantel/search/select?q=ClipID:389685" />
  <content type="text/xml">
   <PlaceHolder>0</PlaceHolder>
   <HasEditData>0</HasEditData>
   <id>389685</id>
   <ClipID>389685</ClipID>
   <Created>2010-10-25T11:10:17.000+01:00</Created>
   <NumVidTracks>0</NumVidTracks>
   <CloneZone>119</CloneZone>
   <MosActive>0</MosActive>
   <Template>0</Template>
   <Completed>2010-10-25T11:10:18.000+01:00</Completed>
   <Frames>0</Frames>
   <Title>Guillaume_clip</Title>
   <UnEdited>1</UnEdited>
   <ClipGUID>7e5aef9c7da44bacbfb49500710138cf</ClipGUID>
   <CloneID>389685</CloneID>
   <NumAudTracks>0</NumAudTracks>
  </content>
 </entry>
</feed>

I've written a search service based around Solr to index a database of objects called clips. The search service returns search results formatted using the OpenSearch atom extensions formats. Clips have various properties, ClipID and Title are the two properties relevant to my question.

I have written a very simple JavaScript program using jquery which invokes the search service asynchronously in the background and populates a table with the ClipId and Title values. The program works fine on Chrome, Safari and FF. However, on IE, it simply fails to parse the value of the Title attribute. It is as if "Title" is a reserved XML tag name and jQuery on IE simple can't find it.

Here is the extract from my JavaScript program:

   // Ajax call to the search service over HTTP.
   var doSearch = function(){
     var query = "Title:" + $("#searchQuery").val() + "*";

     $.ajax({
      url : "/quantel/search/select" ,
      data:{q:query},
      error:function(request,status,error){
       alert(request + "," + status + "," + error);
      },
      dataType: "text/xml",
      success:function(data,status,request){
       // Clear the data table.
       $("#searchResults").dataTable().fnClearTable();
       // Search for all clip entries in the XML document.
       $(data).find("entry").children("content").each(function(index,element) {

        var clipID= $(element).children("ClipID").text();
        var title = $(element).children("Title").text();

        // Add the clip id and title to the table.
        $("#searchResults").dataTable().fnAddData([clipID,title]);
       });    
      }
     });
   };

And here is a sample of search results that I am trying to parse. As you can see the Content tag contains a Title tag, yet, IE simply can't find it.

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
 <title>ProjectFolders Search</title>
 <link href="http://localhost:8182/quantel/search/select?q=guillaume*&rows=100&" />
 <link rel="self" href="http://localhost:8182/quantel/search/select?q=guillaume*&rows=100&" />
 <link rel="first" href="http://localhost:8182/quantel/search/select?q=guillaume*&rows=100&start=0" />
 <link rel="last" href="http://localhost:8182/quantel/search/select?q=guillaume*&rows=100&start=0" />
 <link rel="previous" href="http://localhost:8182/quantel/search/select?q=guillaume*&rows=100&start=0" />
 <link rel="next" href="http://localhost:8182/quantel/search/select?q=guillaume*&rows=100&start=0" />
 <updated>2010-11-29T14:45:53.796Z</updated>
 <author>
  <name>Quantel</name>
 </author>
 <id>urn:uuid:cd9d3362-2159-4c27-a99e-9691dd4ff707</id>
 <opensearch:totalResults>6</opensearch:totalResults>
 <opensearch:startIndex>0</opensearch:startIndex>
 <opensearch:itemsPerPage>100</opensearch:itemsPerPage>
 <entry>
  <title type="html">Guillaume_clip</title>
  <updated>2010-10-25T11:10:17.000+01:00</updated>
  <id>urn:clipid:389685</id>
  <link href="http://localhost:8182/quantel/search/select?q=ClipID:389685" />
  <content type="text/xml">
   <PlaceHolder>0</PlaceHolder>
   <HasEditData>0</HasEditData>
   <id>389685</id>
   <ClipID>389685</ClipID>
   <Created>2010-10-25T11:10:17.000+01:00</Created>
   <NumVidTracks>0</NumVidTracks>
   <CloneZone>119</CloneZone>
   <MosActive>0</MosActive>
   <Template>0</Template>
   <Completed>2010-10-25T11:10:18.000+01:00</Completed>
   <Frames>0</Frames>
   <Title>Guillaume_clip</Title>
   <UnEdited>1</UnEdited>
   <ClipGUID>7e5aef9c7da44bacbfb49500710138cf</ClipGUID>
   <CloneID>389685</CloneID>
   <NumAudTracks>0</NumAudTracks>
  </content>
 </entry>
</feed>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温暖的光 2024-10-11 23:42:41

$.ajax 调用的 dataType 选项具有无效值。它应该是 xml。请参阅 http://api.jquery.com/jQuery.ajax

在您的情况下数据无效type IE 将内容解析为 HTML,因此 title 元素被移动到 head 元素中。

此外,响应的 MIME 类型应为 text/xml。如果您需要保留其他 MIME 类型,您可以按照此处描述的方式解析 XML 响应: http://docs .jquery.com/Specifying_the_Data_Type_for_AJAX_Requests

dataType option of the $.ajax call has an invalid value. It should be xml. See http://api.jquery.com/jQuery.ajax

In your case with an invalid data type IE parses the content as HTML, so the title element is moved into the head element.

Also MIME-type of the response should be text/xml. If you need to keep some other MIME type, you can parse an XML response in the way described here: http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests

ゃ懵逼小萝莉 2024-10-11 23:42:41

标题应该位于不同的命名空间中。您可以尝试

$(elment).find('atom\:Title').text();

在 iPad 上,现在无法尝试。

The title should be in different namespace. You can try

$(elment).find('atom\:Title').text();

On iPad , can't try it now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文