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 27th, 2006, 12:17 PM
Authorized User
 
Join Date: Mar 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default An equivalent to for i = 1 to 10 loop

Can anyone please help me. I am new to xslt. I am trying to find and equivalent to the below code.

for i = 1 to 10
   'do something here
    build html code

    i = i + 1
next

Can anyone please help me. I am trying to do paging and display page numbers from 1 to 10 where i needs to iterate through 1 to 10 dynamically because 10 is going to change all the time. I do not know how to loop though so i can go to the next step. Can we do it in xslt or not atleast. If we cant do it using xslt, then any other pointers will be helpful. Please help me. Any help would be greatly appreciated.

Thanks

 
Old March 27th, 2006, 01:09 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XSLT 2.0 you can do

<xsl:for-each select="1 to 10">

Of course you don't do i=i+1 - there's no variable assignment in XSLT. But you can refer to the current item as ".".

In 1.0 the usual workaround is to select an arbitrary node-set that has at least 10 members - for example (//node()) - and iterate over its first 10 members:

<xsl:for-each select="(//node())[position() &lt; 11]">

and position() tells you where you are in the loop.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 27th, 2006, 01:10 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I should have said that in both the above examples, you can replace 10 by a variable.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 28th, 2006, 11:58 AM
Authorized User
 
Join Date: Mar 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael, Thank you very much for your reply. I really appreciate it.

Can you please look at my code and tell me where i am doing wrong. Its been 2 weeks and still i am not able to proceed.

Here is my xml file.

- <Root>
- <Forms>
    + <Form>
    + <Form>
    + <Form>
    + <Form>
    + <Form>
    + <Form>
    + <Form>
    + <Form>
    + <Form>
    + <Form>
  </Forms>
- <Buttons>
  <Previous>10</Previous>
  <Next>30</Next>
  <CurrentPage>3</CurrentPage>
  </Buttons>
  <Records>88</Records>
  <CurrentPage>3</CurrentPage>
  <NoOfPages>9</NoOfPages>
- <Pages>
  <Page>1</Page>
  <Page>2</Page>
  <Page>3</Page>
  <Page>4</Page>
  <Page>5</Page>
  <Page>6</Page>
  <Page>7</Page>
  <Page>8</Page>
  <Page>9</Page>
  </Pages>
  <BeginRecord>21</BeginRecord>
  <EndRecord>30</EndRecord>
  <catg>5</catg>
  </Root>

Here is the code from my xslt file. The code that i am trying to use to print page numbers is between xsl:foreach select="Pages" and then using xsl:value-of select="page". All i am trying to do is print the value of all the page nodes. Hope i am explaining it right. If i am doing it wrong, please tell me how to do it right. I need to get this right immediately. Thank you once agian for your help.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <html>
        <xsl:apply-templates/>
    </html>
</xsl:template>

<xsl:template match="Root">

    <xsl:param name="page-num" select="1"/>


    <xsl:variable name="page-size" select="20" />

    <xsl:for-each select="Pages">
        <xsl:value-of select="Page" />
    </xsl:for-each>

<SCRIPT LANGUAGE="JavaScript">
function DownLoad() {
    var url;
    url = location.href;
    window.open(url, '640x480', '_popup', 'height=640, width=480, scrollbars, left=10, top=10, screenX=10, screenY=10');
}
</SCRIPT>

<table border="0" cellpadding="3" cellspacing="0" width="600">
    <tr>
        <td colspan="3">
            <span class="bc2">
            <xsl:value-of select="Records"/> Forms</span>

        </td>
        <td colspan="2" align="right">
            <span class="bc2">
            <xsl:value-of select="BeginRecord"/> of
            <xsl:value-of select="EndRecord"/></span>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <span class="bc3"><br/>
            </span>
        </td>
    </tr>
<xsl:for-each select="Root">
        <xsl:apply-templates select="CurrentPage"/>
        <xsl:apply-templates select="NoOfPages"/>
    </xsl:for-each>
    <tr>
        <td colspan="2">
            <span class="bc3">
                <xsl:value-of select="NoOfPages"/>
            </span>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <span class="bc3"><br/>
            </span>
        </td>
    </tr>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="600" bgcolor="white">
    <tr bgcolor="#7f0000">
        <td colspan="6"><span class="bh3">Forms</span></td>
    </tr>
    <tr bgcolor="#cccccc">
        <td width="28"></td>
        <td width="200">
            <span class="bc2">Name </span>
        </td>
        <td width="267">
            <span class="bc2">Version</span>
        </td>
        <td align="right" width="10">
            <a href="#" onclick="window.open('help/help_forms.html', '_popup', 'height=315, width=315, scrollbars, left=10, top=10, screenX=10, screenY=10');return false">
                <img src="../images/icons_help.gif" width="12" height="12" hspace="1" border="0"/>
            </a>
        </td>
        <td align="right" width="75">
            <span class="bc2">Options</span>
        </td>


    </tr>
    <xsl:for-each select="Forms">
        <xsl:apply-templates select="Form"/>
    </xsl:for-each>
    <tr align="center">
        <td colspan="5">
            <form name="Next" method="Get" action="catlist.asp?paging=next?page=CurrentPage">
            <input type="hidden" name="catg">
                <xsl:attribute name="value">
                    <xsl:value-of select="catg"/>
                </xsl:attribute>
            </input>
            <input type="hidden" name="CurrentPage">
                <xsl:attribute name="value">
                    <xsl:value-of select="CurrentPage"/>
                </xsl:attribute>
            </input>
            <xsl:if test="Buttons/Previous">

                <input type="submit" value="Previous" name="Next" class="bc2" />
                <input type="hidden" name="PreviousRS">
                    <xsl:attribute name="value">
                        <xsl:value-of select="Buttons/Previous"/>
                    </xsl:attribute>
                </input>

            </xsl:if>
            <xsl:if test="Buttons/Next">

                <input type="submit" value=" Next " name="Next" class="bc2"/>
                <input type="hidden" name="NextRS">
                    <xsl:attribute name="value">
                        <xsl:value-of select="Buttons/Next"/>
                    </xsl:attribute>
                </input>

            </xsl:if>
            </form>
        </td>
    </tr>
</table>
</xsl:template>

<xsl:template match="Form">
<tr>
        <xsl:attribute name="bgcolor">
            <xsl:value-of select="Color"/>
        </xsl:attribute>
        <td width="28">
            <img>
            <xsl:attribute name="src">
                <xsl:value-of select="Img"/>
            </xsl:attribute>
            </img>
        </td>
        <td width="200">
            <span class="bc3">
                <xsl:value-of select="Name"/>
            </span>
        </td>
        <td colspan="2">
            <span class="bc3">
                <xsl:value-of select="Desc"/>
            </span>
        </td>
        <td align="right" width="85">
            <xsl:if test="Preview">
                  <a>
                    <xsl:attribute name="href">
                        <xsl:value-of select="Preview"/>
                    </xsl:attribute>
                    <xsl:attribute name="Target">
                        New
                    </xsl:attribute>
                    <img src="../images/preview.gif" border="0"/>
                </a>
             </xsl:if>
            <xsl:if test="Download">
                  <a>
                    <xsl:attribute name="href">
                        <xsl:value-of select="Down"/>
                    </xsl:attribute>
                    <img src="../images/down.gif" border="0"/>
                </a>
             </xsl:if>
            <xsl:if test="Modify">
                  <a>
                    <xsl:attribute name="href">
                        <xsl:value-of select="Modify"/>
                    </xsl:attribute>
                    <img src="../images/modify.gif" border="0"/>
                </a>
             </xsl:if>
            <xsl:if test="OrderFax">
                <a target="new">
                    <xsl:attribute name="href">
                        <xsl:value-of select="OM"/>
                    </xsl:attribute>
                    <img align="center" src="../images/rx.gif" border="0"/>
                </a>
            </xsl:if>
        </td>
    </tr>
</xsl:template>
</xsl:stylesheet>


 
Old March 28th, 2006, 12:22 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

    <xsl:for-each select="Pages">
        <xsl:value-of select="Page" />
    </xsl:for-each>

There is only one Pages element so you only go round this loop once. The <xsl:value-of select="Page" selects all the Page elements and displays the first.

You want

    <xsl:for-each select="Pages/Page">
        <xsl:value-of select="." />
    </xsl:for-each>

although this will string all the page numbers together and you probably want some kind of separator.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 28th, 2006, 12:51 PM
Authorized User
 
Join Date: Mar 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you thank you thank you thank you......so..... much.

I cant thank you enough. It worked. You dont know i have been working on this for so long and lost all my hopes. I know this is not all done. But at least i am able to pass through this step. Hopefully i will be able to finish it soon. Thank you once again for all your help. You are the greatest. You made my day.






Similar Threads
Thread Thread Starter Forum Replies Last Post
description != '' equivalent XMLUser XSLT 3 March 1st, 2008 12:58 PM
"HtmlAnchor" equivalent arunagottimukkala ASP.NET 1.0 and 1.1 Professional 0 November 5th, 2007 06:14 AM
nested while loop doesn't loop hosefo81 PHP Databases 5 November 12th, 2003 08:46 AM
javascript equivalent lcsgeek Classic ASP Basics 0 September 22nd, 2003 07:17 AM
ASP Equivalent U.N.C.L.E. Classic ASP Basics 3 July 8th, 2003 03:05 AM





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