我正在尝试对一些 XML 文档进行排序和规范化。期望的最终结果是:
- 每个元素的子元素都按字母顺序排列
- 每个元素的属性都按字母顺序排列
- 评论被删除
- 所有元素的间距都适当(即“漂亮的打印”)。
除了#1,我已经实现了所有这些目标。
我一直使用这个答案作为我的模板。这是我到目前为止所拥有的:
import javax.xml.transform.stream.StreamResult
import javax.xml.transform.stream.StreamSource
import javax.xml.transform.TransformerFactory
import org.apache.xml.security.c14n.Canonicalizer
// Initialize the security library
org.apache.xml.security.Init.init()
// Create some variables
// Get arguments
// Make sure required arguments have been provided
if(!error) {
// Create some variables
def ext = fileInName.tokenize('.').last()
fileOutName = fileOutName ?: "${fileInName.lastIndexOf('.').with {it != -1 ? fileInName[0..<it] : fileInName}}_CANONICALIZED_AND_SORTED.${ext}"
def fileIn = new File(fileInName)
def fileOut = new File(fileOutName)
def xsltFile = new File(xsltName)
def temp1 = new File("./temp1")
def temp2 = new File("./temp2")
def os
def is
// Sort the XML attributes, remove comments, and remove extra whitespace
println "Canonicalizing..."
Canonicalizer c = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS)
os = temp1.newOutputStream()
c.setWriter(os)
c.canonicalize(fileIn.getBytes())
os.close()
// Sort the XML elements
println "Sorting..."
def factory = TransformerFactory.newInstance()
is = xsltFile.newInputStream()
def transformer = factory.newTransformer(new StreamSource(is))
is.close()
is = temp1.newInputStream()
os = temp2.newOutputStream()
transformer.transform(new StreamSource(is), new StreamResult(os))
is.close()
os.close()
// Write the XML output in "pretty print"
println "Beautifying..."
def parser = new XmlParser()
def printer = new XmlNodePrinter(new IndentPrinter(fileOut.newPrintWriter(), " ", true))
printer.print parser.parseText(temp2.getText())
// Cleanup
temp1.delete()
temp2.delete()
println "Done!"
}
完整的脚本在这里。
XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="foo">
<foo>
<xsl:apply-templates>
<xsl:sort select="name()"/>
</xsl:apply-templates>
</foo>
</xsl:template>
</xsl:stylesheet>
示例输入 XML:
<foo b="b" a="a" c="c">
<qwer>
<zxcv c="c" b="b"/>
<vcxz c="c" b="b"/>
</qwer>
<baz e="e" d="d"/>
<bar>
<fdsa g="g" f="f"/>
<asdf g="g" f="f"/>
</bar>
</foo>
所需的输出 XML:
<foo a="a" b="b" c="c">
<bar>
<asdf f="f" g="g"/>
<fdsa f="f" g="g"/>
</bar>
<baz d="d" e="e"/>
<qwer>
<vcxz b="b" c="c"/>
<zxcv b="b" c="c"/>
</qwer>
</foo>
如何使转换应用于所有元素,以便元素的所有子元素都按字母顺序排列?