-1
我有以下的XML數據foreach循環停止的第一個XML元素
<Items>
<Request>
<IsValid>True</IsValid>
<ItemLookupRequest>
<Condition>All</Condition>
<IdType>ISBN</IdType>
<ItemId>0071762345</ItemId>
<ResponseGroup>AlternateVersions</ResponseGroup>
<SearchIndex>All</SearchIndex>
<VariationPage>All</VariationPage>
</ItemLookupRequest>
</Request>
<Item>
<ASIN>0071762345</ASIN>
<AlternateVersions>
<AlternateVersion>
<ASIN>B0058O8V9U</ASIN>
<Title>
Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks) [Paperback] Dave Kerpen Dave Kerpen
</Title>
<Binding>Unknown Binding</Binding>
</AlternateVersion>
<AlternateVersion>
<ASIN>B00511ONPG</ASIN>
<Title>
Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks)
</Title>
<Binding>Kindle Edition</Binding>
</AlternateVersion>
<AlternateVersion>
<ASIN>0071813721</ASIN>
<Title>
Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks)
</Title>
<Binding>Hardcover</Binding>
</AlternateVersion>
</AlternateVersions>
</Item>
<Item>
<ASIN>B00511ONPG</ASIN>
<AlternateVersions>
<AlternateVersion>
<ASIN>0071762345</ASIN>
<Title>
Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (And Other Social Networks)
</Title>
<Binding>Paperback</Binding>
</AlternateVersion>
<AlternateVersion>
<ASIN>B0058O8V9U</ASIN>
<Title>
Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks) [Paperback] Dave Kerpen Dave Kerpen
</Title>
<Binding>Unknown Binding</Binding>
</AlternateVersion>
<AlternateVersion>
<ASIN>0071813721</ASIN>
<Title>
Likeable Social Media: How to Delight Your Customers, Create an Irresistible Brand, and Be Generally Amazing on Facebook (& Other Social Networks)
</Title>
<Binding>Hardcover</Binding>
</AlternateVersion>
</AlternateVersions>
</Item>
</Items>
</ItemLookupResponse>
,我使用的代碼搜索每一個綁定元素,然後做的東西與它這樣的
foreach($xml->Items->Item->AlternateVersions->AlternateVersion->Binding as $BookBinding) { //loop through the xml data to find the correct ASIN for the kindle edition
foreach ($xml->Items->Item->AlternateVersions->AlternateVersion->ASIN as $Kindlestring)
{
var_dump ($BookBinding);
if (preg_match('/Kindle Edition/i',$BookBinding))
{
//do stuff
}
}
}
但只獲得第一次迭代$ Binding和$ ASIN而不是所有4個元素 var_dump的輸出是「Unknown Binding」& B0058O8V9U
這不是一個*,而是*,這是正確的。與xpath查詢不同,您在這裏使用的迭代器每個「僅包含」一個元素。您可能希望使用xpath查詢liek'/ */*/Item/*/AlternateVersion/Binding'等代替。 – hakre 2013-05-04 19:40:40
看起來你有一些無效的XML。嘗試通過http://www.w3schools.com/xml/xml_validator.asp運行它並在再次嘗試之前更正相關錯誤。 – 2013-05-04 19:34:20
使用''ASIN [follow-sibling :: Binding =「Kindle Edition」]' – Gordon 2013-05-04 20:10:13