XSLT Transforms - how to get just some of in a for-each
-
We use XSLT to convert various XML files into tab delimited. Some of the XML input files have a variable number of options. Sometimes none, others 1,2,3 or more. This makes the formatting difficult since there is also a header row that is created in the XSLT file that must have the exact number of columns and proper header context. So the question is how to get this formatted correctly since if we provide for 2 sets of opts (from the W3Schools tryit) in the header row how to get the for-each to stop at 2. <title>Eros</title> Eros Ramazzotti EU BMG 9.90 1997 Nm1111 Nm2222 Nm3333 Typical for-each In this case there will be 3 sets of opts but 2 sets of headers and all data after this will be off. Tried using xls:when test="@length =...' but don't see a way of just getting 2 of the 3 opts. Can anyone help? Thanks
-
We use XSLT to convert various XML files into tab delimited. Some of the XML input files have a variable number of options. Sometimes none, others 1,2,3 or more. This makes the formatting difficult since there is also a header row that is created in the XSLT file that must have the exact number of columns and proper header context. So the question is how to get this formatted correctly since if we provide for 2 sets of opts (from the W3Schools tryit) in the header row how to get the for-each to stop at 2. <title>Eros</title> Eros Ramazzotti EU BMG 9.90 1997 Nm1111 Nm2222 Nm3333 Typical for-each In this case there will be 3 sets of opts but 2 sets of headers and all data after this will be off. Tried using xls:when test="@length =...' but don't see a way of just getting 2 of the 3 opts. Can anyone help? Thanks
Hi, not sure if this article will help but it might be worth a look, http://p2p.wrox.com/xslt/32423-sorting-limiting-each-top-most-recent.html[^] You might be able to do something with position() as they are suggesting based on the number of headers ? Hope this Helps Chris
-
Hi, not sure if this article will help but it might be worth a look, http://p2p.wrox.com/xslt/32423-sorting-limiting-each-top-most-recent.html[^] You might be able to do something with position() as they are suggesting based on the number of headers ? Hope this Helps Chris
Thanks Yes, using the Position() function did help but part 2 of this was still a lot more code that had to be added to complete what I needed to and I do not like it. How do we code for the mininum number of iternations when the input XML has less than the minimum? The Position did limit the output to the number of iterations we needed to see but it did nothing to fill in if the number of iterations was LESS that what we expected. In our case the input XML had a length attribute.
data
So we had to add a bunch of if or choose statements ...fill put in 4 output empty values ...fill put in 3 output empty values Is there a better way?
-
Thanks Yes, using the Position() function did help but part 2 of this was still a lot more code that had to be added to complete what I needed to and I do not like it. How do we code for the mininum number of iternations when the input XML has less than the minimum? The Position did limit the output to the number of iterations we needed to see but it did nothing to fill in if the number of iterations was LESS that what we expected. In our case the input XML had a length attribute.
data
So we had to add a bunch of if or choose statements ...fill put in 4 output empty values ...fill put in 3 output empty values Is there a better way?
Hi, I haven't done anything like this before but I did a quick bit of hunting around and something like this might do it for you. Please note this is totally untested and is just an idea. I thought about how to do a while or a normal for loop in XSL and then try and find away of checking if the element at position x existed. This is cobbled together from a couple of SO posts and my own take on how to put it together. Sources: http://stackoverflow.com/questions/11127693/how-to-do-a-while-like-loop-in-xslt[^] http://stackoverflow.com/questions/5791053/xslt-if-tag-exists-apply-template-if-not-choose-static-value[^] http://www.sourceware.org/ml/xsl-list/2000-08/msg01503.html[^] From those articles this is what I have cobbled together:
1
Hope this helps. Chris
-
Hi, I haven't done anything like this before but I did a quick bit of hunting around and something like this might do it for you. Please note this is totally untested and is just an idea. I thought about how to do a while or a normal for loop in XSL and then try and find away of checking if the element at position x existed. This is cobbled together from a couple of SO posts and my own take on how to put it together. Sources: http://stackoverflow.com/questions/11127693/how-to-do-a-while-like-loop-in-xslt[^] http://stackoverflow.com/questions/5791053/xslt-if-tag-exists-apply-template-if-not-choose-static-value[^] http://www.sourceware.org/ml/xsl-list/2000-08/msg01503.html[^] From those articles this is what I have cobbled together:
1
Hope this helps. Chris
Yes, I started to look at params and variables but with the complexity of the multiple templates I just could not get it to work. The XSLT is too big as it is and it was hard to put it all together. I will have to re-look at it and perhaps test with a small XSLT until I can get the multi-templates and for loop template to work. Thanks!
-
Yes, I started to look at params and variables but with the complexity of the multiple templates I just could not get it to work. The XSLT is too big as it is and it was hard to put it all together. I will have to re-look at it and perhaps test with a small XSLT until I can get the multi-templates and for loop template to work. Thanks!
Hi, not sure if this is any help, I have used this to produce a student timetable grid. It does not do anything fancy at all, but it does use params and nested templates. It's quite old now and i'm sure it could be improved no end but it works well and might be of interest. Chris
Week A
======
A A
Week B
B B
Mon
Tue
Wed
Thu
Fri
Sat