2016-04-20 6 views
0

私の目標は、XML要素をアルファベット順にソートすることです。私はここで解決策を見つけた:Sort elements of arbitrary XML document recursivelyGroovy - XML要素のソート

一部:GroovyのWebコンソールで

def x = '''<foo b="b" a="a" c="c"> 
<qwer> 
<!-- A comment --> 
<zxcv c="c" b="b">Some Text</zxcv> 
<vcxz c="c" b="b"/> 
</qwer> 
<baz e="e" d="d">Woo</baz> 
<bar> 
<fdsa g="g" f="f"/> 
<asdf g="g" f="f"/> 
</bar> 
</foo>''' 

def order(node) { 
[ *:node.attributes() ].sort().with { attr -> 
    node.attributes().clear() 
    attr.each { node.attributes() << it } 
} 
node.children().sort() 
       .grep(Node) 
       .each { order(it) } 
node 
} 

def doc = new XmlParser().parseText(x) 

println groovy.xml.XmlUtil.serialize(order(doc)) 

、それは必ずしもアルファベット順に、異なる順序でXMLノードを返します。私はXSLT変換を使用することはできませんが、これはどんなXML文書でも機能します

コードを変更する助けがありますか?

答えて

0

属性は、それゆえ、HashMapのとして格納されている彼らは、XMLのいずれか

の順序を持​​っていない何のため

を持っていないので、私はあなたが彼らに

を並べ替えることができるとは思いません