使用 XSLT 将 XML 拆分为多个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4036233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Splitting XML into multiple files with XSLT
提问by Fredrik L
I'm having a hard time wrapping my head around XSLT but I heard it's possible to split an XML file into multiple files. Basically I'd like to copy all the elements up to the first file and after the last file and then add the individual file content for each output file.
我很难围绕 XSLT 进行思考,但我听说可以将一个 XML 文件拆分为多个文件。基本上我想将所有元素复制到第一个文件和最后一个文件之后,然后为每个输出文件添加单独的文件内容。
Could someone give me some pointers on this if it's even possible?
如果可能的话,有人可以给我一些指示吗?
Thanks,
谢谢,
complete.xml
完整的.xml
<rootelem>
<elem>
<file attr1='1'>
<content>content file 1</content>
</file>
<file attr2='2'>
<content>content file 2</content>
</file>
<file attr3='3'>
<content>content file 3</content>
</file>
</elem>
</rootelem>
OUTPUT:
输出:
complete_PART1.xml
complete_PART1.xml
<rootelem>
<elem>
<file attr1='1'>
<content>content file 1</content>
</file>
</elem>
</rootelem>
complete_PART2.xml
complete_PART2.xml
<rootelem>
<elem>
<file attr2='2'>
<content>content file 2</content>
</file>
</elem>
</rootelem>
complete_PART3.xml
complete_PART3.xml
<rootelem>
<elem>
<file attr3='3'>
<content>content file 3</content>
</file>
</elem>
</rootelem>
采纳答案by LarsH
Responding to your comment on @Dimitre's answer...
回应你对@Dimitre 的回答的评论......
You wrote,
你写了,
<xsl:template match="/">
<xsl:for-each select="elem/file">
<xsl:result-document method="xml" href="file_{@id}-output.xml">
<xsl:copy-of select="."/>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
This doesn't quite match your XML, which has rootelemas an outermost element, and your comment says rootas an outermost element. You probably want something like this:
这与您的 XML 不太匹配,它具有rootelem最外层元素,而您的评论说root是最外层元素。你可能想要这样的东西:
<xsl:template match="/root">
<xsl:for-each select="elem/file">
<xsl:result-document method="xml" href="file_{@id}-output.xml">
<root>
<xsl:copy-of select="/root/@*" />
<elem>
<xsl:copy-of select="../@* | ." />
</elem>
</root>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
You could get fancier, trying to use <xsl:copy>instead of literal result elements for root and elem, but it doesn't seem worth the effort unless they're going to vary.
您可以变得更有趣,尝试使用<xsl:copy>root 和 elem 代替字面结果元素,但除非它们会有所不同,否则似乎不值得付出努力。
回答by Dimitre Novatchev
It is not possible in pure XSLT 1.0to produce more than one output files. One could use the <exslt:document>extension element for this purpose.
在纯 XSLT 1.0 中不可能产生多个输出文件。<exslt:document>为此可以使用扩展元素。
In XSLT 2.0 use the <xsl:result-document>element.
在 XSLT 2.0 中使用该<xsl:result-document>元素。
回答by millebii
If you want to use
如果你想使用
<xsl:result-document method="xml" href="file_{@id}-output.xml">
from an ANT xslt call, you need to use 2.0., just add the following in your ANT call:
从 ANT xslt 调用中,您需要使用 2.0.,只需在您的 ANT 调用中添加以下内容:
<classpath location="/home/ap/saxon/saxon8.jar" />
And specifiy Version="2.0" And enjoy file splitting.
并指定 Version="2.0" 并享受文件拆分。

