RE: [xsl] Merging Data

Subject: RE: [xsl] Merging Data
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Thu, 5 Aug 2004 18:43:31 +0100
It's a grouping problem, with the extra complication that you can't rely on
the relative document order of nodes from two different documents. XSLT 2.0
solution:

<xsl:variable name="list1" select="doc('1.xml')/*/supp"/>
<xsl:variable name="list2" select="doc('2.xml')/*/supp"/>

<xsl:for-each-group select="$list1, $list2"
                    group-by="supp-desc">
  <supp>
    <xsl:copy-of select="supp-desc"/>
    <supp-price source="filea">
      <xsl:value-of select="(current-group() intersect $list1, '-')[1]"/>
    </supp-price>
    <supp-price source="fileb">
      <xsl:value-of select="(current-group() intersect $list2, '-')[1]"/>
    </supp-price>
  </supp>
</xsl:for-each-group>

I will leave the XSLT 1.0 solution to someone with more time on their hands.
It's tricky because both grouping mechanisms (preceding-sibling and keys)
work only within a single document, so you have to use a two-phase approach.

Michael Kay

> -----Original Message-----
> From: Kevin Bird [mailto:kevin.bird@xxxxxxxxxxxxxxxxxxxxxxx]
> Sent: 05 August 2004 18:20
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Merging Data
>
> Hi Everyone
>
> I need to combine two sets of data (File A and File B). The
> structure is as follows (snippet):
>
> FILE A:
> <supplements>
> <supp>
> 	<supp-desc>Half Board</supp-desc>
> 	<supp-price>#10</supp-price>
> </supp>
> <supp>
> 	<supp-desc>All Inclusive</supp-desc>
> 	<supp-price>#20</supp-price>
> </supp>
> <supp>
> 	<supp-desc>Sea View</supp-desc>
> 	<supp-price>#3</supp-price>
> </supp>
> ...
> </supplements>
>
> FILE B:
> <supplements>
> <supp>
> 	<supp-desc>Half Board</supp-desc>
> 	<supp-price>#20</supp-price>
> </supp>
> <supp>
> 	<supp-desc>All Inclusive</supp-desc>
> 	<supp-price>#40</supp-price>
> </supp>
> <supp>
> 	<supp-desc>Balcony</supp-desc>
> 	<supp-price>#5</supp-price>
> </supp>
> ...
> </supplements>
>
> REQUIRED OUTPUT:
> <supplements>
> <supp>
> 	<supp-desc>Half Board</supp-desc>
> 	<supp-price source="filea">#10</supp-price>
> 	<supp-price source="fileb">#20</supp-price>
> </supp>
> <supp>
> 	<supp-desc>All Inclusive</supp-desc>
> 	<supp-price source="filea">#20</supp-price>
> 	<supp-price source="fileb">#40</supp-price>
> </supp>
> <supp>
> 	<supp-desc>Sea View</supp-desc>
> 	<supp-price source="filea">#3</supp-price>
> 	<supp-price source="fileb">-</supp-price></supp>
> <supp>
> 	<supp-desc>Balcony</supp-desc>
> 	<supp-price source="filea">-</supp-price>
> 	<supp-price source="fileb">#5</supp-price></supp>
> ...
> </supplements>
>
> DESCRIPTION:
> I need to compare <supp> nodes based on the text value of
> <supp-desc>. If the <supp> exists in both files then the
> <supp-price> node from File B is added underneath the
> <supp-price> node from File A (a "source" attribute is also
> added). If the <supp> exists in one file but not the other, a
> <supp-price> node with the text value of "-" is added. "Sea
> View" and "Balcony" are examples of <supp> being present in
> one file only.
>
> Any suggestions on how best to achieve the desired result
> will be greatly appreciated.
>
>
> --
> Kevin Bird

Current Thread