Question

I have two XML-files that I currently are merging in a new result XML-file.

First file:

<T>
    <T1>AA</T1>
    <T2>1</T2>
</T>    
<T>
    <T1>BB</T1>
    <T2>1</T2>
</T>
<T>
    <T1>AC</T1>
    <T2>1</T2>
</T>

Second file:

<T>
    <T1>BB</T1>
    <T2>3</T2>
</T>
<T>
    <T1>AB</T1>
    <T2>3</T2>
</T>

I want the result to be:

<T>
    <T1>AA</T1>
    <T2>1</T2>
</T>    
<T>
    <T1>BB</T1>
    <T2>3</T2>
</T>
<T>
    <T1>AC</T1>
    <T2>1</T2>
</T>
<T>
    <T1>AB</T1>
    <T2>3</T2>
</T>

Ie. I want to add the parent nodes <T> to the result file if they don't already exist(depending on <T1>) in my first file, but replace if they do exist. I want the solution to be generic, the solution should work for a "new" second file. Can anyone assist, this is what I currently have:

<xsl:output indent="yes"/>

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

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

Edit:

I have two XML-files that I currently are merging in a new result XML-file. First file:

<TT>
<T>
    <T1>AA</T1>
    <T2>1</T2>
</T>    
<T>
    <T1>BB</T1>
    <T2>1</T2>
    <T3>1</T3>
</T>
</TT>

Second file:

<TT>
<T>
    <T1>AA</T1>
    <T2>3</T2>
    <T3>3</T3>
</T>

<T>
    <T1>BB</T1>
    <T3>3</T3>
</T>
</TT>

I want the result to be:

<TT>
<T>
    <T1>AA</T1>
    <T2>3</T2>
    <T3>3</T3>
</T>    
<T>
    <T1>BB</T1>
    <T2>1</T2>
    <T3>3</T3>
</T>
</TT>

In other words the <T1> will be the deciding factor. Answers received so far has been great I just realized that my question actually was more complex than I initially thought.

Was it helpful?

Solution

Given two valid XML files:

file1.xml

<input>
    <T>
        <T1>AA</T1>
        <T2>1</T2>
    </T>    
    <T>
        <T1>BB</T1>
        <T2>1</T2>
    </T>
    <T>
        <T1>AC</T1>
        <T2>1</T2>
    </T>
</input>

file2.xml

<input>
    <T>
        <T1>BB</T1>
        <T2>2</T2>
    </T>
    <T>
        <T1>AB</T1>
        <T2>3</T2>
    </T>
</input>

when the following stylesheet is applied to file1.xml:

XSLT 1.0

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

<xsl:variable name="file2" select="document('file2.xml')" />
<xsl:variable name="localT1s" select="/input/T/T1" />

<xsl:template match="/input">
    <result>
        <xsl:apply-templates select="T"/>
        <xsl:apply-templates select="$file2/input/T[not(T1=$localT1s)]"/>
    </result>
</xsl:template>

<xsl:template match="T">
    <xsl:copy>
        <xsl:copy-of select="T1"/>
        <T2>
            <xsl:choose>
                <xsl:when test="$file2/input/T[T1=current()/T1]">
                    <xsl:value-of select="$file2/input/T[T1=current()/T1]/T2"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="T2"/>
                </xsl:otherwise>
            </xsl:choose>
        </T2>   
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

the result will be:

<?xml version="1.0" encoding="UTF-8"?>
<result>
   <T>
      <T1>AA</T1>
      <T2>1</T2>
   </T>
   <T>
      <T1>BB</T1>
      <T2>2</T2>
   </T>
   <T>
      <T1>AC</T1>
      <T2>1</T2>
   </T>
   <T>
      <T1>AB</T1>
      <T2>3</T2>
   </T>
</result>

Edit

In response to your amended question:
It's not quite clear what do you know about the names of the <T#> elements in the input files. If (as I assumed) nothing, then this gets rather complex:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="file2" select="document('file2.xml')" />

<xsl:template match="/TT">
<xsl:variable name="localT1s" select="T/T1" />
    <xsl:copy>
        <xsl:apply-templates select="T"/>
        <xsl:apply-templates select="$file2/TT/T[not(T1=$localT1s)]" mode="import"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="T">
<xsl:variable name="elemNames">
    <xsl:for-each select="*">
        <name><xsl:value-of select="name()"/></name>
    </xsl:for-each>
</xsl:variable> 
    <xsl:copy>
        <xsl:apply-templates select="*"/>
        <xsl:apply-templates select="$file2/TT/T[T1=current()/T1]/*[not(name()=exsl:node-set($elemNames)/name)]" mode="import"/>
    </xsl:copy>
</xsl:template>        

<xsl:template match="T/*">
    <xsl:variable name="T1" select="../T1" />
    <xsl:variable name="Tx" select="name()" />
    <xsl:copy>
        <xsl:choose>
            <xsl:when test="$file2/TT/T[T1=$T1]/*[name()=$Tx]">
                <xsl:value-of select="$file2/TT/T[T1=$T1]/*[name()=$Tx]"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="."/>
                </xsl:otherwise>
        </xsl:choose>
    </xsl:copy>
</xsl:template>

<xsl:template match="*" mode="import">
    <xsl:copy-of select="."/>
</xsl:template> 

</xsl:stylesheet>

I tested the above with these two files:

file1.xml

<TT>
    <T>
        <T1>A</T1>
        <T2>a2</T2>
    </T>    
    <T>
        <T1>B</T1>
        <T2>b2</T2>
        <T3>b3</T3>
    </T>
</TT>

file2.xml

<TT>
    <T>
        <T1>A</T1>
        <T2>A2</T2>
        <T3>A3</T3>
    </T>
    <T>
        <T1>B</T1>
        <T3>B3</T3>
    </T>
    <T>
        <T1>C</T1>
        <T2>C2</T2>
    </T>
</TT>

and received this result:

<?xml version="1.0" encoding="UTF-8"?>
<TT>
   <T>
      <T1>A</T1>
      <T2>A2</T2>
      <T3>A3</T3>
   </T>
   <T>
      <T1>B</T1>
      <T2>b2</T2>
      <T3>B3</T3>
   </T>
   <T>
      <T1>C</T1>
      <T2>C2</T2>
   </T>
</TT>

OTHER TIPS

With XSLT 2.0 (and then using an XSLT 2.0 processor like Saxon 9) you could nicely cross-reference between the two documents:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">

<xsl:param name="doc2-url" select="'file2.xml'"/>
<xsl:variable name="doc2" select="doc($doc2-url)"/>

<xsl:variable name="main-doc" select="/"/>

<xsl:output indent="yes"/>

<xsl:key name="by-T1" match="T" use="T1"/>

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

<xsl:template match="*[T]">
    <xsl:copy>
        <xsl:apply-templates select="T, $doc2//T[not(key('by-T1', T1, $main-doc))]"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="T[$main-doc is /][key('by-T1', T1, $doc2)]/T2">
  <xsl:copy-of select="key('by-T1', ../T1, $doc2)/T2"/>
</xsl:template>

</xsl:stylesheet>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top