473,372 Members | 1,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 14660
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

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

Similar topics

by: ted | last post by:
Was wondering if XSLT alone is appropriate for the following situation. From XML, I'm creating a small website (around 50 pages) with pages that link to each other through a nav menu and a...
by: Mohit | last post by:
Hi Friends I have to call 1 of the 2 child XSLT files from the Main XSLT file based on some criteria. I want one child XSLT file will be executed by version 1 of XSLT processor and the other by...
by: Miguel J. Jiménez | last post by:
Hi, I need to make a FOR structure using XSL... ie. I want to make a structure for moving between 0 and x, being x a variable name... How can I do this? In PHP/Java would be something like this: ...
by: M.Kamermans | last post by:
I have an XSLT script that has to operate on some particularly nasty XML to turn it into a SQL databse, but 'entries' in the XML aren't actually unique, so their unique identifier is useless. I...
by: John Wilkinson | last post by:
Hi, I am new to XSLT. My problem is that I wish to create an HTML table, and give each row an incrementing number from 1.This would increment every itteration of a for-each loop. The XSLT...
by: Teksure | last post by:
Hi group, searching in the Internet I found two products for XML which incorporate a very robust debugger for XSL/XSLT, I would like you to see these products and then, give me your opinion about...
by: Sergey Dubinets | last post by:
In effort to prioritize our goals we composed the list of random features each of them may add value to set of XSLT tools offered from Microsoft. 1. XSLTc (Compiler for XSLT...
by: Chris | last post by:
Hi, Just wondering if anyone out there knows if it is possible to convert a CSV to xml using XSLT? I've seen a lot of examples of xml to CSV, but is it possible to go back the other way? I...
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.