如何使用 SimpleDom Sortedxpath 对多个属性上的 XML 节点进行排序?

发布于 2024-10-02 09:29:46 字数 942 浏览 0 评论 0原文

我正在使用 SimpleDom 库,需要排序xpath 函数的帮助。 这是 XML

<cal>
 <entry entry_id="1">
  <entry_date year="1980" month="10" day="12" />
  <entry_title>John Bday</entry_title>
 </entry>
 <entry entry_id="2">
  <entry_date year="1980" month="10" day="10" />
  <entry_title>Peter Bday</entry_title>
 </entry>
 <entry entry_id="3">
  <entry_date year="1980" month="10" day="16" />
  <entry_title>Allan Bday</entry_title>
 </entry>
</cal>

我想根据“entry_date”节点的“年”、“月”和“日”属性的值对所有“entry”节点进行排序,所有这些都按相反顺序(最近的在顶部) 。因此,在上面的示例中,我希望最终的顺序是:

entry #3
entry #1
entry #2

这是我当前无法正常工作的 PHP(根本没有输出):

$xml = simpledom_load_file("data.xml");
foreach ($xml->sortedXPath('entry', 'entry_date_start[@year]', SORT_DESC) as $i => $item)
{
   echo($item);
}

非常感谢任何帮助。谢谢!

I'm using the SimpleDom library and need help with the sortedxpath function.
Here's the XML

<cal>
 <entry entry_id="1">
  <entry_date year="1980" month="10" day="12" />
  <entry_title>John Bday</entry_title>
 </entry>
 <entry entry_id="2">
  <entry_date year="1980" month="10" day="10" />
  <entry_title>Peter Bday</entry_title>
 </entry>
 <entry entry_id="3">
  <entry_date year="1980" month="10" day="16" />
  <entry_title>Allan Bday</entry_title>
 </entry>
</cal>

I would like to sort all the 'entry' nodes according to the values of the 'year', 'month' and 'day' attributes of the 'entry_date' node, all that in reverse order (most recent on top). So in the example above, I'd like the final order to be:

entry #3
entry #1
entry #2

Here's the PHP I currently have which is not working (no output at all):

$xml = simpledom_load_file("data.xml");
foreach ($xml->sortedXPath('entry', 'entry_date_start[@year]', SORT_DESC) as $i => $item)
{
   echo($item);
}

Any help gladly appreciated. Thanks!

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

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

发布评论

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

评论(1

冬天旳寂寞 2024-10-09 09:29:46

最终通过反复试验找到了正确的语法:

foreach ($xml->sortedXPath('entry', 'entry_date/@year', SORT_DESC, 'entry_date/@month', SORT_DESC, 'entry_date/@day', SORT_DESC) as $i => $item)
{
   // do stuff
}

我希望这可以帮助其他人。感谢您的阅读。

Finally found the correct syntax by trial and error:

foreach ($xml->sortedXPath('entry', 'entry_date/@year', SORT_DESC, 'entry_date/@month', SORT_DESC, 'entry_date/@day', SORT_DESC) as $i => $item)
{
   // do stuff
}

I hope this can help others. Thanks for reading.

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