RE: [xsl] Merging Data

Subject: RE: [xsl] Merging Data
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Wed, 11 Aug 2004 07:21:58 -0700 (PDT)
Hi Kevin,
  A pure XSLT 1.0 solution struck to me(i.e. without
extension functions), and I thought of sharing it with
you.

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

<xsl:variable name="filea"
select="document('filea.xml')" /> 
<xsl:variable name="fileb"
select="document('fileb.xml')" />
	
<xsl:template match="/">
  <supplements>
    <!--finding supp-desc tags common to both files
-->
    <xsl:for-each select="supplements/supp">
      <xsl:variable name="desc-a" select="supp-desc"
/>
      <xsl:variable name="price-a" select="supp-price"
/>
      <xsl:for-each select="$fileb/supplements/supp">
        <xsl:if test="supp-desc = $desc-a">
          <supp>
            <supp-desc><xsl:value-of
select="supp-desc" /></supp-desc>
            <supp-price source="filea"><xsl:value-of
select="$price-a" /></supp-price>
            <supp-price source="fileb"><xsl:value-of
select="supp-price" /></supp-price>
          </supp>
        </xsl:if>
      </xsl:for-each>
    </xsl:for-each>

    <!--finding supp-desc tags existing only in
filea.xml -->
    <xsl:for-each select="supplements/supp">
      <xsl:variable name="desc-a" select="supp-desc"
/>
      <xsl:variable name="price-a" select="supp-price"
/>
      <xsl:variable name="temp">
        <xsl:for-each
select="$fileb/supplements/supp">                     
          <xsl:if test="supp-desc = $desc-a">
            1
          </xsl:if>
        </xsl:for-each>                    
      </xsl:variable>
      <xsl:if test="not(contains($temp, '1'))">
        <supp>
          <supp-desc><xsl:value-of select="$desc-a"
/></supp-desc>
          <supp-price source="filea"><xsl:value-of
select="$price-a" /></supp-price>
          <supp-price source="fileb">-</supp-price>
        </supp>
      </xsl:if>
    </xsl:for-each>

    <!--finding supp-desc tags existing only in
fileb.xml -->
    <xsl:for-each select="$fileb/supplements/supp">
      <xsl:variable name="desc-b" select="supp-desc"
/>
      <xsl:variable name="price-b" select="supp-price"
/>
      <xsl:variable name="temp">
        <xsl:for-each
select="$filea/supplements/supp">                     
          <xsl:if test="supp-desc = $desc-b">
            1
          </xsl:if>
        </xsl:for-each>                    
      </xsl:variable>
      <xsl:if test="not(contains($temp, '1'))"> 
        <supp>
          <supp-desc><xsl:value-of select="$desc-b"
/></supp-desc>
	  <supp-price source="filea">-</supp-price>
	  <supp-price source="fileb"><xsl:value-of
select="$price-b" /></supp-price>
	</supp>
      </xsl:if>
    </xsl:for-each>
            
</supplements>	  	  	  
</xsl:template>
	
</xsl:stylesheet>

Regards,
Mukul

--- Kevin Bird <kevin.bird@xxxxxxxxxxxxxxxxxxxxxxx>
wrote:

> Hi Mukul
> 
> Many thanks for taking the time to supply the
> version 1.0 solution. I
> changed the extension calls to Saxon (my preference)
> and it works fine.
> 
> Regards.
> 
> --
> Kevin Bird
> 
> 
> -----Original Message-----
> From: Mukul Gandhi [mailto:mukul_gandhi@xxxxxxxxx] 
> Sent: 07 August 2004 07:44
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re: [xsl] Merging Data
> 
> Hi Kevin,
>   Please try this stylesheet. This is a XSLT 1.0
> solution and uses a nodeset extension function.
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> xmlns:xalan="http://xml.apache.org/xalan";
> exclude-result-prefixes="xalan">
> 
> <xsl:output method="xml"  indent="yes"/>
> 
> <xsl:variable name="filea"
> select="document('filea.xml')" />
> <xsl:variable name="fileb"
> select="document('fileb.xml')" />
> 
> <xsl:template match="/">
>   <supplements>
> 
>   <!--finding supp-desc tags common to both files
> -->
>   <xsl:for-each select="supplements/supp">
>     <xsl:variable name="desc-a" select="supp-desc"
> />
>     <xsl:variable name="price-a" select="supp-price"
> />
>     <xsl:for-each select="$fileb/supplements/supp">
>       <xsl:if test="supp-desc = $desc-a">
>        <supp>
>          <supp-desc><xsl:value-of select="supp-desc"
> /></supp-desc>
>          <supp-price source="filea"><xsl:value-of
> select="$price-a" /></supp-price>
>          <supp-price source="fileb"><xsl:value-of
> select="supp-price" /></supp-price>
>        </supp>
>       </xsl:if>
>     </xsl:for-each>
>   </xsl:for-each>
>   <!--finding supp-desc tags existing only in
> filea.xml -->
>   <xsl:for-each select="supplements/supp">
>     <xsl:variable name="desc-a" select="supp-desc"
> />
>     <xsl:variable name="price-a" select="supp-price"
> />
>     <xsl:variable name="rtf">
>       <xsl:for-each
> select="$fileb/supplements/supp">
> 
>         <xsl:if test="supp-desc = $desc-a">
>           <x/>
>         </xsl:if>
>       </xsl:for-each>
>      </xsl:variable>
>      <xsl:if test="count(xalan:nodeset($rtf)/x) =
> 0">
>         <supp>
>           <supp-desc><xsl:value-of select="$desc-a"
> /></supp-desc>
>           <supp-price source="filea"><xsl:value-of
> select="$price-a" /></supp-price>
> 	  <supp-price source="fileb">-</supp-price>
> 	</supp>
>       </xsl:if>
>    </xsl:for-each>
>    <!--finding supp-desc tags existing only in
> fileb.xml -->
>    <xsl:for-each select="$fileb/supplements/supp">
>      <xsl:variable name="desc-b" select="supp-desc"
> />
>      <xsl:variable name="price-b"
> select="supp-price"
> />
>      <xsl:variable name="rtf">
>        <xsl:for-each
> select="$filea/supplements/supp">
> 
>           <xsl:if test="supp-desc = $desc-b">
>             <x/>
>           </xsl:if>
>        </xsl:for-each>
>      </xsl:variable>
>      <xsl:if test="count(xalan:nodeset($rtf)/x) =
> 0">
>        <supp>
>          <supp-desc><xsl:value-of select="$desc-b"
> /></supp-desc>
>          <supp-price source="filea">-</supp-price>
>          <supp-price source="fileb"><xsl:value-of
> select="$price-b" /></supp-price>
>        </supp>
>      </xsl:if>
>   </xsl:for-each>
> 
>   </supplements>
> </xsl:template>
> 
> </xsl:stylesheet>
> 
> Regards,
> Mukul
> 
> --- Kevin Bird <kevin.bird@xxxxxxxxxxxxxxxxxxxxxxx>
> wrote:
> 
> > 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




	
		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
http://promotions.yahoo.com/new_mail 

Current Thread