2010-05-19 71 views
1

我的XML看起來像這樣:e4x過濾多個孩子?

<?xml version="1.0" encoding="utf-8" ?> 
<projects> 
    <project 
     id="1" 
     thumb="media/images/thumb.jpg" 
     > 
     <categories> 
      <id>1</id> 
      <id>2</id> 
     </categories> 
     <director>Director name</director> 
     <name><![CDATA[IPhone commercial]]></name> 
     <url><![CDATA[http://www.iphone.com]]></url> 
     <description><![CDATA[Description about the project]]></description> 
     <thumb><![CDATA[/upload/images/thumb.jpg]]></thumb> 
    </project> 
</projects> 

但我無法弄清楚如何篩選根據類別ID項目?有人知道該怎麼辦? :)

喜歡的東西:

projects.project.(categories.(id == 3)) 

剛剛返回的所有項目:(

回答

0

這裏有一個更好的方法,而無需使用任何自定義功能:

projects.project.(categories.id.contains(1)) 

contains接受一個值來檢查用於XML或XMLList對象


你可以使用一個額外的功能做處理:

// check if any of the <id> nodes matches any of the given values 
function containing(nodes, values) { 
    for each(var id in nodes) { 
     if(values.indexOf(parseInt(id)) !== -1) return true; 
    } 
    return false; 
} 

projects.project.(containing(categories.id, [1])); // matches the first project 
projects.project.(containing(categories.id, [46])); // matches nothing 
+0

真棒thx :)工程很好,但希望e4x有一些本機支持這種查詢。 – Chris 2010-05-20 03:03:53

+0

不客氣。沒有使用任何功能有更好的方法..更新了答案。 – Anurag 2010-05-20 20:48:43

0

應該projects.project..(id==3)雙點跳過任何節點,雖然這是一個問題,如果你有多個ID。

現在,如果我在actionScript中這樣做,這是我所有的e4x知識來自我會做這projects.project.containing.(id==3).parent()我不知道如果JS支持該父方法或可能它有它自己的。