Sorting XML with SimpleXML/XPath?

前端 未结 3 1628
囚心锁ツ
囚心锁ツ 2020-12-07 05:41

I have some XML, say:


  
    14
  
  
    19&         


        
3条回答
  •  一个人的身影
    2020-12-07 06:15

    Possible solution:

    • Parse the XML using SimpleXml
    • Use usort to sort the array containing the background elements.
    • Optionally, export it to XML again (there is no built-in function to do this)

    Usort would work as follows:

    function comp($a, $b) {
        return $b->Uses - $a->Uses;
    }
    
    usort($xml->Backgrounds, 'comp');
    

    Another way is to use an XSLT, with something like this:

    
    

提交回复
热议问题