472,145 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

XSLT increment

I have a number of variables which I want to use in svg diagrams for coordinates. And some of these coordinates have to increment or decrement as need be, for each iteration in the <for-each> loop.
I know xslt variables cant be changed once assigned a value. But I think there is a way to get around it by making templates. But I am not really sure how to do that.
Could somebody help?
Nov 18 '08 #1
9 14499
8,658 Expert Mod 8TB
simply redefine it.
Expand|Select|Wrap|Line Numbers
  1. <xsl:variable name="test">1</xsl:variable>
  2. // some code...
  3. <xsl:variable name="test" select="$test + 1"/>

PS: I think w3schools.com's note about not being able to change a variable value is a mistake, I didn't find any such note in the specs.
Nov 19 '08 #2
2,057 Expert 2GB
Pure XSLT doesn't allow you to reassign variables. You'll get a compilation error if you do it that way. (variable already exists within that scope)
1. Have you tried using <xsl:number/> ?
2. Another way is to use a recursive template.
3. Depending on what you're doing, you may be able to use a combination of count() or sum(), and preceding:: axis.
4. If you're using saxon, you can cheat using an extension.
Nov 19 '08 #3
thanks for your reply.
- Dont know how to use xsl:number for this situation
-How would recursive template help in situation, I dont want a number to increment a number of times in one template at the same time. I want every time the control goes in the for-each, it should increment by 10( or some other number). Shouldnt I be calling one template from another. May be its the programmer in me which is thinking this way. Not really sure how to accomplish the task.
-also dont know how to use count() and sum() for this task

Please throw some more light
Nov 19 '08 #4
2,057 Expert 2GB
Can we see your current xml, xslt, and expected result?
Please simplify where possible.
Nov 19 '08 #5
Can we see your current xml, xslt, and expected result?
Please simplify where possible.
I'll try to simplify although that wont be easy.
I have an xsql file which queries the db and throws the result in XML format. This generated xml file would be displayed in terms of xml diagrams(using svg and xslt) on the browser.
So I dont have the xml file as such because its generated at run time.
The xsl file is as under:
Expand|Select|Wrap|Line Numbers
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  3. <xsl:template match="/">
  5. <xsl:variable name="x" select="500"/>
  6. <xsl:variable name="y" select="200"/>
  7. <xsl:variable name="rx" select="5"/>
  8. <xsl:variable name="ry" select="5"/>
  9. <svg:svg xmlns:svg="http://www.w3.org/2000/svg" viewBox="100%">
  10. <xsl:for-each select="/somenode">
  12. <svg:rect x="{$x}" y="{$y}" rx="{$rx}" ry="{$ry}" width="40" height="60"
  13. fill="red" stroke="none"></svg:rect>
  14. <svg:text font-family="Verdana" font-size="6" fill="black">
  15. <xsl:value-of select="node_name"/>
  16. </svg:text>
  18. <!-- here I would like to decrease x and y values by some constant numbers , so that for next iteration the values are somewhere aound 480 and 190 respectively. Right now I am getting all the rectangles overlapping on each other because the x and y coordinates remain the same. Instead, I want to have kinda stacked view of the rectangles.
  20. -->
  22. </xsl:for-each>
  24. </svg:svg>
  25. </xsl:template>
  26. </xsl:stylesheet>
So when I view source from the browser, I see lots of rectangle elements and different node_names for each of those, but same x and y coordinates.

View Source/ xml file after applying existing xslt:--

Expand|Select|Wrap|Line Numbers
  2. <?xml version = '1.0' encoding = 'UTF-8'?>
  3. <svg:svg viewBox="100%" xmlns:svg="http://www.w3.org/2000/svg">
  5. <svg:rect x="500" y="200" rx="5" ry="5" width="40" height="60" fill="red" stroke="none"/>
  6. <svg:text font-family="Verdana" font-size="6" fill="black">nodename1</svg:text> <!-- End of first iteration-->
  7. <svg:rect x="500" y="200" rx="5" ry="5" width="40" height="60" fill="red" stroke="none"/>
  8. <svg:text font-family="Verdana" font-size="6" fill="black">nodename2</svg:text> <!-- End of second iteration-->
  9. <svg:rect x="500" y="200" rx="5" ry="5" width="40" height="60" fill="red" stroke="none"/>
  10. <svg:text font-family="Verdana" font-size="6" fill="black">nodename3</svg:text> <!-- End of third iteration-->
  12. </svg:svg>
I hope I am clear in describing my specific problem

Nov 19 '08 #6
2,057 Expert 2GB
Could you use the position function then? eg something like:
<svg:rect x="{$x- position() * 40}" y="{$y - position() * 60}" rx="{$rx}" ry="{$ry}" width="40" height="60"
Nov 20 '08 #7
Just before looking at your comment I successfully tried exactly the same thing and it worked out. Instead of decrementing I incremented it by some value. Thanks a lot for your help. The only thing I would love to hear is that how would I put same logic in the form of a template, so that it can be generalized and used for a number of diagrams.
Nov 21 '08 #8
2,057 Expert 2GB
Just swap out the numbers you're multiplying by with another variable.
Nov 21 '08 #9
Thanks a lot again !
Nov 25 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by ted | last post: by
5 posts views Thread by Miguel J. Jiménez | last post: by
6 posts views Thread by M.Kamermans | last post: by
2 posts views Thread by John Wilkinson | last post: by
3 posts views Thread by Teksure | last post: by
1 post views Thread by Sergey Dubinets | last post: by
12 posts views Thread by Chris | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.