PHP SimpleXML with XPath?

There are several errors in your script, which explains why it doesn't work. First, your $response array doesn't seem right. Did you really intend to use variables in place of keys?

Try print_r($response) and see if it's really what you want You can select the nth question using the array notation (0-based) as in $results->question$i Once you get the right question, all you need to do is verify that it does indeed contain the suggested answer with XPath. Then only, you can increment the value of responses Also, you have to escape special characters such as or which would make your XPath query fail Finally, you can use asXML() to actually save the file (no file_put_contents() needed here. ) I assume that your $surveyResults is a typo and that you meant $surveyResultsFile $response = array( 'sid' => session_id(), 'answers' => array( $_POST'input1' ) ); $results = simplexml_load_file($surveyResultsFile); foreach ($response'answers' as $i => $answer) { $title = htmlspecialchars($answer); $a = $results->question$i->xpath('answertitle="' .

$title . '"'); if (empty($a)) { echo "No answer '$title' for question $i\n"; continue; } ++$a0->responses; } $results->asXML($surveyResultsFile).

There are several errors in your script, which explains why it doesn't work. First, your $response array doesn't seem right. Did you really intend to use variables in place of keys?

Try print_r($response); and see if it's really what you want. You can select the nth using the array notation (0-based) as in $results->question$i Once you get the right question, all you need to do is verify that it does indeed contain the suggested answer with XPath. Then only, you can increment the value of .

Also, you have to escape special characters such as session_id(), 'answers' => array( $_POST'input1' ) ); $results = simplexml_load_file($surveyResultsFile); foreach ($response'answers' as $i => $answer) { $title = htmlspecialchars($answer); $a = $results->question$i->xpath('answertitle="' . $title . '"'); if (empty($a)) { echo "No answer '$title' for question $i\n"; continue; } ++$a0->responses; } $results->asXML($surveyResultsFile).

Thanks! The $response array's keys being variables was a typo, as was the $surveyResults instead of $surveyResultsFile at the end. I definitely need to study XPath and SimpleXML more, as this is my first time using them.

– Jared Jan 23 '10 at 22:29.

The variables in the XPath experssion aren't being evaluated, instead it's processed literally. Replace the single quotes with normal quotes (") and the variables will be processed normally. See: php.net/manual/en/language.types.string.....

1 You also need to mark the string (literal) for xpath with quotes `title='$answer``. If $answer can possibly contain quotes you have to replace them by their entity representation, i.e. ' (or " depending on what quotes you chose to mark the string literal) – VolkerK Jan 22 '10 at 21:34 I made the changes, as well as removed the first forward slash which was causing problems.

I can successfully increment the value of $r now, but changes are not being saved to the XML file. – Jared Jan 22 '10 at 23:28.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions