Reputation: 35434
The original XML text:
<a>
<ccc />
<bb></bb>
<d>
<ff />
<eee />
</d>
</a>
After sorted, it should be:
<a>
<bb />
<ccc />
<d>
<eee />
<ff />
</d>
</a>
If you know a nice way to do it, please show me. Thank you!
Upvotes: 1
Views: 673
Reputation: 96189
Can be done e.g. via 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;
}
prints
<?xml version="1.0" encoding="iso-8859-1"?>
<a>
<bb/>
<ccc/>
<d>
<eee/>
<ff/>
</d>
</a>
Upvotes: 3