SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot vodica's Avatar
    Join Date
    Jul 2001
    Location
    SLovenia
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XSL Element counting

    Hello.

    Does anybody know how to achive something like an iterator in XSL. For example:

    i have an XML document which has inside n-elements - equations. I have previously generated images that contain theese equations and have the name eqN where N is their number of appearance. The first equation has the name eq1.gif, the next one eq2.gif and so on. What I want to do now, using XSL is to repeat this count. I know that during execution of XSL you cannot modify the values of variables so doing something like $count++ is not a good idea :-). I think this could be done by counting all predecessors but I do not know how I can do this.

    Please help.

    Regards,
    Vodica
    while(true) sheep++;

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Are you looping through nodes to generate these images? If so, you can use the XPath position() function to find the "index" of the element in the node set. Here's a quick example that may apply to what you're doing.

    XML
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet href="pos.xsl" type="text/xsl"?>
    <root>
     <eq>1+1=2</eq>
     <eq>1+2=3</eq>
     <eq>1+3=4</eq>
     <eq>1+4=5</eq>
     <eq>1+5=6</eq>
    </root>
    XSL
    HTML Code:
    <?xml version="1.0" encoding="utf-8"?> 
    <xsl:stylesheet 
    	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    	version="1.0">
      	<xsl:output method="html" 
      	version="4.01" 
      	encoding="utf-8" />
      	<xsl:template match="/">
      	<html>
      	<head>
      		<title>position test</title>
      	</head>
      	<body>
      		<xsl:for-each select="/root/eq">
      			<div>
      				<!--output the equation-->
      				<xsl:value-of select="." />
      				
      				<!--output an image based on position of element in node set-->
      				<img>
      				<xsl:attribute name="src">
      				eq<xsl:value-of select="position()" />.gif
      				</xsl:attribute>
      				</img>
      			</div>
      		</xsl:for-each>
      	</body>
      	</html>
      	</xsl:template>
    </xsl:stylesheet>
    This should output the equation, and an image named eq{position}.gif next to it, where {position} is the position of the current equation in the node list (or a loop counter if you will).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •