在 XSLT 1.0 中,不可能创建多个树作为任何转换的输出,但在 XSLT 2.0 中,这可以很容易地完成。
在 XSLT 1.0 中,可以使用 EXSLT 的 <exsl:document> 扩展元素。
或者,可以进行转换,即提供一个全局(和外部指定的)参数,其中包含必须提取到单个文档中的元素的元素名称:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pDocElement" select="'doc1'"/>
<xsl:template match="/*/*">
<xsl:if test="name()=$pDocElement">
<xsl:apply-templates select="." mode="copy"/>
</xsl:if>
</xsl:template>
<xsl:template match="node()" mode="copy">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
应用于此 XML 文档时(基于提供的文档):
<t>
<doc1>
Doc 1 Content
</doc1>
<doc2>
Doc 2 Content
</doc2>
<doc3>
Doc 3 Content
</doc3>
</t>
产生想要的结果:
<doc1>
Doc 1 Content
</doc1>
您将为每个元素运行一次此转换,其子树应提取到单独的文档中。
这是一个 XSLT 2.0 解决方案:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|@*" mode="copy">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*/*[starts-with(name(),'doc')]">
<xsl:result-document href="{name()}.xml">
<xsl:apply-templates select="." mode="copy"/>
</xsl:result-document>
</xsl:template>
<xsl:template match="/*">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于以下 XML 文档时(基于提供的文档):
<t>
<doc1>
Doc 1 Content
</doc1>
<doc2>
Doc 2 Content
</doc2>
<doc3>
Doc 3 Content
</doc3>
</t>
结果正确输出到三个文档:
Saxon 9.1.0.5J from Saxonica
Java version 1.6.0_21
Stylesheet compilation time: 868 milliseconds
Loading net.sf.saxon.event.MessageEmitter
Writing to file:/C:/Program%20Files/Java/jre6/bin/doc1.xml
Writing to file:/C:/Program%20Files/Java/jre6/bin/doc2.xml
Writing to file:/C:/Program%20Files/Java/jre6/bin/doc3.xml
Execution time: 151 milliseconds
Memory used: 11467936
NamePool contents: 18 entries in 18 chains. 6 prefixes, 6 URIs