2010-11-29 60 views
2

我有以下XML如何使用jQuery'find'避免具有名称空间的xml中具有相同标记的多个节点?

<ProjectResponse xmlns="Services.Messages" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">  
    <Projects xmlns:a="Services.DTO"> 
    <a:Project> 
     <a:ID>113</a:ID> 
     <a:Name>Test project</a:Name> 
     <a:Documents> 
     <a:ProjectDocument> 
      <a:FileName>DS.docx</a:FileName> 
      <a:ID>65</a:ID> 
      <a:ProjectID>113</a:ProjectID> 
     </a:ProjectDocument>   
     </a:Documents> 
    </a:Project> 
    </Projects> 
</ProjectResponse> 

在i执行$(本).find( '[节点名称= A:ID]')在 '各自' 功能我得到2点的ID,一个从项目和另一个从文件。

$(projectsXml).find('Projects').children().each(function() { 
      var projectId = $(this).find('[nodeName=a:ID]').text(); 

问题是我怎样才能得到项目ID,而不是文档ID和可能发生的其他ID?

回答

2

使用.children()代替.find()内为好,所以只查找立即孩子,像这样:

$(projectsXml).find('Projects').children().each(function() { 
    var projectId = $(this).children('[nodeName=a:ID]').text(); 
}); 
+0

请检查这些问题,并告诉我的错误http://stackoverflow.com/questions/18591761 /如何做 - 比较 - 的价值与 - XML数据 – 2013-09-04 09:08:08

相关问题