how can i merge these two xml files using xslt

余生颓废 提交于 2020-01-24 21:13:05

问题


Please i want to know how i can merge two xml files into one xml file using xslt. I have these both xml files that i want to merge into an xml file as shown in the expected output.I want to include the each node Hn from the second file to the corresponding block with same number in the file 1.

file1:

<Test>
  <R1>
    <Th1>
        here are some instruction.
    </Th1>
  </R1>

  <R2>
    <Th2>
        here are some instruction.
    </Th2>
  </R2>

  <R3>
    <Th3>
        here are some instruction.
    </Th3>
  </R3>
</Test>

file 2:

<test1>
  <H1>
    here are some instruction.
  </H1>

  <H2>
    here are some instruction.
  </H2>

  <H3>
    here are some instruction.
  </H3>
</test1>    

and here is the expected output:

<test2>
  <R1>
    <H1>
        here are some instruction.
    </H1>
    <Th1>
        here are some instruction.
    </Th1>
  </R1>

  <R2>
    <H2>
        here are some instruction.
    </H2>
    <Th2>
        here are some instruction.
    </Th2>
  </R2>

  <R3>
    <H3>
        here are some instruction.
    </H3>
    <Th3>
        here are some instruction.
    </Th3>
  </R3>

</test2>

Thanks for your help.


回答1:


I. This XSLT 1.0 transformation:

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

 <xsl:variable name="vDigits" select="'0123456789'"/>
 <xsl:variable name="vDoc2" select="document('file:///c:/temp/delete/file2.xml')"/> 

 <xsl:template match="/*">
     <test2><xsl:apply-templates/></test2>
 </xsl:template>

 <xsl:template match="/*/*">
  <xsl:copy>
   <xsl:text>&#xA;</xsl:text>
   <xsl:copy-of select=
    "$vDoc2/*/*
      [translate(name(),translate(name(),$vDigits,''),'')
      =
       translate(name(current()),translate(name(current()),$vDigits,''),'')
      ]"/>
    <xsl:copy-of select="node()"/>
   </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

When applied on the first XML document (file1.xml):

<Test>
  <R1>
    <Th1>
        here are some instruction.
    </Th1>
  </R1>

  <R2>
    <Th2>
        here are some instruction.
    </Th2>
  </R2>

  <R3>
    <Th3>
        here are some instruction.
    </Th3>
  </R3>
</Test>

and having the second XML document reside at c:\temp\delete\file2.xml:

<test1>
  <H1>
    here are some instruction.
  </H1>

  <H2>
    here are some instruction.
  </H2>

  <H3>
    here are some instruction.
  </H3>
</test1>

produces the wanted, correct result:

<test2>
  <R1>
<H1>
    here are some instruction.
  </H1>
    <Th1>
        here are some instruction.
    </Th1>
  </R1>

  <R2>
<H2>
    here are some instruction.
  </H2>
    <Th2>
        here are some instruction.
    </Th2>
  </R2>

  <R3>
<H3>
    here are some instruction.
  </H3>
    <Th3>
        here are some instruction.
    </Th3>
  </R3>
</test2>

Explanation:

Proper use of the "double-translate" method first demoed by Michael Kay.


II. XSLT 2.0 solution:

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

 <xsl:variable name="vDigits" select="'0123456789'"/>
 <xsl:variable name="vDoc2" select="document('file:///c:/temp/delete/file2.xml')"/> 

 <xsl:template match="/*">
     <test2><xsl:apply-templates/></test2>
 </xsl:template>

 <xsl:template match="/*/*">
  <xsl:if test="not(matches(name(), '^.+\d+$'))">
    <xsl:message terminate="yes">
     Element Name doesn't end with number: <xsl:sequence select="name()"/>
    </xsl:message>
  </xsl:if>
  <xsl:copy>
   <xsl:text>&#xA;</xsl:text>
   <xsl:copy-of select=
    "$vDoc2/*/*
      [replace(name(),'^.+(\d+)$', '$1')
      =
       replace(name(current()),'^.+(\d+)$', '$1')
      ]"/>
    <xsl:copy-of select="node()"/>
   </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

Explanation:

Proper use of the standard XPath 2.0 functions matches() and replace()



来源:https://stackoverflow.com/questions/12423701/how-can-i-merge-these-two-xml-files-using-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!