0
I have a pair of XML Files with following structure :

* the data contained here is random

        <root_tag>
        <packages>
         <package>
          <name>class_name1</name>
          <classes>3</classes>
          <functions>21</functions>
          < ncss>285</ncss>
          <javadocs>20</javadocs>
          <javadoc_lines>111</javadoc_lines>
          <single_comment_lines>11</single_comment_lines>
          <multi_comment_lines>222</multi_comment_lines>
    </package>
     </packages>

    <objects>

        <object>
          <name>object1</name>
          <ncss>255</ncss>
          <functions>17</functions>
          <classes>2</classes>
          <javadocs>20</javadocs>
        </object>

    </objects<
    <functions>
    <function>
          <name>function1</name>
          <ncss>242</ncss>
          <ccn>63</ccn>
          <javadocs>1</javadocs>
        </function>
    </functions>
    </root_tag>

包中包含以下数据项:

命名类函数 ncss javadocs javadoc_lines single_comment_lines multi_comment_lines

一个对象有以下与之关联的数据项:

命名函数 ncss javadocs 类

一个函数有以下数据项:

名称 ncss ccn javadocs

假设我的第二个 xml 文件包含 function1 的一些不同值。如何将这些 xml 文件合并到第三个文件中,并为每个名称元素分配一个唯一的 id,以便输出如下:

File  Id    Name    Classes  Functions  NCSS JavaDocs JavaDocLines SingleCommentLines

File1 func1 somefun Null     Null       10   20       30           40

File2 func1 somefun Null     Null       11   23        40          50 

有没有办法通过java程序做到这一点?

4

1 回答 1

1

使用 XSLT,您可以通过以下方式实现它:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<!-- You need to store the other inputs in xsl:variable to access it inside the same
stylesheet -->
<xsl:variable name="file2" select="document('file2.xml')"/>
<xsl:output method="xml"/>   
<xsl:template match="root_tag">
    <!--Open here your personnal layout stuffs,
    like opening tables <table:table> and things like that (column labels...)-->
    <table>
    <labels/>
    <xsl:apply-templates select=".//function | $file2//function">
         <!-- All the work is done by the xsl:sort which can specify the order you
         want to process you elements. -->
         <xsl:sort select="name"/>
    </xsl:apply-templates>
    <!-- Close here your layout stuffs -->
    </table>
</xsl:template>

<xsl:template match="function">
    <!-- Open here your layout inline stuffs-->
    <line>
    <!-- Here you may prefer to apply the templates
         in specific order in case of 'melted' input, 
        do this by calling templates in queue, like  <xsl:apply-templates
        select="name"/> <xsl:apply-templates select="ncss"/>...-->
        <xsl:apply-templates select="*"/>
    <!-- Close here your layout inline stuffs -->            
    </line>
</xsl:template>

<!-- This template may apply to anything but he's applied only on function childs
during the process -->
<xsl:template match="*">
    <!-- Open here the cell stuffs (<table:table-cell>) -->
    <cell>
    <xsl:value-of select="."/>
    <!-- Close here the cell stuffs-->
    </cell>
</xsl:template>

我为“布局材料”(如表格、线条和单元格)使用了一些虚拟元素。

希望这可能会有所帮助。

于 2013-03-13T18:27:39.080 回答