比方說,我有這樣的XML:使用Xpath和PHP獲取XML父/祖的價值?
<Routes>
<Route ref="timeconditions">
<Table>
<Tablename>timeconditions</Tablename>
<Fields>
<Fieldsname>truegoto</Fieldsname>
</Fields>
</Table>
</Route>
</Routes>
PHP:
$doc = new SimpleXMLElement('routingConfig.xml', null, true);
$foo = $doc->xpath('/Routes/Route[@ref="timeconditions"]/Table/Fields[@Fieldsname="truegoto"]/ancestor::Tablename');
print_r($foo);
返回空數組。現在我意識到我基本上是在挖掘一個孩子,然後回到父母身邊,但在我的情況下,這是我需要做的。我究竟做錯了什麼?我試圖獲得<Fieldsname>falsegoto</Fieldsname>
父母兄弟姐妹的值爲<Tablename>
。
將於本鬥雞眼盯着....
非常感謝 - 總是一個簡單的錯誤。 – Jared