XSLT合并两个XML文件

时间:2018-06-29 08:50:48

标签: xml apache xslt merge xalan

我需要使用XSLT合并两个XML文件。转换在一个XML文件上进行,该XML文件包含要合并的XML文件列表。

list.xml

<?xml version="1.0" encoding="UTF-8" ?>
<files>
    <file>..\src\main\resources\testOne.xml</file>
    <file>..\src\main\resources\testTwo.xml</file>
</files>

这是我要合并的两个模板:

<xsl:template name="merge_nodes">
    <xsl:param name="fnNewDeept"/>
    <xsl:param name="snNewDeept"/>

    <xsl:for-each select="$fnNewDeept">
        <xsl:call-template name="merge_node">
            <xsl:with-param name="first-node" select="$fnNewDeept"/>
            <xsl:with-param name="second-node" select="$snNewDeept"/>
        </xsl:call-template>
    </xsl:for-each>
</xsl:template>

<xsl:template name="merge_node">
    <xsl:param name="first-node" />
    <xsl:param name="second-node" />

    <xsl:element name="{name(current())}">
        <xsl:for-each select="$second-node/@*">
            <xsl:copy/>
        </xsl:for-each>
        <xsl:if test="$first-node = '' and not(boolean($first-node/*) and boolean($second-node/*))">
            <xsl:value-of select="$second-node"/>
        </xsl:if>

        <xsl:for-each select="$first-node/@*">
            <xsl:copy/>
        </xsl:for-each>
        <xsl:if test="not(boolean($first-node/*) and boolean($second-node/*))">
            <xsl:value-of select="$first-node"/>
        </xsl:if>

        <xsl:choose>
            <xsl:when test="boolean($first-node/*) or boolean($second-node/*)">     
                <xsl:choose>                                                        
                    <xsl:when test="boolean($first-node/*/*)">                      
                        <xsl:call-template name="merge_nodes">                      
                            <xsl:with-param name="fnNewDeept" select="$first-node/*"/>
                            <xsl:with-param name="snNewDeept" select="$second-node/*"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        2. Value: <xsl:value-of select="current()/*"/>
                        2. Current: <xsl:value-of select="name(current()/*)"/>
                        2. First: <xsl:value-of select="name($first-node/*)"/>
                        2. Second: <xsl:value-of select="name($second-node/*)"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                1. Value: <xsl:value-of select="current()"/>
                1. Current: <xsl:value-of select="name(current())"/>
                1. First: <xsl:value-of select="name($first-node)"/>
                1. Second: <xsl:value-of select="name($second-node)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:element>
</xsl:template>
  • 仅出于调试原因,值,当前,第一和第二。

和我的两个XML:

<?xml version="1.0" encoding="UTF-8" ?>
<first x="1">
    <second param="wt" second="true">
        <third>abc</third>
        <third>def</third>
    </second>
    <fourth>
        <fifth x="1">hij</fifth>
        <fifth>klm</fifth>
    </fourth>
    <sixth>qrs</sixth>
</first>

2。

<?xml version="1.0" encoding="UTF-8" ?>
<first y="2">
    <second param="123" second="false">
        <third>asd</third>
        <third>def</third>
    </second>
    <fourth>
        <fifth y="2">tuv</fifth>
        <fifth>wxy</fifth>
    </fourth>
    <sixth>678</sixth>
    <sixth>910</sixth>
</first>

我希望第一个文件是首选的,以便第二个文件合并到第一个文件中。元素不应重复。

预期输出:

<?xml version="1.0" encoding="UTF-8" ?>
<first x="1" y="2">
    <second param="wt" second="true">
        <third>abc</third>
        <third>def</third>
        <third>asd</third>
    </second>
    <fourth>
        <fifth x="1">hij</fifth>
        <fifth>klm</fifth>
        <fifth y="2">tuv</fifth>
        <fifth>wxy</fifth>
    </fourth>
    <sixth>qrs</sixth>
    <sixth>678</sixth>
    <sixth>910</sixth>
</first>

我得到的输出:

<?xml version="1.0" encoding="windows-1252"?><first y="2" x="1">
<second param="wt" second="true">
                            2. Value: abc
                            2. Current: third
                            2. First: third
                            2. Second: third</second>
<fourth param="wt" second="true">
                            2. Value: hij
                            2. Current: fifth
                            2. First: third
                            2. Second: third</fourth>
<sixth param="wt" second="true">
                            2. Value: 
                            2. Current: 
                            2. First: third
                            2. Second: third</sixth>
</first>

我不知道如何同时沿着两棵树跑,所以我可以复制元素。有人有想法吗? 我只能使用Apaches XALAN。我使用最新的版本2.7.2。

编辑:由于存在误解。转换必须适用于类似的XML文件,这是个大问题。

2 个答案:

答案 0 :(得分:1)

<xsl:output method="xml" indent="yes"/>
    <xsl:variable name="doc" select="doc('merge2.xml')"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="first">
        <xsl:copy>
        <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="$doc/first/@*"/>
            <xsl:apply-templates/>

        </xsl:copy>
    </xsl:template>
    <xsl:template match="second">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates/>
            <xsl:copy-of select="$doc/first/second/third[ not(.= current()/third)]"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="fourth">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates/>
            <xsl:copy-of select="$doc/first/fourth/fifth[ not(.= current()/fifth)]"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="sixth">
        <xsl:copy>

            <xsl:apply-templates/>

        </xsl:copy>
        <xsl:copy-of select="$doc/first/sixth[ not(.= current()/sixth)]"/>
    </xsl:template>
CHeck it

答案 1 :(得分:0)

我尝试通过调整元素节点的比较来理解您的要求和http://web.archive.org/web/20160502222322/http://www2.informatik.hu-berlin.de/~obecker/XSLT/#merge中解释并在http://web.archive.org/web/20160502194427/http://www2.informatik.hu-berlin.de/~obecker/XSLT/merge/merge.xslt.html中展示的Oliver Becker实现的部分(如果名称,没有子元素的元素被认为是等效的,如果名称空间和内容匹配,则具有复杂内容的元素(即元素子元素)如果名称和名称空间相同,则认为是等效的,并且通过调整元素的合并以从第二个文档复制属性($node2/@*首先复制为我想您想在第一个文档中具有属性以具有首选项。

我已经在https://xsltfiddle.liberty-development.net/nc4NzQq上使用Saxon 9.8 HE在线进行了测试,但是只能以XSLT 2或3处理器的可执行方式共享代码和结果,因此我可以轻松地内联第二个文档。所以我有

<!--
   Merging two XML files
   Version 1.6
   LGPL (c) Oliver Becker, 2002-07-05
   obecker@informatik.hu-berlin.de
-->
<xslt:transform xmlns:xslt="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://informatik.hu-berlin.de/merge" version="1.0" exclude-result-prefixes="m">


<!-- Normalize the contents of text, comment, and processing-instruction
     nodes before comparing?
     Default: yes -->
<xslt:param name="normalize" select="'yes'" />

<!-- Don't merge elements with this (qualified) name -->
<xslt:param name="dontmerge" />

<!-- If set to true, text nodes in file1 will be replaced -->
<xslt:param name="replace" select="false()" />

<!-- Variant 1: Source document looks like
     <?xml version="1.0"?>
     <merge xmlns="http://informatik.hu-berlin.de/merge">
        <file1>file1.xml</file1>
        <file2>file2.xml</file2>
     </merge>         
     The transformation sheet merges file1.xml and file2.xml.
-->
<xslt:template match="m:merge">
   <xslt:variable name="file1" select="string(m:file1)" />
   <xslt:variable name="file2" select="string(m:file2)" />
   <xslt:message>
      <xslt:text />Merging '<xslt:value-of select="$file1" />
      <xslt:text />' and '<xslt:value-of select="$file2" />'<xslt:text />
   </xslt:message>
   <xslt:if test="$file1='' or $file2=''">
      <xslt:message terminate="yes">
         <xslt:text>No files to merge specified</xslt:text>
      </xslt:message>
   </xslt:if>
   <xslt:call-template name="m:merge">
      <xslt:with-param name="nodes1" select="document($file1,/*)/node()" />
      <xslt:with-param name="nodes2" select="document($file2,/*)/node()" />
   </xslt:call-template>
</xslt:template>


<!-- Variant 2:
     The transformation sheet merges the source document with the
     document provided by the parameter "with".
-->
<xslt:param name="with" />

<!-- for testing I inline the second document -->
<xslt:param name="with-doc">
<first y="2">
    <second param="123" second="false">
        <third>asd</third>
        <third>def</third>
    </second>
    <fourth>
        <fifth y="2">tuv</fifth>
        <fifth>wxy</fifth>
    </fourth>
    <sixth>678</sixth>
    <sixth>910</sixth>
</first>    
</xslt:param>

<xslt:template match="*">
   <xslt:message>
      <xslt:text />Merging input with '<xslt:value-of select="$with" />
      <xslt:text>'</xslt:text>
   </xslt:message>
   <!--<xslt:if test="string($with)=''">
      <xslt:message terminate="yes">
         <xslt:text>No input file specified (parameter 'with')</xslt:text>
      </xslt:message>
   </xslt:if>-->

   <xslt:call-template name="m:merge">
      <xslt:with-param name="nodes1" select="/node()" />
      <!-- <xslt:with-param name="nodes2" select="document($with,/*)/node()" /> -->
      <xslt:with-param name="nodes2" select="$with-doc/node()" />
   </xslt:call-template>
</xslt:template>


<!-- ============================================================== -->

<!-- The "merge" template -->
<xslt:template name="m:merge">
   <xslt:param name="nodes1" />
   <xslt:param name="nodes2" />

   <xslt:choose>
      <!-- Is $nodes1 resp. $nodes2 empty? -->
      <xslt:when test="count($nodes1)=0">
         <xslt:copy-of select="$nodes2" />
      </xslt:when>
      <xslt:when test="count($nodes2)=0">
         <xslt:copy-of select="$nodes1" />
      </xslt:when>

      <xslt:otherwise>
         <!-- Split $nodes1 and $nodes2 -->
         <xslt:variable name="first1" select="$nodes1[1]" />
         <xslt:variable name="rest1" select="$nodes1[position()!=1]" />
         <xslt:variable name="first2" select="$nodes2[1]" />
         <xslt:variable name="rest2" select="$nodes2[position()!=1]" />
         <!-- Determine type of node $first1 -->
         <xslt:variable name="type1">
            <xslt:apply-templates mode="m:detect-type" select="$first1" />
         </xslt:variable>

         <!-- Compare $first1 and $first2 -->
         <xslt:variable name="diff-first">
            <xslt:call-template name="m:compare-nodes">
               <xslt:with-param name="node1" select="$first1" />
               <xslt:with-param name="node2" select="$first2" />
            </xslt:call-template>
         </xslt:variable>

         <xslt:choose>
            <!-- $first1 != $first2 -->
            <xslt:when test="$diff-first='!'">
               <!-- Compare $first1 and $rest2 -->
               <xslt:variable name="diff-rest">
                  <xslt:for-each select="$rest2">
                     <xslt:call-template name="m:compare-nodes">
                        <xslt:with-param name="node1" select="$first1" />
                        <xslt:with-param name="node2" select="." />
                     </xslt:call-template>
                  </xslt:for-each>
               </xslt:variable>

               <xslt:choose>
                  <!-- $first1 is in $rest2 and 
                       $first1 is *not* an empty text node  -->
                  <xslt:when test="contains($diff-rest,'=') and not($type1='text' and normalize-space($first1)='')">
                     <!-- determine position of $first1 in $nodes2
                          and copy all preceding nodes of $nodes2 -->
                     <xslt:variable name="pos" select="string-length(substring-before( $diff-rest,'=')) + 2" />
                     <xslt:copy-of select="$nodes2[position() &lt; $pos]" />
                     <!-- merge $first1 with its equivalent node -->
                     <xslt:choose>
                        <!-- Elements: merge -->
                        <xslt:when test="$type1='element'">
                           <xslt:element name="{name($first1)}" namespace="{namespace-uri($first1)}">
                              <xslt:copy-of select="$first1/namespace::*" />
                              <xslt:copy-of select="$first2/namespace::*" />
                              <xslt:copy-of select="$first2/@*"/>
                              <xslt:copy-of select="$first1/@*" />
                              <xslt:call-template name="m:merge">
                                 <xslt:with-param name="nodes1" select="$first1/node()" />
                                 <xslt:with-param name="nodes2" select="$nodes2[position()=$pos]/node()" />
                              </xslt:call-template>
                           </xslt:element>
                        </xslt:when>
                        <!-- Other: copy -->
                        <xslt:otherwise>
                           <xslt:copy-of select="$first1" />
                        </xslt:otherwise>
                     </xslt:choose>

                     <!-- Merge $rest1 and rest of $nodes2 -->
                     <xslt:call-template name="m:merge">
                        <xslt:with-param name="nodes1" select="$rest1" />
                        <xslt:with-param name="nodes2" select="$nodes2[position() &gt; $pos]" />
                     </xslt:call-template>
                  </xslt:when>

                  <!-- $first1 is a text node and replace mode was
                       activated -->
                  <xslt:when test="$type1='text' and $replace">
                     <xslt:call-template name="m:merge">
                        <xslt:with-param name="nodes1" select="$rest1" />
                        <xslt:with-param name="nodes2" select="$nodes2" />
                     </xslt:call-template>
                  </xslt:when>

                  <!-- else: $first1 is not in $rest2 or
                       $first1 is an empty text node -->
                  <xslt:otherwise>
                     <xslt:copy-of select="$first1" />
                     <xslt:call-template name="m:merge">
                        <xslt:with-param name="nodes1" select="$rest1" />
                        <xslt:with-param name="nodes2" select="$nodes2" />
                     </xslt:call-template>
                  </xslt:otherwise>
               </xslt:choose>
            </xslt:when>

            <!-- else: $first1 = $first2 -->
            <xslt:otherwise>
               <xslt:choose>
                  <!-- Elements: merge -->
                  <xslt:when test="$type1='element'">
                     <xslt:element name="{name($first1)}" namespace="{namespace-uri($first1)}">
                        <xslt:copy-of select="$first2/namespace::*" />
                        <xslt:copy-of select="$first1/namespace::*" />
                        <xslt:copy-of select="$first2/@*" />
                        <xslt:copy-of select="$first1/@*" />
                        <xslt:call-template name="m:merge">
                           <xslt:with-param name="nodes1" select="$first1/node()" />
                           <xslt:with-param name="nodes2" select="$first2/node()" />
                        </xslt:call-template>
                     </xslt:element>
                  </xslt:when>
                  <!-- Other: copy -->
                  <xslt:otherwise>
                     <xslt:copy-of select="$first1" />
                  </xslt:otherwise>
               </xslt:choose>

               <!-- Merge $rest1 and $rest2 -->
               <xslt:call-template name="m:merge">
                  <xslt:with-param name="nodes1" select="$rest1" />
                  <xslt:with-param name="nodes2" select="$rest2" />
               </xslt:call-template>
            </xslt:otherwise>
         </xslt:choose>
      </xslt:otherwise>
   </xslt:choose>
</xslt:template>


<!-- Comparing single nodes: 
     if $node1 and $node2 are equivalent then the template creates a 
     text node "=" otherwise a text node "!" -->
<xslt:template name="m:compare-nodes">
   <xslt:param name="node1" />
   <xslt:param name="node2" />
   <xslt:variable name="type1">
      <xslt:apply-templates mode="m:detect-type" select="$node1" />
   </xslt:variable>
   <xslt:variable name="type2">
      <xslt:apply-templates mode="m:detect-type" select="$node2" />
   </xslt:variable>

   <xslt:choose>
      <!-- Are $node1 and $node2 complex element nodes with the same name? -->
      <xslt:when test="$type1='element' and $type2='element' and $node1/* and $node2/* and local-name($node1)=local-name($node2) and namespace-uri($node1)=namespace-uri($node2) and name($node1)!=$dontmerge and name($node2)!=$dontmerge">
          <xslt:text>=</xslt:text>
      </xslt:when>

      <!-- Are $node1 and $node2 simple elements with the same name and same content -->
      <xslt:when test="$type1='element' and $type2='element' and not($node1/*) and not($node2/*) and local-name($node1)=local-name($node2) and namespace-uri($node1)=namespace-uri($node2) and $node1 = $node2 and name($node1)!=$dontmerge and name($node2)!=$dontmerge">
          <xslt:text>=</xslt:text>
      </xslt:when>

      <!-- Other nodes: test for the same type and content -->
      <xslt:when test="$type1!='element' and $type1=$type2 and name($node1)=name($node2) and ($node1=$node2 or ($normalize='yes' and normalize-space($node1)= normalize-space($node2)))">=</xslt:when>

      <!-- Otherwise: different node types or different name/content -->
      <xslt:otherwise>!</xslt:otherwise>
   </xslt:choose>
</xslt:template>


<!-- Type detection, thanks to M. H. Kay -->
<xslt:template match="*" mode="m:detect-type">element</xslt:template>
<xslt:template match="text()" mode="m:detect-type">text</xslt:template>
<xslt:template match="comment()" mode="m:detect-type">comment</xslt:template>
<xslt:template match="processing-instruction()" mode="m:detect-type">pi</xslt:template>

</xslt:transform>

然后给出结果

<first y="2" x="1">
    <second param="wt" second="true">
        <third>abc</third>
        <third>asd</third><third>def</third>
    </second>
    <fourth>
        <fifth x="1">hij</fifth>
        <fifth>klm</fifth>
    <fifth y="2">tuv</fifth><fifth>wxy</fifth></fourth>
    <sixth>qrs</sixth>
<sixth>678</sixth><sixth>910</sixth></first>

这与您想要的结果很接近,我不确定您是否可以混合使用内容(例如,文本和元素子元素混合使用),如果不能,我认为可以使用xsl:strip-spacexsl:output indent="yes"https://xsltfiddle.liberty-development.net/nc4NzQq/1中,为您提供清晰的结果

<first y="2" x="1">
   <second param="wt" second="true">
      <third>abc</third>
      <third>asd</third>
      <third>def</third>
   </second>
   <fourth>
      <fifth x="1">hij</fifth>
      <fifth>klm</fifth>
      <fifth y="2">tuv</fifth>
      <fifth>wxy</fifth>
   </fourth>
   <sixth>qrs</sixth>
   <sixth>678</sixth>
   <sixth>910</sixth>
</first>

另一方面,由于我有第二个示例内联代码,其中的空格被删除,因此可以假设在使用document函数然后在{内联样本上{3}}和xml:space="preserve",结果

<first y="2" xml:space="preserve" x="1">
    <second param="wt" second="true">
        <third>abc</third>
        <third>asd</third>
        <third>def</third>
    </second>
    <fourth>
        <fifth x="1">hij</fifth>
        <fifth>klm</fifth>
    <fifth y="2">tuv</fifth>
        <fifth>wxy</fifth>
    </fourth>
    <sixth>qrs</sixth>
<sixth>678</sixth>
    <sixth>910</sixth>
</first>

看起来也很有希望。因此,尝试像原始版本那样分别使用with参数和document函数来对其进行修改,至少对于已显示的两个示例,您可能会得到所需的结果。很难说这是否是通用解决方案,因为我认为整个合并的想法在很大程度上取决于如何精确比较节点的明确规范。