I have an XML document with a basic structure like this:
<?xml version="1.0" encoding="UTF-8" ?>
<records timestamp="1264777862">
<record></record>
<record></record>
<record></record>
<record></record>
</records>
I’ve been using the following so far:
$doc = new DOMDocument();
$doc->load('myfile.xml');
$xpath = new DOMXPath($doc);
$timestamp = $xpath->query("/records@timestamp");
But this gives me an invalid expression error.
What would be the correct PHP/XPath expression syntax to use to obtain the timestamp attribute of the root?
What is your existing PHP code? Are you using DOMDocument? SimpleXML?
The correct Xpath expression is ‘string(/records/@timestamp)’
For SimpleXML see http://php.net/manual/en/simplexmlelement.xpath.php
e.g.
And for DOMXPath see http://www.php.net/manual/en/domxpath.evaluate.php
EDIT
See edit above. Need to cast to string in the xpath expression