您可以使用Xpath獲取多個節點。
- 結合兩個替代位置路徑/表達式
/Address/FirstLine|/Address/SecondLine
- 取任何地址子元素節點
/Address/*
- 取任何地址子元素節點,並將其篩選
/Address/*[self::FirstLine or self::SecondLine]
取合併的串直接使用SimpleXML是不可能的,但在DOM中是不可能的。 SimpleXMLElement::xpath()
只能返回節點數組(由SimpleXMLElement對象表示)。 DOMXpath::evaluate()
可以返回標量。
$xml = '<Address>
<FirstLine>Indiabulls Finance Centre Tower</FirstLine>
<SecondLine>Elphinstone Road</SecondLine>
</Address>';
$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$result = $xpath->evaluate("concat(/Address/FirstLine, '\n', /Address/SecondLine)");
var_dump($result);
輸出:
string(48) "Indiabulls Finance Centre Tower
Elphinstone Road"
來源
2017-03-03 09:24:18
ThW
我試着像$ XML->字符串連接( 「// CDT:地址/ *」,「「);但它顯示錯誤join():傳遞的參數無效 – hrishi
不,我的意思是'$ xml-> xpath('string-join(// Address/*,「」)');' – Andersson
SimpleXMLElement :: xpath():xmlXPathCompOpEval :function string-concat not found – hrishi