2010-11-29 62 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

相關問題