开发者

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

开发者 https://www.devze.com 2023-01-27 13:22 出处:网络
XML newbie here! I have a file containing only the following XML: <tags> <tag>orange</tag&开发者_如何学编程gt;

XML newbie here!

I have a file containing only the following XML:

<tags>
 <tag>orange</tag&开发者_如何学编程gt;
 <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!


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消