【问题标题】:How to merge 2 XML files with common nodes using XSLT?如何使用 XSLT 将 2 个 XML 文件与公共节点合并?
【发布时间】:2013-01-19 21:37:40
【问题描述】:

file1.xml:

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

文件2.xml:

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

输出.xml:

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

这是我尝试生成样式表的方法: 合并.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/>

请帮忙。

【问题讨论】:

    标签: xml xslt xpath


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 2022-01-22
      相关资源
      最近更新 更多