I apologize in advance - there are a lot of newbies here and is struggling after reading almost all XSLT threads. So I desperately need some aspirins and your leadership!
I have three input parameters / variables that need to be processed using xslt version 1.0.
<xsl:variable name="tw">125</xsl:variable> <xsl:variable name="rows">4</xsl:variable> <xsl:variable name="cols">6</xsl:variable>
I want to get the calculated values (pixels) in the " background-position " HTML, as shown below: -
<div style="background-position:-0px -0px;"><img src="images/thumbs/1.jpg" alt="one" /></div> <div style="background-position:-125px -0px;"><img src="images/thumbs/2.jpg" alt="two" /></div> <div style="background-position:-250px -0px;"><img src="images/thumbs/3.jpg" alt="three" /></div> <div style="background-position:-375px -0px;"><img src="images/thumbs/4.jpg" alt="four" /></div> <div style="background-position:-500px -0px;"><img src="images/thumbs/5.jpg" alt="five" /></div> <div style="background-position:-625px -0px;"><img src="images/thumbs/6.jpg" alt="six" /></div> <div style="background-position:-0px -125px;"><img src="images/thumbs/7.jpg" alt="seven" /></div> <div style="background-position:-125px -125px;"><img src="images/thumbs/8.jpg" alt="eight" /></div> <div style="background-position:-250px -125px;"><img src="images/thumbs/9.jpg" alt="nine" /></div> <div style="background-position:-375px -125px;"><img src="images/thumbs/10.jpg" alt="ten" /></div> <div style="background-position:-500px -125px;"><img src="images/thumbs/11.jpg" alt="11" /></div> <div style="background-position:-625px -125px;"><img src="MIB/images/thumbs/12.jpg" alt="12" /></div> <div style="background-position:-0px -250px;"><img src="images/thumbs/13.jpg" alt="13" /></div> <div style="background-position:-125px -250px;"><img src="/mages/thumbs/14.jpg" alt="14" /></div> <div style="background-position:-250px -250px;"><img src="images/thumbs/15.jpg" alt="15" /></div> <div style="background-position:-375px -250px;"><img src="images/thumbs/16.jpg" alt="16" /></div> <div style="background-position:-500px -250px;"><img src="images/thumbs/17.jpg" alt="17" /></div> <div style="background-position:-625px -250px;"><img src="images/thumbs/18.jpg" alt="18" /></div> <div style="background-position:-0px -375px;"><img src="images/thumbs/19.jpg" alt="19" /></div> <div style="background-position:-125px -375px;"><img src="images/thumbs/20.jpg" alt="20" /></div> <div style="background-position:-250px -375px;"><img src="images/thumbs/21.jpg" alt="21" /></div> <div style="background-position:-375px -375px;"><img src="images/thumbs/22.jpg" alt="22" /></div> <div style="background-position:-500px -375px;"><img src="images/thumbs/23.jpg" alt="23" /></div> <div style="background-position:-625px -375px;"><img src="images/thumbs/24.jpg" alt="24" /></div>
I understand that I need to go in cycles (somehow) and save the count (somehow), and having seen numerous examples of loop and counting, I expected the process to be direct, but then I read about position()
and number
and so many other things that my head rotates. I donβt know how and where to position the count loop in the for-each
statement or even if for-each
is the best solution. None of my weak attempts have created a well-formed XSL, so you can see that I am not getting quick access.
So here, hoping that one of the gurus here can help me start the process and help in my understanding of xslt.
Image data comes from an XML file, and I can process img src and alt without problems, so this is just pixel calculations, loop, count, etc. This is problem
<xsl:for-each select="DATASET/ITEM"> <div style="background-position:-{rowpos}px -{colpos}px;"><img src="{thumbnailimage}" alt="{imagealttext}" /></div> </xsl:for-each>
So for clarification:
The calculation. There are 3 variables that will result in 6 calculated values. TW = 125. The width is used for background positions, therefore, depending on the row / column, the calculated values ββare given in the example - for example. tw-tw, tw, tw * 2, tw * 3, tw * 4 and tw * 5 (a total of 6 calculations). The number of rows and the number of columns determines how and where each thumbnail is positioned using the calculated pixel values, which in the above case are 0px, -125px, -250px, -375px, -500px and -675px.
Further expansion of calculations: - The positions of rows and columns are determined by the width of the thumbnail. The position of each sketch is determined by the number of rows and the number of columns. A grid of 3 columnar columns with thumbs of 150 pixels wide will require 9 thumbnails, therefore 9 sets of px values, but only 2 unique calculations, for example, will be required. tw * 2 and tw * 3 as shown below: -
<div style="background-position: -0px -0px;" /><img src="image 1.jpg" alt="Alt text 1" /></div> <div style="background-position: -125px -0px;" /><img src="image 2.jpg" alt="Alt text 2" /></div> <div style="background-position: -250px -0px;" /><img src="image 3.jpg" alt="Alt text 3" /></div> <div style="background-position: -375px -0px;" /><img src="image 4.jpg" alt="Alt text 4" /></div> <div style="background-position: -500px -0px;" /><img src="image 5.jpg" alt="Alt text 5" /></div> <div style="background-position: -0px -125px;" /><img src="image 6.jpg" alt="Alt text 6" /></div> <div style="background-position: -125px -125px;" /><img src="image 7.jpg" alt="Alt text 7" /></div> <div style="background-position: -250px -125px;" /><img src="image 8.jpg" alt="Alt text 8" /></div> <div style="background-position: -375px -125px;" /><img src="image 9.jpg" alt="Alt text 9" /></div> <div style="background-position: -500px -125px;" /><img src="image 10.jpg" alt="Alt text 10" /></div> <div style="background-position: -0px -250px;" /><img src="image 11.jpg" alt="Alt text 11" /></div> <div style="background-position: -125px -250px;" /><img src="image 12.jpg" alt="Alt text 12" /></div> <div style="background-position: -250px -250px;" /><img src="image 13.jpg" alt="Alt text 13" /></div> <div style="background-position: -375px -250px;" /><img src="image 14.jpg" alt="Alt text 14" /></div> <div style="background-position: -500px -250px;" /><img src="image 15.jpg" alt="Alt text 15" /></div> <div style="background-position: -0px -375px;" /><img src="image 16.jpg" alt="Alt text 16" /></div> <div style="background-position: -125px -375px;" /><img src="image 17.jpg" alt="Alt text 17" /></div> <div style="background-position: -250px -375px;" /><img src="image 18.jpg" alt="Alt text 18" /></div> <div style="background-position: -375px -375px;" /><img src="image 19.jpg" alt="Alt text 19" /></div> <div style="background-position: -500px -375px;" /><img src="image 20.jpg" alt="Alt text 20" /></div>
XML file The src and alt text image comes from an XML file: -
<DATASET> <ITEM> <THUMBNAILIMAGE>image1.jpg</THUMBNAILIMAGE> <IMAGEALTTEXT>Alt text 1</IMAGEALTTEXT> </ITEM> <ITEM> <THUMBNAILIMAGE>image2.jpg</THUMBNAILIMAGE> <IMAGEALTTEXT>Alt text 2</IMAGEALTTEXT> </ITEM> ...... <ITEM> <THUMBNAILIMAGE>image20.jpg</THUMBNAILIMAGE> <IMAGEALTTEXT>Alt text 20</IMAGEALTTEXT> </ITEM> </DATASET>