问题
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