每个文件如下所示:
<thickness_metafile>
<job_bundle>
<analysis_description>
<material_table>
material
</material_table>
<thickness_set>
ray element 1
ray element 2
ray element 3
</thickness_set>
</analysis_description>
</job_bundle>
因为每个文件中的所有内容都是相同的,所以我想复制最后224个文件中的每个thickness_set,并将它们直接粘贴到第一个thickness_set下的第一个文件中。本质上,我希望输出如下所示:
<thickness_metafile>
<job_bundle>
<analysis_description>
<material_table>
material
</material_table>
<thickness_set>
ray element 1
ray element 2
ray element 3
</thickness_set>
<thickness_set>
ray element 4
ray element 5
ray element 6
</thickness_set>
<thickness_set>
ray element 7
ray element 8
ray element 9
</thickness_set>
</analysis_description>
</job_bundle>
在最终编辑的文件中应该有225个厚度集。我尝试了一些方法,取得了一些成功,但输出的格式不是我想要的格式。
发布于 2015-11-15 06:59:39
假设我们有名称为"file1.xml“、"file2.xml”、"file3.xml“等的xml文件。我们可以在xsl中使用递归模板调用来处理它们:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="thickness_set">
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
<xsl:call-template name="file-loop">
<!-- continue with file number 2 -->
<xsl:with-param name="count" select="2"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="file-loop">
<xsl:param name="count"/>
<!-- no more than 226 files -->
<xsl:if test="$count < 226">
<thickness_set>
<xsl:variable name="fi" select="document(concat('file',$count,'.xml'))"/>
<xsl:value-of select="$fi//thickness_set"/>
</thickness_set>
<xsl:call-template name="file-loop">
<xsl:with-param name="count" select="$count+1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>我们在xsl中传递的第一个xml文件(可以使用任何名称)。其他名称为"file2.xml“等的文件会自动加载。
https://stackoverflow.com/questions/33713845
复制相似问题