RE: [xsl] Merging Data

Subject: RE: [xsl] Merging Data
From: "Kevin Bird (Matrix)" <kevin.bird@xxxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 6 Aug 2004 17:20:32 +0100
Hi Michael

I ran your XSLT 2.0 solution against my data and produced the following
output. The text value of <supp-desc> is also appearing in <supp-price>
(which is not required). I've had a play around with the stylesheet but I'm
stabbing around in the dark.

Any suggestions would be greatly appreciated.


<?xml version="1.0" encoding="UTF-8"?>
<supp>
   <supp-desc>Half Board</supp-desc>
   <supp-price source="filea">Half Board#10</supp-price>
   <supp-price source="fileb">Half Board#20</supp-price>
</supp>
<supp>
   <supp-desc>All Inclusive</supp-desc>
   <supp-price source="filea">All Inclusive#20</supp-price>
   <supp-price source="fileb">All Inclusive#40</supp-price>
</supp>
<supp>
   <supp-desc>Sea View</supp-desc>
   <supp-price source="filea">Sea View#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">Balcony#5</supp-price>
</supp>

Many thanks.

--
Kevin Bird



-----Original Message-----
From: Michael Kay [mailto:mhk@xxxxxxxxx]
Sent: 05 August 2004 18:44
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] Merging Data

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