Re: [xsl] Pairing of nodes

Subject: Re: [xsl] Pairing of nodes
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Thu, 1 Nov 2007 10:37:48 +0000
> 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