I have an xml how can I get the node in levelone that has an attribute called myatt whose value is a and then access it’s myval.
I tried referencing other posts to make it work but it doesn’t seem to work what’s wrong with my xpath
$this->myXmlObj->xpath("//levelone[myfield[attributes/myatt='a]]]"));
<myxml>
<levelone>
<myfield myatt="a" myval="aa" />
<myfield myatt="b" myval="bb" />
</levelone>
<leveltwo>
<myfield myatt="c" myval="dd" />
<myfield myatt="c" myval="dd" />
</leveltwo>
</myxml>
edit 1
array
0 =>
object(SimpleXMLElement)[41]
public '@attributes' =>
array
'myval' => string 'a' (length=40)
edit 2
$myVar = $this->myXmlObj->xpath("//levelone/myfield[@myatt='a']");
$myOutput = ((string)$myVar[0]->attributes()->myVal;
Attributes in XPATH are referenced with
@attrsyntax. So, you could retrieveaawith the following xpathWhich means, grab all
myfieldelements that have attributemyattequal to'a'. Then, from those, select the value of theirmyvalattributes. Note that this could be multiple results.A handy place to test XPATH expressions is at http://chris.photobooks.com/xml/default.htm.