How can XML data be sorted and displayed in PHP?

To sort and display XML data in PHP, you can use SimpleXML to parse the XML file, extract the data, sort it using PHP functions like usort(), and then display the sorted data. You can also use XSLT to transform the XML data into a desired format before displaying it.

<?php
$xmlString = file_get_contents('data.xml');
$xml = simplexml_load_string($xmlString);

// Sort the XML data by a specific attribute
usort($xml->item, function($a, $b) {
    return (int) $a->attribute - (int) $b->attribute;
});

// Display the sorted XML data
foreach ($xml->item as $item) {
    echo $item->attribute . ': ' . $item->value . '<br>';
}
?>