2011-11-10 2 views
1

원래 XML 텍스트 : 정렬 후XML 태그를 사전 순으로 정렬하는 PHP 코드는 무엇입니까?

<a> 
    <ccc /> 
    <bb></bb> 
    <d> 
    <ff /> 
    <eee /> 
    </d> 
</a> 

, 그것은해야한다 : 당신이 그것을 할 수있는 좋은 방법을 알고 있다면

<a> 
    <bb /> 
    <ccc /> 
    <d> 
    <eee /> 
    <ff /> 
    </d> 
</a> 

이, 저를 보여주십시오. 고맙습니다!

+0

이 –

+0

http://stackoverflow.com/questions/1359224/sort-xml-via-attribute-value-php – Mob

+0

@NamGiVU하지만 C#에서의 @abhinav 죄송 통해 , 삭제됨. – abhinav

답변

3

예 : php's XSL module

<?php 
$xsl = new XSLTProcessor(); 
$xsl->importStyleSheet(getDoc(getStylesheetData())); 
$doc = getDoc(getDocData()); 
echo $xsl->transformToXML($doc); 

function getDoc($s) { 
    $doc = new DOMDocument; 
    $doc->loadxml($s); 
    return $doc; 
} 

function getStylesheetData() { 
    return <<< eox 
<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/> 
<xsl:strip-space elements="*" /> 
<xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:copy-of select="@*"/> 
     <xsl:apply-templates select="node()"> 
      <xsl:sort select="name()"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 
eox; 
} 


function getDocData() { 
    return <<< eox 
<a> 
    <ccc /> 
    <bb></bb> 
    <d> 
     <ff /> 
     <eee /> 
    </d> 
</a> 
eox; 
} 

인쇄

<?xml version="1.0" encoding="iso-8859-1"?> 
<a> 
    <bb/> 
    <ccc/> 
    <d> 
    <eee/> 
    <ff/> 
    </d> 
</a> 
+0

완벽한 답변! 고맙습니다. VolkerK –

관련 문제