Re: [xsl] Combining two node Sets into one

Subject: Re: [xsl] Combining two node Sets into one
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 31 Mar 2005 17:38:23 -0500
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