PHP SimpleXML 与 XPath

发布于 2024-08-19 01:36:51 字数 1233 浏览 6 评论 0原文

我有一个如下所示的 XML 结构:

<?xml version="1.0"?>
<survey>
  <responses>0</responses>
  <question>
    <title>Some survey question</title>
    <answer>
      <title>Answer 1</title>
      <responses>0</responses>
    </answer>
    <answer>
      <title>Answer 2</title>
      <responses>0</responses>
    </answer>
    ...
  </question>
  ...
</survey>

我想要增加与 $response 数组中的值匹配的答案的 值。 $response 数组的结构如下:

$response = array(
  'sid' => session_id(),
  'answers' => array(
    $_POST['input1'],
    $_POST['input2'],
    ...
  )
);

我的调查 xml 文件有一个名为 $results 的 SimpleXMLElement。我的处理方法如下:

$results = simplexml_load_file($surveyResultsFile);

$i = 1;
foreach($response->answers as $answer) {
  $r = $results->xpath("question[$i]/answer[title='$answer']/responses");
  $r = $r[0];
  $r = intval($r) + 1;
  $i++;
}

file_put_contents($surveyResultsFile, $results->asXML());

增加 $r 的值后,我的结果没有被保存。关于我做错了什么有什么想法吗?谢谢!

I have an XML structure that looks like this:

<?xml version="1.0"?>
<survey>
  <responses>0</responses>
  <question>
    <title>Some survey question</title>
    <answer>
      <title>Answer 1</title>
      <responses>0</responses>
    </answer>
    <answer>
      <title>Answer 2</title>
      <responses>0</responses>
    </answer>
    ...
  </question>
  ...
</survey>

I want to increment the <responses> values for answers that match the values in a $response array. Here's how the $response array is structured:

$response = array(
  'sid' => session_id(),
  'answers' => array(
    $_POST['input1'],
    $_POST['input2'],
    ...
  )
);

I have a SimpleXMLElement called $results for my survey xml file. Here's how I'm going about it:

$results = simplexml_load_file($surveyResultsFile);

$i = 1;
foreach($response->answers as $answer) {
  $r = $results->xpath("question[$i]/answer[title='$answer']/responses");
  $r = $r[0];
  $r = intval($r) + 1;
  $i++;
}

file_put_contents($surveyResultsFile, $results->asXML());

My results aren't being saved after incrementing the value of $r. Any ideas on what I'm doing wrong? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩人痒 2024-08-26 01:36:51

您的脚本中有几个错误,这解释了为什么它不起作用。首先,您的 $response 数组似乎不正确。您真的打算使用变量代替键吗?尝试 print_r($response); 看看它是否真的是您想要的。

您可以使用数组符号(从 0 开始)选择第 n 个 ,如

$results->question[$i]

一旦您得到正确的问题,您需要做的就是验证它确实包含建议的问题用 XPath 回答。只有这样,您才能增加 的值<响应/>。另外,您必须转义特殊字符,例如 <",这会使您的 XPath 查询失败。

最后,您可以使用 asXML() 实际保存文件(此处不需要 file_put_contents()。)我假设您的 $surveyResults 是一个拼写错误,您的意思是 $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('answer[title="' . $title . '"]');

    if (empty($a))
    {
        echo "No answer '$title' for question $i\n";
        continue;
    }
    ++$a[0]->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 <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('answer[title="' . $title . '"]');

    if (empty($a))
    {
        echo "No answer '$title' for question $i\n";
        continue;
    }
    ++$a[0]->responses;
}

$results->asXML($surveyResultsFile);
红玫瑰 2024-08-26 01:36:51

一个问题可能是这样的:

$r = $results->xpath('/question[$i]/answer[title=$answer]/responses');

由于您使用单引号(' 字符,是实际的英文名称“单引号”?),XPath 表达式中的变量不会被计算,而是按字面意思进行处理。将单引号替换为普通引号("),变量将被正常处理。

参见:http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single

One problem might be this:

$r = $results->xpath('/question[$i]/answer[title=$answer]/responses');

Since you're using single quotes (the ' -character, is the actual English name "single quote"?), 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: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文