我很难绕过XSLT,但我听说可以将XML文件拆分成多个文件.基本上我想将所有元素复制到第一个文件和最后一个文件之后,然后为每个输出文件添加单个文件内容.
如果有可能,有人可以给我一些指示吗?
谢谢,
complete.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>
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
complete_PART1.xml
<rootelem>
<elem>
<file attr1='1'>
<content>content file 1</content>
</file>
</elem>
</rootelem>
Run Code Online (Sandbox Code Playgroud)
complete_PART2.xml
<rootelem>
<elem>
<file attr2='2'>
<content>content file 2</content>
</file>
</elem>
</rootelem>
Run Code Online (Sandbox Code Playgroud)
complete_PART3.xml
<rootelem>
<elem>
<file attr3='3'>
<content>content file 3</content>
</file>
</elem>
</rootelem>
Run Code Online (Sandbox Code Playgroud)
Lar*_*rsH 16
回应你对@Dimitre答案的评论......
你写了,
<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>
Run Code Online (Sandbox Code Playgroud)
这与您的XML不完全匹配,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>
Run Code Online (Sandbox Code Playgroud)
你可能会变得更加漂亮,尝试使用<xsl:copy>
而不是文字结果元素用于root和elem,但它似乎不值得努力,除非它们会变化.
Dim*_*hev 14
纯XSLT 1.0中不可能生成多个输出文件.可以使用<exslt:document>
扩展元素来实现此目的.
在XSLT 2.0中使用该<xsl:result-document>
元素.