合并2个XML文件并修改属性值 [英] Merge 2 XML files and modifying the attribute values

查看:83
本文介绍了合并2个XML文件并修改属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个xml文件.我想合并它们,并使用一些属性进行一些算术运算.请提供一些想法.我正在使用标准的xslt http://informatik.hu-berlin.de/merge 进行合并文件.

I have two xml files. I want to merge them and make some arithmetic with a few attributes. Please provide some ideas. I am using a standard xslt http://informatik.hu-berlin.de/merge to merge the files.

文件1:

<coverage branch-rate="0.5125" branch-total="50" line-rate="0.00593031875463">  
</coverage> 

文件2:

<coverage branch-rate="0.5" branch-total="40" line-rate="1.0">  
</coverage>

预期结果文件

<coverage branch-rate="(0.5125*50 + 05*40)/(50+40)" branch-total="50" line-rate="0.00593031875463"> 
</coverage> 

推荐答案

此转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pFile1" select="'file:///c:/temp/delete/file1.xml'"/>
 <xsl:param name="pFile2" select="'file:///c:/temp/delete/file2.xml'"/>

 <xsl:variable name="vF2Cover" select="document($pFile2)/coverage"/>

 <xsl:template match="/">
   <xsl:apply-templates select="document($pFile1)/coverage"/>
 </xsl:template>

 <xsl:template match="coverage">
   <coverage branch-rate=
    "{(@branch-rate*@branch-total + $vF2Cover/@branch-rate*$vF2Cover/@branch-total)
      div (@branch-total+$vF2Cover/@branch-total)
     }"
   branch-total=
    "{@branch-total*(@branch-total>= $vF2Cover/@branch-total)
    +
     $vF2Cover/@branch-total*($vF2Cover/@branch-total >@branch-total)
     }"
   line-rate=
    "{@line-rate*($vF2Cover/@line-rate >= @line-rate)
    +
     $vF2Cover/@line-rate*(@line-rate > $vF2Cover/@line-rate)
     }"/>
 </xsl:template>
</xsl:stylesheet>

应用于任何XML文档(未使用)时,并且提供的两个XML文档位于:

when applied on any XML document (not used), and having the two provided XML documents reside in:

c:/temp/delete/file1.xml:

c:/temp/delete/file1.xml:

<coverage branch-rate="0.5125" branch-total="50" line-rate="0.00593031875463">
</coverage>

和c:/temp/delete/file2.xml:

and c:/temp/delete/file2.xml:

<coverage branch-rate="0.5" branch-total="40" line-rate="1.0">
</coverage>

产生所需的正确结果:

<coverage branch-rate="0.5069444444444444" branch-total="50" line-rate="0.00593031875463" />

这篇关于合并2个XML文件并修改属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆