我想知道如何根据以下要求编写XSLT来将XML文件拆分为多个XML文件:
XML输入文件是:
<Lakes>
<Lake>
<id>1</id>
<Name>Caspian</Name>
<Type>Natyral</Type>
</Lake>
<Lake>
<id>2</id>
<Name>Moreo</Name>
<Type>Glacial</Type>
</Lake>
<Lake>
<id>3</id>
<Name>Sina</Name>
<Type>Artificial</Type>
</Lake>
</Lakes>发布于 2011-01-28 22:46:42
使用XSLT2.0,如以下样式表所示:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each-group select="Lakes/Lake" group-by="Type">
<xsl:result-document href="file{position()}.xml">
<Lakes>
<xsl:copy-of select="current-group()"/>
</Lakes>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>备注:xsl:result-document指令。
发布于 2011-01-28 21:55:30
使用标准的XSL,不可能有一个以上的输出xml (即结果树)。
但是,使用Xalan 重定向扩展,您可以。
查看链接中页面上的示例。我用XalanJava2.7.1测试了以下内容
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:redirect="http://xml.apache.org/xalan/redirect" extension-element-prefixes="redirect">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="/Lakes/Lake[Type='Natyral']">
<redirect:write file="/home/me/file1.xml">
<NatyralLakes>
<xsl:copy-of select="." />
</NatyralLakes>
</redirect:write>
</xsl:template>
<xsl:template match="/Lakes/Lake[Type='Artificial']">
<redirect:write file="/home/me/file1.xml">
<ArtificialLakes>
<xsl:copy-of select="." />
</ArtificialLakes>
</redirect:write>
</xsl:template>
<xsl:template match="/Lakes/Lake[Type='Glacial']">
<redirect:write file="/home/me/file3.xml">
<GlacialLakes>
<xsl:copy-of select="." />
</GlacialLakes>
</redirect:write>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/4833361
复制相似问题