如何合并使用XSLT公共节点2个XML文件?(How to merge 2 XML files wi

2019-07-19 05:57发布

file1.xml:

<config>
  <version>
     <input00 version ="1"/>
  </version>
</config>

file2.xml:

 <config>
  <version>
     <input01 version ="2"/>
  </version>
</config>

与Output.xml:

<config>
  <version>
     <input00 version ="1"/>
     <input01 version ="2"/>
  </version>
</config>

这是我试过的生成样式表:merge.xslt

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
</xsl:template>


<xsl:template match="/config">
     <xsl:copy>
        <xsl:apply-templates select="config"/>
        <xsl:apply-templates select="document('./file1.xml')/config/version" />
     </xsl:copy>
</xsl:template>


</xsl:stylesheet>

&我这是怎么运行的XSLT处理器:

 $xsltproc merge.xslt file2.xml

这是我得到的:

<?xml version="1.0"?>
<config/>

请帮忙。

Answer 1:

试试这个XSLT:

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

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="version">
    <xsl:copy>
      <xsl:apply-templates select="*"/>
      <xsl:apply-templates select="document('file1.xml')/config/version/*" />
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>


文章来源: How to merge 2 XML files with common nodes using XSLT?
标签: xml xslt xpath