RE: [xsl] Combining two node Sets into one

Subject: RE: [xsl] Combining two node Sets into one
From: "Williamson, Chris" <cwilliamson@xxxxxxxxx>
Date: Fri, 1 Apr 2005 12:47:01 -0500
Thanks Wendell.  

Everything seems to be working good. (except the sort.. But I think I can
figure that out) However, when I assign the value to a variable and try to
use it later in my stylesheet..as I'm sure you know.. you cannot use most
xpath expression on a tree frag. 

Not fully understanding how your example actually works I can't figure out
how to wrap it so that the data creates my desired table.  If they would
only let me redo the xml. *sigh*. 

Here is my desired table.. (-) is a refund type and the other is a disp
type.

Program    amount     processdate
Abc         500        1/1/2005 
	    (-)200       2/25/2005 


Def		1000	     3/5/2005
            500        5/5/2005
	    (-)500       7/9/2005

Thanks for the help.. If you have time can you also explain at a high level
how wrapping an expression in an xml tag works to generate the combined
nodes.. Ie..

<xsl:template match="Damount">
  <Tamount>  <!---xml tag
	<xsl:apply-templates/>
  </Tamount>
</xsl:template>

Thanks again.

Chris

-----Original Message-----
From: Wendell Piez [mailto:wapiez@xxxxxxxxxxxxxxxx] 
Sent: Thursday, March 31, 2005 5:38 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Combining two node Sets into one


Chris,

At 03:27 PM 3/31/2005, you wrote:
>Is there any possible way to combine two node sets into one single node 
>set (assign to variable?) and then add an extra childnode to the set 
>based on which parent node the new node came from.

Sure, this is a fairly straightforward "plain vanilla" XML->XML 
transformation, maybe with a few sprinkles....

You'll need at some point to select all the Disbs and Refunds elements 
together -- that's where you'll do your sorting by date, and also where 
you'll create the wrapper for the entire output:

<xsl:template match="/">
   <Trans>
     <xsl:apply-templates select="//Disbs | //Refunds">
       <xsl:sort select="DisbDetail/Ddate | RefDetail/Rdate"/>
     </xsl:apply-templates>
   </Trans>

Now you need templates to match the Disbs and Refunds elements, and for 
their descendants in turn (which will map to your new elements). I'll just 
show you a couple of them:

<xsl:template match="Disbs">
   <xsl:apply-templates/>
   <!-- nothing to be done here except select and process our
        children, which will map -->
</xsl:template>

<xsl:template match="DisbDetail">
   <!-- maps to TranDetail -->
   <TranDetail>
     <!-- but here we need to announce our type: -->
     <Ttype>dis</Ttype>
     <xsl:apply-templates/>
   <!-- descends another level -->
   </TranDetail>
</xsl:template>

<xsl:template match="Damount">
   <Tamount>
     <xsl:apply-templates/>
   </Tamount>
</xsl:template>

and so forth.

Once you've got all these templates, you'll find many of them are very 
similar ... for example you'll have

<xsl:template match="RefDetail">
   <!-- maps to TranDetail, and descends another level -->
   <TranDetail>
     <!-- but here we need to announce our type: -->
     <Ttype>refund</Ttype>
     <xsl:apply-templates/>
   </TranDetail>
</xsl:template>

... notice this is almost exactly like the template matching DisbDetail. 
(The one matching Ramount will be exactly like the one matching Damount.)

So they can be combined:

<xsl:template match="DisbDetail | RefDetail">
   <!-- maps to TranDetail, and descends another level -->
   <TranDetail>
     <!-- but here we need to announce our type: -->
     <Ttype>
       <xsl:choose>
         <xsl:when test="self::DisbDetail">dis</xsl:when>
         <xsl:otherwise>refund</xsl:otherwise>
       </xsl:choose>
     </Ttype>
     <xsl:apply-templates/>
   </TranDetail>
</xsl:template>

Your entire stylesheet will have just a few fairly simple templates.

I hope that helps! If anything here is mysterious to a newbie, my guess 
it'll be about how templates match and how xsl:apply-templates works ... 
the famous XSLT processing model.

Cheers,
Wendell


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
   Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread