473,385 Members | 1,707 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,385 software developers and data experts.

XSLT increment

Hi
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 14663
Dormilich
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"/>
regards

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
jkmyoung
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)
Options:
1. Have you tried using <xsl:number/> ?
http://www.w3schools.com/xsl/el_number.asp
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.
http://saxon.sourceforge.net/saxon7....l#saxon:assign
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
jkmyoung
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
  1.  
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  3. <xsl:template match="/">
  4.  
  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">
  11.  
  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>
  17.  
  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.
  19.  
  20. -->
  21.  
  22. </xsl:for-each>
  23.  
  24. </svg:svg>
  25. </xsl:template>
  26. </xsl:stylesheet>
  27.  
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
  1.  
  2. <?xml version = '1.0' encoding = 'UTF-8'?>
  3. <svg:svg viewBox="100%" xmlns:svg="http://www.w3.org/2000/svg">
  4.  
  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-->
  11.  
  12. </svg:svg>
  13.  
I hope I am clear in describing my specific problem

Thanks
Nov 19 '08 #6
jkmyoung
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
jkmyoung
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

2
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...
1
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...
5
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: ...
6
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...
2
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...
3
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...
1
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...
12
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...
2
jkmyoung
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.