我正在尝试对一些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!"
}
Run Code Online (Sandbox Code Playgroud)
完整的脚本在这里.
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>
Run Code Online (Sandbox Code Playgroud)
示例输入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>
Run Code Online (Sandbox Code Playgroud)
期望的输出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>
Run Code Online (Sandbox Code Playgroud)
如何使变换适用于所有元素,以便元素的所有子元素按字母顺序排列?
如果要将变换应用于所有元素,则需要一个模板来匹配所有元素,而不是使模板与特定的"foo"元素匹配
<xsl:template match="*">
Run Code Online (Sandbox Code Playgroud)
请注意,您必须更改与"node()"匹配的当前模板以排除元素:
<xsl:template match="node()[not(self::*)]|@*">
Run Code Online (Sandbox Code Playgroud)
在此模板中,您还需要代码来选择属性,因为此时"foo"模板将忽略它们(<xsl:apply-templates />不选择属性).
实际上,根据您的要求,项目1到3都可以使用单个XSLT完成.例如,要删除注释,您可以从当前与node()匹配的模板中忽略它
<xsl:template match="node()[not(self::comment())][not(self::*)]|@*">
Run Code Online (Sandbox Code Playgroud)
尝试以下XSLT,将实现1到3点
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()[not(self::comment())][not(self::*)]|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*">
<xsl:sort select="name()"/>
</xsl:apply-templates>
<xsl:apply-templates>
<xsl:sort select="name()"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
编辑:<xsl:template match="node()[not(self::comment())][not(self::*)]|@*">实际上可以替换模板,<xsl:template match="processing-instruction()|@*">这可能会提高可读性.这是因为"node()"匹配元素,文本节点,注释和处理指令.在您的XSLT中,元素由其他模板拾取,文本节点由内置模板拾取,以及您要忽略的注释,只留下处理指令.