[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Pairing of nodes

Subject: Re: Pairing of nodes
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Thu, 1 Nov 2007 10:37:48 +0000
Re:  Pairing of nodes
> This is my first post to this list. I hope I will get positive replies
> to my first post.

Hi, a good first post - you just left out which version of XSLT you're
using, and/or which processor.


> My Input XML is:
>
> <Gateways>
> <!--Gateway 1-->
> <Gateway>
> <ID>ID-001</ID>
> <Type>T001</Type>
> </Gateway>
>
> <!--Gateway 2-->
> <Gateway>
> <ID>ID-002</ID>
> <Type>T001</Type>
> </Gateway>

> In this XML, the combination of <ID> and <Type> for a <Gateway> is
> always unique.
> Each <Gateway> has to make pair with other <Gateway>
> First Gateway will be the <Head> and second Gateway will be the <Tail>.
>
> i.e. Gateway1 will act as <Head> and make pair with Gateway2, Gateway3
> and so on....
> Gateway2 will then act as <Head> and make pair with Gateway1, Gateway3
> and so on....

So each <Gateway> is paired with another <Gateway> based on <ID> or
<Type> and the list is denormalized, which makes it easier.

I would define two keys:

<xsl:key name="gateway-by-id" match="Gateway" use="ID"/>
<xsl:key name="gateway-by-type" match="Gateway" use="Type"/>

Then in your <Gateway> template construct a list of all the <Gateway>
elements that have the same <ID> or <Type>, and then filter the list
to stop the <Gateway> matching itself:

<xsl:template match="Gateway">
    <xsl:variable name="id" select="@id"/>
    <xsl:variable name="thisID" select="ID"/>
    <xsl:variable name="thisType" select="Type"/>
    <xsl:for-each select="(key('gateway-by-id',
$thisID)|key('gateway-by-type', $thisType))[generate-id() !=
generate-id(current())]">
        <GatewayPair pair="{$id}-{@id}">
            <Head><xsl:value-of select="$thisID"/></Head>
            <Tail><xsl:value-of select="ID"/></Tail>
        </GatewayPair>
    </xsl:for-each>
</xsl:template>

I've also added an "id" attribute to each <Gateway> in your source:

            <Gateway id="1">
                <ID>ID-001</ID>
                <Type>T001</Type>
            </Gateway>

            <!--Gateway 2-->
            <Gateway id="2">
                <ID>ID-002</ID>
                <Type>T001</Type>
            </Gateway>

....which makes it easier to spot which pairs have been created.  So
if you add id's to your source and the use the above template, you get
this result:

<GatewayData>
   <GatewayPair pair="1-2">
      <Head>ID-001</Head>
      <Tail>ID-002</Tail>
   </GatewayPair>
   <GatewayPair pair="1-3">
      <Head>ID-001</Head>
      <Tail>ID-001</Tail>
   </GatewayPair>
   <GatewayPair pair="1-4">
      <Head>ID-001</Head>
      <Tail>ID-003</Tail>
   </GatewayPair>
   <GatewayPair pair="1-5">
      <Head>ID-001</Head>
      <Tail>ID-001</Tail>
   </GatewayPair>
   <GatewayPair pair="2-1">
      <Head>ID-002</Head>
      <Tail>ID-001</Tail>
   </GatewayPair>
   <GatewayPair pair="2-4">
      <Head>ID-002</Head>
      <Tail>ID-003</Tail>
   </GatewayPair>
   <GatewayPair pair="3-1">
      <Head>ID-001</Head>
      <Tail>ID-001</Tail>
   </GatewayPair>
   <GatewayPair pair="3-5">
      <Head>ID-001</Head>
      <Tail>ID-001</Tail>
   </GatewayPair>
   <GatewayPair pair="4-1">
      <Head>ID-003</Head>
      <Tail>ID-001</Tail>
   </GatewayPair>
   <GatewayPair pair="4-2">
      <Head>ID-003</Head>
      <Tail>ID-002</Tail>
   </GatewayPair>
   <GatewayPair pair="5-1">
      <Head>ID-001</Head>
      <Tail>ID-001</Tail>
   </GatewayPair>
   <GatewayPair pair="5-3">
      <Head>ID-001</Head>
      <Tail>ID-001</Tail>
   </GatewayPair>
</GatewayData>


Is that what you needed....?


cheers
-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.