need Help on XSLT logic

JaviSoft

New Member
can anone please help me to get the XSLT logic over here.I have explained the requirement as comments in the output.please find the below is the output and input.input:\[code\]<t> <element1>A</element1> <element2>B</element2> <colors> <!-- unbounded element --> <color1>colorA</color1> <color2>colorB</color2> <color3>colorA</color3> <colorItems> <!-- unbounded element --> <Level1>colorItemsA</Level1> <Level2>colorItemsB</Level2> <Level3>colorItemsC</Level3> <color>Red</color> </colorItems> <colorItems> <Level1>colorItemsD</Level1> <Level2>colorItemsB</Level2> <Level3>colorItemsC</Level3> <color>Red</color> </colorItems> <colorItems> <Level1>colorItemsC</Level1> <Level2>colorItemsB</Level2> <Level3>colorItemsA</Level3> <color>blue</color> </colorItems> </colors> <colors> <color1>colorC</color1> <color2>colorB</color2> <color3>colorD</color3> <colorItems> <Level1>colorItemsA</Level1> <Level2>colorItemsC</Level2> <Level3>colorItemsC</Level3> <color>green</color> </colorItems> <colorItems> <Level1>colorItemsC</Level1> <Level2>colorItemsB</Level2> <Level3>colorItemsA</Level3> <color>yello</color> </colorItems> <colorItems> <Level1>colorItemsC</Level1> <Level2>colorItemsA</Level2> <Level3>colorItemsA</Level3> <color>yello</color> </colorItems> </colors></t>\[/code\]1)There is a multiple Colors elements.2)Inside Colors multiple ColorItems are present.3)for each iteration we need a sequence of number assigned to the below showed elements and condition is also there if previous color = current color ignore level1,2,3 elements and directly pass color value.\[code\]output:<t> <element1>1</element1> <!-- hard code as 1 --> <element2>E</element2> <!-- hard code as E--> <colors> <!-- unbounded element --> <color1>2</color1> <!-- sequence number start from 2 --> <color2>1</color2> <!-- hard code as value 1 --> <color3>C</color3> <!-- hard code as C--> <colorItems> <!-- unbounded element --> <Level1>3</Level1> <!-- sequence number --> <Level2>2</Level2> <!-- sequence number of color1 --> <Level3>CI</Level3> <!-- hard code as CI--> <color>Red</color> <!-- directly pass color value --> </colorItems> <colorItems> <!-- IF previouscolor=current color(ignore the Level1,2,3 elements only pass color value) --> <color>Red</color> </colorItems> <colorItems> <Level1>4</Level1> <Level2>2</Level2> <Level3>CI</Level3> <color>blue</color> </colorItems> </colors> <colors> <color1>5</color1> <color2>1</color2> <color3>C</color3> <colorItems> <Level1>6</Level1> <Level2>5</Level2> <Level3>CI</Level3> <color>green</color> </colorItems> <colorItems> <Level1>7</Level1> <Level2>5</Level2> <Level3>CI</Level3> <color>yello</color> </colorItems> <colorItems> <!-- IF previouscolor=current color(ignore the Level1,2,3 elements only pass color value) --> <color>yello</color> </colorItems> </colors></t>\[/code\]I have tried the below xslt which is giving as same.1)but i am unable to get the below logic.When previous \[code\]<color>\[/code\] = current \[code\]<color>\[/code\] we have to ignore the Level1,2,3 elements and their sequence numbers also. xslt:\[code\]<xsl:template match="/"> <ns1:t> <ns1:element1> <xsl:text disable-output-escaping="no">1</xsl:text> </ns1:element1> <ns1:element2> <xsl:text disable-output-escaping="no">E</xsl:text> </ns1:element2> <xsl:for-each select="/ns1:t/ns1:colors"> <xsl:variable name="CLRPosition"> <xsl:value-of select="position()"/> </xsl:variable> <xsl:variable name="CLRVar"> <xsl:value-of select="int(count(/ns1:t/ns1:colors[position()<$CLRPosition]//ns1:colorItems)+position())+1"/> </xsl:variable> <ns1:colors> <ns1:color1> <xsl:value-of select="$CLRVar"/> </ns1:color1> <ns1:color2> <xsl:text disable-output-escaping="no">1</xsl:text> </ns1:color2> <ns1:color3> <xsl:text disable-output-escaping="no">C</xsl:text> </ns1:color3> <xsl:for-each select="ns1:colorItems"> <xsl:variable name="Ivar"> <xsl:value-of select="int($CLRVar+position())"/> </xsl:variable> <ns1:colorItems> <ns1:Level1> <xsl:value-of select="$Ivar"/> </ns1:Level1> <ns1:Level2> <xsl:value-of select="$CLRVar"/> </ns1:Level2> <ns1:Level3> <xsl:text disable-output-escaping="no">CI</xsl:text> </ns1:Level3> <ns1:color> <xsl:value-of select="ns1:color"/> </ns1:color> </ns1:colorItems> </xsl:for-each> </ns1:colors> </xsl:for-each> </ns1:t> </xsl:template></xsl:stylesheet>\[/code\]
 
Back
Top