我有以下格式的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中存在。我想我在這裏錯過了一些關於語言如何工作的基礎知識。
你在contentType賦值的末尾有兩個雙引號:+'「」]' –
好的。不幸的是,結果相同。 – Anthony
原來ron tornambe是對的。這是我的雙引號。我從來沒有讓他們正確匹配,這似乎是我所有問題的根源。這固定它: var contentType = $(xml).find('ContentType [ContentTypeName =''+ $(collectionGroup).attr(「ContentTypeName」)+'「]') 謝謝! – Anthony