2
votes

I have this code

$XMLTranslationObject = new DOMDocument();
$XMLTranslationObject->load("../xdata/xml/translation/en-US/profile.xml");

$Xpath = new DOMXpath($XMLTranslationObject);
$Result = $Xpath->evaluate("Field[@Name = 'AttachedFiles']")->nodeValue;

echo $Result;

and the xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<Translation language="en-US">

    <!-- Basic Begin -->
        <Field Name="SiteName">Brainfavor</Field>
        <Field Name="Error">Error</Field>
    <!-- Basic End -->

    <!-- Header Begin -->
        <Field Name="Name">Name</Field>
        <Field Name="WorkingAt">Working at</Field>
        <Field Name="Phone">Phone</Field>

witch gives me the following error

Notice: Undefined property: DOMNodeList::$nodeValue in C:\xampp\htdocs\brainbook\profile\loadmore.php on line 30

All i want is to retrieve the value of a row where field name is AttachedFiles , just run a simple Xpath query and retrieve the result. Using XSLT it is easy

<xsl:value-of select="$TranslationProfile/Field[@Name = 'AttachedFiles']"/>

but i didn't used PHP Xpath before.

1

1 Answers

2
votes

The correct formulation is

$Xpath->evaluate("string(Field[@Name = 'AttachedFiles'])");

This works because the string function converts the node set (which actually would consist of just one node in this case) to the value the first node in the set.

Warning: this means that if there are multiple elements that match the query only the first one (in document order) will have its value returned.