Merging two XML source documents with XSLT

前端 未结 1 1570
被撕碎了的回忆
被撕碎了的回忆 2021-01-22 23:35

XML file one could have 1000 - 6000 forms; XML file two could have one to 100 or more. I want to replace any identical form in file one with file two. If it exists in file 2 b

相关标签:
1条回答
  • 2021-01-23 00:23

    If maintaining document order is not a priority:

    <xsl:variable name="forms1" select="document('forms1.xml')/Forms/Form" />
    <xsl:variable name="forms2" select="document('forms2.xml')/Forms/Form" />
    
    <xsl:variable name="merged" select="
      $forms1[not(@name = $forms2/@name)] | $forms2
    " />
    
    <xsl:template match="/">
      <xsl:apply-templates select="$merged" />
    </xsl:template>
    
    <xsl:template match="Form">
      <!-- for the sake of the example; you can use a more specialized template -->
      <xsl:copy-of select="." />
    </xsl:template>
    

    If maintaining document order is a priority for whatever reason…

    <xsl:template match="/">
      <!-- check values of file 1 sequentially, and replace them if needed -->
      <xsl:for-each select="$forms1">
        <xsl:variable name="this"  select="." />
        <xsl:variable name="newer" select="$forms2[@name = $this/@name]" />
        <xsl:choose>
          <xsl:when test="$newer">
            <xsl:apply-templates select="$newer" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="$this" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
      <!-- append any values from file 2 that are not in file 1 -->
      <xsl:apply-templates select="$forms2[not(@name = $forms1/@name)]" />
    </xsl:template>
    
    0 讨论(0)
提交回复
热议问题