Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. XSLT Transforms - how to get just some of in a for-each

XSLT Transforms - how to get just some of in a for-each

Scheduled Pinned Locked Moved Web Development
xmlquestionhelptutorial
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QuickBooksDev
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • Q QuickBooksDev

      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

      C Offline
      C Offline
      Chris Grove
      wrote on last edited by
      #2

      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

      Q 1 Reply Last reply
      0
      • C Chris Grove

        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

        Q Offline
        Q Offline
        QuickBooksDev
        wrote on last edited by
        #3

        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?

        C 1 Reply Last reply
        0
        • Q QuickBooksDev

          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?

          C Offline
          C Offline
          Chris Grove
          wrote on last edited by
          #4

          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

          Q 1 Reply Last reply
          0
          • C Chris Grove

            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

            Q Offline
            Q Offline
            QuickBooksDev
            wrote on last edited by
            #5

            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!

            C 1 Reply Last reply
            0
            • Q QuickBooksDev

              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!

              C Offline
              C Offline
              Chris Grove
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups