【发布时间】:2016-04-20 06:32:00
【问题描述】:
我的目标是按字母顺序对 XML 元素进行排序。我在这里找到了解决方案:Sort elements of arbitrary XML document recursively
部分:
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 ) )
在 Groovy Web 控制台中,它总是以不同的顺序返回 XML 节点,而不是按字母顺序。我不能使用 XSLT 转换,这可能适用于任何 XML 文档
代码修改有帮助吗?
【问题讨论】: