xslt - Get the last occurrence of counter -


consider xml file:

<record>    <aa bac="1" kjd="same"/>    <bb dfd="02" dad="342"/>    <cc pod="11-a" dsd="11-b"/>    <cc pod="22-a" dsd="22-b"/>    <cc pod="33-a" dsd="33-b"/>    <cc pod="44-a" dsd="44-b"/>    <cc pod="55-a" dsd="55-b"/>    <cc pod="66-a" dsd="66-b"/>    <cc pod="77-a" dsd="77-b"/> </record> 

i need add attribute (cnt) under element cc, counter every 3 occurrences of cc. if reaches 4, need increment it. did part, need last value of @cnt in cc , place in 1 new (@cnt_1) of attribute in aa.

here xslt:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output indent="yes" method="xml"/> <xsl:strip-space elements="*"/> <xsl:template match="@* | node()">     <xsl:copy>         <xsl:apply-templates select="@* | node()"/>     </xsl:copy> </xsl:template> <xsl:template match="record">     <xsl:copy>         <xsl:apply-templates select="* except cc"/>         <xsl:for-each-group select="cc" group-adjacent="(position() - 1) idiv 3">             <xsl:apply-templates select="current-group()">                 <xsl:with-param name="group-pos" select="position()"/>             </xsl:apply-templates>         </xsl:for-each-group>     </xsl:copy> </xsl:template> <xsl:template match="cc">     <xsl:param name="group-pos"/>     <cc cnt="{$group-pos}" seq="{position()}">         <xsl:apply-templates select="@*"/>     </cc> </xsl:template> <xsl:template match="cc/@*[not(normalize-space())]"/> <xsl:template match="bb/@*[not(normalize-space())]" /> <xsl:template match="aa/@*[not(normalize-space())]" /> 

expected output:

<record>    <aa cnt_1="3" bac="1" kjd="same"/>    <bb dfd="02" dad="342"/>    <cc cnt="1" seq="1" pod="11-a" dsd="11-b"/>    <cc cnt="1" seq="2" pod="22-a" dsd="22-b"/>    <cc cnt="1" seq="3" pod="33-a" dsd="33-b"/>    <cc cnt="2" seq="1" pod="44-a" dsd="44-b"/>    <cc cnt="2" seq="2" pod="55-a" dsd="55-b"/>    <cc cnt="2" seq="3" pod="66-a" dsd="66-b"/>    <cc cnt="3" seq="1" pod="77-a" dsd="77-b"/> </record> 

thanks!

how about:

<xsl:template match="aa">     <xsl:copy>         <xsl:attribute name="cnt_1" select="ceiling(count(../cc ) div 3)"/>         <xsl:apply-templates select="@*|node()"/>     </xsl:copy> </xsl:template> 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -