【问题标题】:How to avoid multiple nodes with the same tag in xml with namespace using jQuery 'find'?如何使用jQuery'find'避免在带有命名空间的xml中具有相同标签的多个节点?
【发布时间】:2010-11-29 12:42:12
【问题描述】:

我有以下 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>

当我在“每个”函数中执行 $(this).find('[nodeName=a:ID]') 时,我得到 2 个 ID,一个来自 Project,另一个来自 Document。

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

问题是我怎样才能只获得项目 ID,而不是文档 ID 和其他可能出现的 ID?

【问题讨论】:

    标签: javascript jquery xml wcf xml-namespaces


    【解决方案1】:

    在里面也使用.children()而不是.find(),所以它只寻找立即子,像这样:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 2022-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多