0

file1.xml

<config>
 <state version="10">
  <root value="100" group="5">
     <leaf number = "2"/>
  </root>
  <root value="101" group="6">
     <leaf number = "3"/>
  </root>
 </state>
</config>

file2.xml

<config>
 <state version="10">
  <root value="100" group="5">
     <leaf number = "6"/>
  </root>
  <root value="101" group="6">
     <leaf number = "4"/>
  </root>
 </state>
</config>

output.xml

<config>
 <state version="10">
  <root value="100" group="5">
     <leaf number = "2"/>
     <leaf number = "6"/>
  </root>
  <root value="101" group="6">
     <leaf number = "3"/>
     <leaf number = "4"/>
  </root>
 </state>
</config>

I tried writing an XSLT based on the answer here: How to merge 2 XML files with common nodes using XSLT?

but I do not know how to compare based on attribute values (in this case, attributes value and group of 'root' tag? Can someone please help?

1 Answer 1

1

This should do it:

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

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

  <xsl:template match="root">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" />
      <xsl:apply-templates
        select="document('file2.xml')
              /config/state[@version = current()/../@version]
                     /root[@value = current()/@value and
                           @group = current()/@group]/*" />
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

When run with your file1.xml example as the main input, this produces:

<config>
  <state version="10">
    <root value="100" group="5">
      <leaf number="2" />
      <leaf number="6" />
    </root>
    <root value="101" group="6">
      <leaf number="3" />
      <leaf number="4" />
    </root>
  </state>
</config>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.