2011-11-18 132 views
0

我發現了一個腳本,可以讓我在網站上實現上一個/下一個導航,但我不確定它是否正確。這個XSLT是否正確?

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:output method="html" version="3.2" encoding="ISO-8859-1"/> 
<xsl:param name="Page" select="0" /> 
<xsl:param name="PageSize" select="1" /> 

<xsl:template name="results" match="/"> 


<xsl:variable name="mycount" select="count(root/customer)"/> 
<xsl:variable name="selectedRowCount" select="floor((number($numberOfRecords)-1) div 
$recordsPerPage)+1"/> 


    <xsl:for-each select="root/customer"> 
    <!-- Pagination logic --> 
    <xsl:if test="position() &gt;= ($Page * $PageSize) + 1"> 
    <xsl:if test="position() &lt;= $PageSize + ($PageSize * $Page)"> 


    <!-- Do display here --> 

    </xsl:if> 
    </xsl:if> 
    </xsl:for-each> 


    <!-- Prev link for pagination --> 
    <xsl:choose> 
    <xsl:when test="number($Page)-1 &gt;= 0">&#160; 
    <A> 
    <xsl:attribute name="href">_dirresult?page=<xsl:value-of select="number($Page)- 
    1"/>&amp;pagesize=<xsl:value-of select="$PageSize"/></xsl:attribute> 
     &lt;&lt;Prev 
    </A> 
    </xsl:when> 
    <xsl:otherwise> 
    <!-- display something else --> 
    </xsl:otherwise> 
    </xsl:choose> 

    <xsl:if test="$selectedRowCount &gt; 1"> 
    &#160;<b class="blacktext"><xsl:value-of select="number($Page)+1"/>&#160;of&#160;<xsl:value-of select="number($selectedRowCount)"/></b>&#160; 
    </xsl:if> 

    <!-- Next link for pagination --> 
    <xsl:choose> 
    <xsl:when test="number($Page)+1 &lt; number($selectedRowCount)">&#160; 
    <A> 
    <xsl:attribute name="href">_dirresult?page=<xsl:value-of select="number($Page)    
    +1"/>&amp;pagesize=<xsl:value-of select="$PageSize"/></xsl:attribute> 
     Next&gt;&gt; 
    </A> 
    </xsl:when> 
    <xsl:otherwise> 
    <!-- display something else --> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 

該腳本是否正確?據我所知,當你在第1頁第1條時,「不要在這裏顯示」是永遠不會達到的。

回答

2

找出它是否有效的方法是嘗試一下!

您需要做的第一件事是修正一些錯誤。這個變量的聲明是完全錯誤的,因爲它引用不存在的

<xsl:variable name="selectedRowCount" 
    select="floor((number($numberOfRecords)-1) div $recordsPerPage)+1"/> 

$ numberOfRecords大概應該是mycount的,並$ recordsPerPage應該$每頁

從其他兩個變量看着XSLT,它期望的列表客戶元素,像這樣...

<root> 
    <customer>Bob 1</customer> 
    <customer>Bob 2</customer> 
    <customer>Bob 3</customer> 
</root> 

嘗試過它自己,它似乎工作,但要注意不要承擔它假定頁編號從0開始,而不是1,所以如果你想要顯示的第一頁,可以設置$頁參數0.不要驚慌,它會在輸出中顯示第1頁......

所以,給它一個去看看你是怎麼回事...

+1

奧克,我會但我有一個問題。我還發現了這個腳本(http://www.bobulous.org.uk/coding/paging-xslt.html),它看起來更像我。您是否同意 –

+0

如果您能夠使用XSLT 2.0,那麼它可能是一個更好的選擇。再次嘗試一下,看看你的想法! –