Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 29th, 2010, 02:49 PM
Authorized User
 
Join Date: Mar 2010
Posts: 28
Thanks: 3
Thanked 0 Times in 0 Posts
Default Recursive Loop in XSLT file

Respected Sir,

I want to put recursive loop in my XSLT file. The recursive loop is similar to for loop in C or C++.

This recursive loop is used to display some values upto limit of that loop.

So, How can i put the recursive loop in XSLT file.

Regards/Thanks,
Dhrumil Shah.
 
Old March 29th, 2010, 02:56 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Please show an example of the input XML, and the output you want to produce. It would also be useful to say whether you are using XSLT 1.0 or 2.0.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 29th, 2010, 04:49 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I asked for three things:

Please show (1) an example of the input XML, and (2) the output you want to produce. It would also be useful to say (3) whether you are using XSLT 1.0 or 2.0.

You've provided the first, could we please have the other two?

The reason that your code is badly formatted, by the way, is that you didn't read the instructions above the edit box: use [ code ] and [ / code ] before and after code to format it properly.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 30th, 2010, 01:06 AM
Authorized User
 
Join Date: Mar 2010
Posts: 28
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Code:
<?xml version="1.0"?>
<?xml-stylesheet href='newCSS.XSL' type='text/xsl'?>
<Lineage>
   <sample_001>
      <time>06:03:09.750000</time>
      <date>2009/05/26</date>
      <X>4025.0784</X>
      <Y>4488.9818</Y>
      <Z>3925.5985</Z>
      <X1>3.6739</X1>
      <Y1>2.0819</Y1>
      <Z1>+6.1319</Z1>
      <R>0.0015</R>
      <P>+0.0035</P>
      <Ya>3.2564</Ya>
      <side_angle>334.7379</side_angle>
   </sample_001>
   <sample_002>
        <time>06:03:09.875000</time>
        <date>2009-05-26</date>
        <X>4025.5376</X>
        <Y>4489.2420</Y>
        <Z>3924.8320</Z>
         <X1>3.6734</X1>
        <Y1>2.0813</Y1>
       <Z1>-6.1324</Z1>
       <R>0.0015</R>
        <P>-0.0035</P>
        <Ya>3.2567</Ya>
        <side_angle>334.7384</side_angle>
    </sample_002>
    <sample_003>
        <time>06:03:10.000000</time>
        <date>2009-05-26</date>
        <X>4025.9967</X>
        <Y>4489.5021</Y>
        <Z>3924.0654</Z>
        <X1>3.6728</X1>
        <Y1>2.0807</Y1>
        <Z1>-6.1329</Z1>
        <R>0.0015</R>
         <P>-0.0034</P>
         <Ya>3.2570</Ya>
        <side_angle>334.7389</side_angle>
    </sample_003>
    <sample_004>
        <time>06:03:10.125000</time>
        <date>2009-05-26</date>
         <X>4026.4558</X>
        <Y>4489.7622</Y>
        <Z>3923.2987</Z>
        <X1>3.6723</X1>
        <Y1>2.0801</Y1>
        <Z1>-6.1335</Z1>
        <R>0.0015</R>
         <P>-0.0034</P>
         <Ya>3.2572</Ya>
        <side_angle>334.7394</side_angle>
    </sample_004>
    <sample_005>
         <time>06:03:10.250000</time>
         <date>2009-05-26</date>
        <X>4026.9148</X>
        <Y>4490.0222</Y>
       <Z>3922.5320</Z>
        <X1>3.6717</X1>
        <Y1>2.0795</Y1>
        <Z1>-6.1340</Z1>
        <R>0.0015</R>
        <P>-0.0034</P>
        <Ya>3.2575</Ya>
        <side_angle>334.7400</side_angle>
    </sample_005>
    <sample_006>
         <time>06:03:10.375000</time>
         <date>2009-05-26</date>
         <X>4027.3737</X>
         <Y>4490.2821</Y>
         <Z>3921.7652</Z>
         <X1>3.6712</X1>
         <Y1>2.0789</Y1>
         <Z1>-6.1345</Z1>
         <R>0.0015</R>
         <P>-0.0034</P>
         <Ya>3.2578</Ya>
         <side_angle>334.7405</side_angle>
    </sample_006>
    <sample_007>
          <time>06:03:10.500000</time>
          <date>2009-05-26</date>
          <X>4027.8326</X>
          <Y>4490.5419</Y>
          <Z>3920.9984</Z>
          <X1>3.6707</X1>
          <Y1>2.0783</Y1>
          <Z1>-6.1350</Z1>
          <R>0.0015</R>
          <P>-0.0034</P>
          <Ya>3.2581</Ya>
          <side_angle>334.7410</side_angle>
     </sample_007>
     <sample_008>
           <time>06:03:10.625000</time>
           <date>2009-05-26</date>
           <X>4028.2914</X>
           <Y>4490.8017</Y>
           <Z>3920.2314</Z>
           <X1>3.6701</X1>
           <Y1>2.0777</Y1>
           <Z1>-6.1356</Z1>
           <R>0.0015</R>
           <P>-0.0034</P>
           <Ya>3.2583</Ya>
           <side_angle>334.7415</side_angle>
     </sample_008>
     <sample_009>
           <time>06:03:10.750000</time>
           <date>2009-05-26</date>
          <X>4028.7502</X>
         <Y>4491.0614</Y>
         <Z>3919.4645</Z>
         <X1>3.6696</X1>
         <Y1>2.0771</Y1>
         <Z1>-6.1361</Z1>
         <R>0.0015</R>
         <P>-0.0034</P>
         <Ya>3.2586</Ya>
         <side_angle>334.7420</side_angle>
    </sample_009>
    <sample_010>
          <time>06:03:10.875000</time>
         <date>2009-05-26</date>
         <X>4029.2088</X>
         <Y>4491.3210</Y>
         <Z>3918.6974</Z>
         <X1>3.6690</X1>
         <Y1>2.0765</Y1>
         <Z1>-6.1366</Z1>
         <R>0.0015</R>
         <P>-0.0034</P>
         <Ya>3.2589</Ya>
         <side_angle>334.7426</side_angle>
     </sample_010>
</Lineage>
Now sir you can see that I use one tag Lineage and in that tag there are sub tags like sample_001, sample_002 n ...

I want to use recursive loop for these 10 samples and want to display it in a tabular format. That means if there are 10 values so the loop goes for 10 times. If there 200 values than the loop goes for 200 times. Give me the suggestion.

I want the output like this way.

Samples No. Time Date X Y Z X1 Y1 Z1 R P Ya Side Angle
1
2
.
.
.
10


So this is my output table.
 
Old March 30th, 2010, 02:51 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try the below:
Code:
<xsl:template match="Lineage">
<html><body>
<table border="1">
<tr><td>Samples No.</td><td>Time</td><td>Date</td><td>X</td><td>Y</td><td>Z</td><td>X1</td><td>Y1</td><td>Z1</td><td>R</td><td>P</td><td>Ya</td><td>Side Angle</td></tr>
<xsl:for-each select="*">
<tr>
<td><xsl:value-of select="position()"/></td>
<xsl:for-each select="*">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
__________________
Rummy
 
Old March 30th, 2010, 03:56 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

"I want to use recursive loop".

The answer from mrame is good. I was puzzled as to why you would want to use recursion here. I suspect you are using the word "recursive" incorrectly. Recursion is a powerful technique - having functions or templates that call themselves - and it is often useful in XSLT. But it's not needed for a simple problem like this, where xsl:for-each is quite adequate.

It's not just a problem with vocabulary, though. When you ask a question on a forum like this, it's generally best to explain the problem you want to solve, and not to focus on the technique you want to use to solve it. The people answering the question will know what technique is most appropriate, and it might not be one you thought of.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 31st, 2010, 01:50 PM
Authorized User
 
Join Date: Mar 2010
Posts: 28
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Respected to all,

Thank you for advising me. I gone through every line of the file but i do not understand what this line says. The line is
<td><xsl:value-of select="position()"/></td>

Sir, Can you please explain what the position() method do in this file?

Regards/Thanks,
Dhrumil
 
Old March 31st, 2010, 01:58 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

The outer for-each processes all child elements of the "Lineage" element. And then xsl:value-of select="position()" outputs the position of each child element in the list of all child elements, starting with position 1. So it outputs 1, 2, 3, and so on to the position of the last child element.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old April 1st, 2010, 01:02 AM
Authorized User
 
Join Date: Mar 2010
Posts: 28
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Respected Sir,

Thank you sir for explanation but in this there is an error of undefined Postition() method.

Regards/Thanks,
Dhrumil
 
Old April 1st, 2010, 01:07 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Can you post the whole error text.
__________________
Rummy





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT recursive Additions raafishaafi XSLT 2 January 13th, 2009 11:41 AM
XSLT Loop? kwilliams XSLT 1 June 16th, 2008 06:04 PM
FOR LOOP IN XSLT im_himanshu2004 XSLT 0 October 10th, 2007 02:27 PM
transform in a loop vs recursive template ramarc XSLT 3 April 10th, 2006 04:40 PM
recursive XSLT Help boates XSLT 2 January 11th, 2006 03:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.