如何通过id获取节点


How to get a node by id?

我有以下xml格式

<locale id='us'>
items here
</locale>

如何通过其id获得区域设置节点?

我有以下代码

$xml = simplexml_load_file("config.xml");
$nodes = $xml->xpath('*[id = "us"]');

但我猜这不是正确的方法

使用@轴说明符来引用属性:

*[@id='us']

如果您想通过ID获取可以出现在文档中任何位置的元素,则使用:

//*[@id='us']
$xml = simplexml_load_file("config.xml");
$nodes = $xml->xpath("*[@id='us']");