How does one use SimpleDOM sortedXPath to sort on node value?

て烟熏妆下的殇ゞ 提交于 2019-12-11 13:34:10

问题


XML newbie here!
I have a file containing only the following XML:

<tags>
 <tag>orange</tag>
 <tag>apple</tag>
 <tag>banana</tag>
</tags>

I want to ouput the tags alphabetically.
I am trying to use SimpleDOM library and its sortedXPath method. Here's what I have so far, which outputs the tags unsorted.

$allTags = simpledom_load_file("tags.xml");
foreach ($allTags->sortedXPath("//tags/tag", "tag") as $i => $item)
{
   echo($item);
}

Could someone tell me how to write this correctly so it works? Cheers!


回答1:


In XPath, you can refer the current node (called "context node") using a single dot . so if you're accessing //tags/tag you have to use . to get the value of tag. Your example becomes:

$allTags = simpledom_load_file("tags.xml");
foreach ($allTags->sortedXPath("//tags/tag", ".") as $i => $item)
{
    echo($item);
}


来源:https://stackoverflow.com/questions/4299188/how-does-one-use-simpledom-sortedxpath-to-sort-on-node-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!