XSLT: A simple way to merge xml files

前端 未结 1 1011
说谎
说谎 2020-11-27 07:43

I have two xml files. I need to merge them together where the element \"myid\" matches between the two. Please have a look at these example files...

File1.xml:

相关标签:
1条回答
  • 2020-11-27 08:16

    I have been researching a bit and found a fairly similar question here: http://forums.tizag.com/showthread.php?p=76699

    Here is what I have come up with, this seems to be mostly working except that Firefox is not recognizing it as an xml file even though I have added the xml:output.

    File1.xml (note line two, referencing our transformation):

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <?xml-stylesheet type="text/xsl" href="merge.xsl"?>
    <catalog>
      <data>
        <title>Title1</title>
        <description>Description1</description>
        <myid>1</myid>
      </data>
    
      <data>
        <title>Title2</title>
        <description>Description2</description>
        <myid>2</myid>
      </data>
    </catalog>
    

    File2.xml:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <catalog>
      <data>
        <author>Author1</author>
        <date>12/34/5678</date>
        <myid>1</myid>
      </data>
    
      <data>
        <author>Author2</author>
        <date>87/65/4321</date>
        <myid>2</myid>
      </data>
    </catalog>
    

    merge.xsl:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes" />
      <xsl:variable name="with" select="'File2.xml'" />
    
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="scene">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()" />
          <xsl:variable name="info" select="document($with)/catalog/data[myid=current()/myid]/." />
          <xsl:for-each select="$info/*">
            <xsl:if test="name()!='myid'">
              <xsl:copy-of select="." />
            </xsl:if>
          </xsl:for-each>
        </xsl:copy>
      </xsl:template>
    </xsl:transform>
    

    Output xml when viewing File1.xml:

    <catalog>
      <data>
        <title>Title1</title>
        <description>Description1</description>
        <myid>1</myid>
        <author>Author1</author>
        <date>12/34/5678</date>
      </data>
    
      <data>
        <title>Title2</title>
        <description>Description2</description>
        <myid>2</myid>
        <author>Author2</author>
        <date>87/65/4321</date>
      </data>
    </catalog>
    
    0 讨论(0)
提交回复
热议问题