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

Pairing of nodes

Subject: Pairing of nodes
From: Charanjeet <charanjeet.singh@xxxxxxxxx>
Date: Thu, 1 Nov 2007 11:18:50 +0530
 Pairing of nodes
Hi xsl-list,

This is my first post to this list. I hope I will get positive replies
to my first post.

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>

<!--Gateway 3-->
<Gateway>
<ID>ID-001</ID>
<Type>T002</Type>
</Gateway>

<!--Gateway 4-->
<Gateway>
<ID>ID-003</ID>
<Type>T001</Type>
</Gateway>

<!--Gateway 5-->
<Gateway>
<ID>ID-001</ID>
<Type>T003</Type>
</Gateway>
...
</Gateways>

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,
My output XML will be:

<!--Pair between Gateway1 and Gateway2-->
<GatewayPair>
<Head>ID-001</Head>
<Tail>ID-002</Tail>
</GatewayPair>

<!--Pair between Gateway1 and Gateway3-->
<GatewayPair>
<Head>ID-001</Head>
<Tail>ID-001</Tail>
</GatewayPair>

<!--Pair between Gateway1 and Gateway4-->
<GatewayPair>
<Head>ID-001</Head>
<Tail>ID-003</Tail>
</GatewayPair>

<!--Pair between Gateway1 and Gateway5-->
<GatewayPair>
<Head>ID-001</Head>
<Tail>ID-001</Tail>
</GatewayPair>

<!--Pair between Gateway2 and Gateway1-->
<GatewayPair>
<Head>ID-002</Head>
<Tail>ID-001</Tail>
</GatewayPair>

<!--Pair between Gateway2 and Gateway3-->
<GatewayPair>
<Head>ID-002</Head>
<Tail>ID-001</Tail>
</GatewayPair>

<!--Pair between Gateway2 and Gateway4-->
<GatewayPair>
<Head>ID-002</Head>
<Tail>ID-003</Tail>
</GatewayPair>

<!--Pair between Gateway2 and Gateway5-->
<GatewayPair>
<Head>ID-002</Head>
<Tail>ID-001</Tail>
</GatewayPair>


<!--Pair between Gateway3 and Gateway1-->
<GatewayPair>
<Head>ID-001</Head>
<Tail>ID-001</Tail>
</GatewayPair>

<!--Pair between Gateway3 and Gateway2-->
<GatewayPair>
<Head>ID-001</Head>
<Tail>ID-002</Tail>
</GatewayPair>

<!--Pair between Gateway3 and Gateway4-->
<GatewayPair>
<Head>ID-001</Head>
<Tail>ID-003</Tail>
</GatewayPair>

<!--Pair between Gateway3 and Gateway5-->
<GatewayPair>
<Head>ID-001</Head>
<Tail>ID-001</Tail>
</GatewayPair>

<!--Pair between Gateway4 and Gateway1 -->
<GatewayPair>
<Head>ID-003</Head>
<Tail>ID-001</Tail>
</GatewayPair>

<!--Pair between Gateway4 and Gateway2 -->
<GatewayPair>
<Head>ID-003</Head>
<Tail>ID-002</Tail>
</GatewayPair>

<!--Pair between Gateway4 and Gateway3 -->
<GatewayPair>
<Head>ID-003</Head>
<Tail>ID-001</Tail>
</GatewayPair>

<!--Pair between Gateway4 and Gateway5 -->
<GatewayPair>
<Head>ID-003</Head>
<Tail>ID-001</Tail>
</GatewayPair>
.......

I hope I am clear with my problem.


Relevant portion of my XSLT:

<xsl:template match="/">
<GatewayData>
<xsl:apply-templates select="//Gateways"/>
</GatewayData>
</xsl:template>

<xsl:template match="//Gateways">
<xsl:for-each select="./Gateway">
<xsl:variable name="gwId">
<xsl:value-of select="ID"/>
</xsl:variable>
<xsl:for-each select="preceding-sibling::*">
<GatewayPair>
<Head>
<xsl:value-of select="$gwId"/>
</Head>
<Tail>
<xsl:value-of select="./ID"/>
</Tail>
</GatewayPair>
</xsl:for-each>
<xsl:for-each select="following-sibling::*">
<GatewayPair>
<Head>
<xsl:value-of select="$gwId"/>
</Head>
<Tail>
<xsl:value-of select="./ID"/>
</Tail>
</GatewayPair>
</xsl:for-each>
</xsl:for-each>
</xsl:template>

I am a newbie to XSLT, so my questions could be quite basic...
Questions:
1. Is there any other way of transforming this without using
'preceding-sibling::*' and 'following-sibling::*'?
2. My code is redundant and is same in 'for loops' for
'preceding-sibling::*' and 'following-sibling::*'. How can we avoid
this redundancy?
3. How can we access a variable of outer loop in the inner loop. I am
doing this via defining a local variable. In my actual XML (not
provided here), I have to access several variables from the outer
loop, so I will end up defining lots of local variables.

Thanks,
Charanjeet

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.