Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


[xsl] Pairing of nodes

From: Charanjeet <charanjeet.singh@--------->
To:
Date: 11/1/2007 5:49:00 AM
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


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