2013-12-20 139 views
0

我有以下格式的XML文檔:基於另一種元素選擇元素屬性

<CollectionMappingData> 
    <ContentTypes> 
    <ContentType ContentTypeName="Content Type Name" SomeAttr="ValueINeed" /> 
    </ContentTypes> 
    <CollectionGroup CollectionName="collection_name" ContentTypeName="Content Type Name"/> 
    <CollectionGroup CollectionName="collection_name2" ContentTypeName="Content Type Name 2"/> 
    <CollectionGroup CollectionName="collection_name3" ContentTypeName="Content Type Name 3"/> 
</CollectionMappingData> 

給定一個集合的名字,我是一個<CollectionGroup />內尋找一個CollectionName,然後我試圖找到一個<ContentType />根據CollectionName。這裏是我的JS至今:

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#inputForm").submit(function(event){ 
      event.preventDefault(); 
      var collectionName = $('#collectionName').val(); // User supplied collection name 

      $.ajax({ 
       type: "GET", 
       url: "ContentTypeMapData.xml", 
       dataType: "xml", 
       success: function (xml) { 
        findCollectionGroup(xml, collectionName); 
       } 
      }); 

      return false; 
     }); 
    }); 

    function findCollectionGroup(xml, collectionName) { 
     var output = ''; 
     var collectionGroup = $(xml).find('CollectionGroup[CollectionName=' + collectionName + ']'); 
     var contentType = $(xml).find('ContentType[ContentTypeName=' + $(collectionGroup).attr('ContentTypeName') + ']'); 

     output += contentType.attr("SomeAttr"); 

     $("#xmlDump").append(output); 
    } 

它似乎並沒有被找到<ContentType />如我所料,即使它在XML中存在。我想我在這裏錯過了一些關於語言如何工作的基礎知識。

+0

你在contentType賦值的末尾有兩個雙引號:+'「」]' –

+0

好的。不幸的是,結果相同。 – Anthony

+0

原來ron tornambe是對的。這是我的雙引號。我從來沒有讓他們正確匹配,這似乎是我所有問題的根源。這固定它: var contentType = $(xml).find('ContentType [ContentTypeName =''+ $(collectionGroup).attr(「ContentTypeName」)+'「]') 謝謝! – Anthony

回答

0

它結果ron tornambe是正確的。這是我的雙引號。我從來沒有讓他們正確匹配,這似乎是我所有問題的根源。這固定它:

var contentType = $(xml).find('ContentType[ContentTypeName="' + $(collectionGroup).attr("ContentTypeName") + '"]') 

謝謝!

0

我不是語法100%肯定,我不能在此刻測試,但你可能在尋找這樣的事情:

findCollectionGroup功能:

function findCollectionGroup(xml, collectionName) { 
     var output = ''; 
     var xmlDoc = $.parseXML(xml); 
     $xml = $(xmlDoc); 
     $cg = $xml.find("CollectionGroup").attr("CollectionName",collectionName); 
     $ct = $xml.find("ContentType").attr("ContentTypeName").each(function() { 
      if($(this) == $cg) 
       $("#xmlDump").append($(this)); 
     }) 
} 
+0

當我回家並進行相應編輯時,我會進行測試。 –

相關問題