Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] Pairing of nodes

From: "Andrew Welch" <andrew.j.welch@--------->
To:
Date: 11/1/2007 10:38:00 AM
> 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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent