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
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--