javascript - count the number of elements in an XSL for-each loop? -
i have been trying past day figure out how count number of elements in xsl for-each loop. have tried doing javascript can't seem work. i'm not sure if it's matter of not displaying output correctly or if can xslt. appreciated. nasty bug not prepared for.
<script type="text/javascript"> var colatotal=0; function loopcountera(){ colatotal++; } function printa(){ document.write(colatotal); } </script> <xsl:for-each select='bookmarks/category'> <xsl:for-each select='./bookmark'> <script type="text/javascript"> loopcountera(); printa(); </script> </xsl:for-each> </xsl:for-each>
from comments:
i figured showing code may waste of time. have div columna , div columnb, both of vertical columns. boss wants of bookmarks wrap, can't start wrap in middle of category block. intention keep track of 3 variables this: total bookmarks in columna far (a), total bookmarks in columnb far (b), , number of bookmarks in next category block placed (c). if b + c greater a, place block columna , i'll place columnb otherwise. make more sense now? thx much.
this stylesheet (your algorithm):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="bookmarks"> <html> <xsl:apply-templates select="category[1]"/> </html> </xsl:template> <xsl:template match="category"> <xsl:param name="pcolumna" select="/.."/> <xsl:param name="pcolumnb" select="/.."/> <xsl:variable name="vtest" select="count($pcolumnb/bookmark|bookmark) > count($pcolumna/bookmark)"/> <xsl:variable name="vcolumna" select="$pcolumna|self::*[$vtest]"/> <xsl:variable name="vcolumnb" select="$pcolumnb|self::*[not($vtest)]"/> <xsl:variable name="vnext" select="following-sibling::category[1]"/> <xsl:apply-templates select="$vnext"> <xsl:with-param name="pcolumna" select="$vcolumna"/> <xsl:with-param name="pcolumnb" select="$vcolumnb"/> </xsl:apply-templates> <xsl:if test="not($vnext)"> <div id="columna"> <xsl:copy-of select="$vcolumna"/> </div> <div id="columnb"> <xsl:copy-of select="$vcolumnb"/> </div> </xsl:if> </xsl:template> </xsl:stylesheet>
with input:
<bookmarks> <category id="a"> <bookmark id="1"/> <bookmark id="2"/> <bookmark id="3"/> </category> <category id="b"> <bookmark id="4"/> <bookmark id="5"/> </category> <category id="c"> <bookmark id="6"/> <bookmark id="7"/> <bookmark id="8"/> <bookmark id="9"/> <bookmark id="10"/> </category> <category id="d"> <bookmark id="11"/> <bookmark id="12"/> <bookmark id="13"/> <bookmark id="14"/> <bookmark id="15"/> </category> </bookmarks>
output:
<html> <div id="columna"> <category id="a"> <bookmark id="1"></bookmark> <bookmark id="2"></bookmark> <bookmark id="3"></bookmark> </category> <category id="c"> <bookmark id="6"></bookmark> <bookmark id="7"></bookmark> <bookmark id="8"></bookmark> <bookmark id="9"></bookmark> <bookmark id="10"></bookmark> </category> </div> <div id="columnb"> <category id="b"> <bookmark id="4"></bookmark> <bookmark id="5"></bookmark> </category> <category id="d"> <bookmark id="11"></bookmark> <bookmark id="12"></bookmark> <bookmark id="13"></bookmark> <bookmark id="14"></bookmark> <bookmark id="15"></bookmark> </category> </div> </html>
Comments
Post a Comment