xslt showing a group of n xml nodes one at a time

2020-02-16 02:58发布

问题:

update:

This question has some little xsl code in it !

I'm struggling with this problem for weeks (even months). I'm rather beginner through this xml/xslt area and I only need at least one working pagination technique for my xml data. I just can't seem to find just ONE working solution about this issue!!

I already state that, in my view, pagination is one of the most useful xml/xslt web topics and is one of the less discussed topics over the whole web. Only portions of it and most of them non-functional. Anyway, it's all about this stkovrflw question:

xslt xml table records pagination

where one could see my whole problem. It has been answered by michael.hor257k which I thank him very much. Point is it's only halfway working (as far as my requirement concerned)

I only need to display 5 nodes at a time, based on some parameter passed in which is the page number. I'll name it as

param name = "pagnmbr"

And, at first if this $pagnmbr is 1, that xslt will display:

01 02 03 04 05

Else, if it's 2 ($pagnmbr = 2), it will display:

06 07 08 09 10

And if same parameter become 3, the reading will be:

11 12 13 14 15

And so on. See, this kind of functionality I'm in very need for doing.

update: I just did the following xslt code:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:param name="crrtPag" select="2"/>     <!-- current page -->
 <xsl:variable name="rcrdsppag" select="5"/> <want 5 displaying records per page -->

 <xsl:template name="pag" match="/group/item">
 <html>
  <body>
   <xsl:if test="position()-$crrtPag = ($rcrdsppag - 1)*($crrtPag - 1)"> 
    <xsl:call-template name="display_pag">
    <xsl:with-param name="crrtPag" select="item[position() mod $rcrdsppag = 1]"/>
    </xsl:call-template>
   </xsl:if>
   </body>
   </html>
  </xsl:template>

  <xsl:template name="display_pag" match="item"> <!-- item -->
   <xsl:param name="crrtPag"/> 
     <xsl:for-each select="//item"> 
       <xsl:if test="position()&lt;=5">
         <xsl:value-of select="."/> 
       </xsl:if>
     </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

But, as one could notice, it's kept showing only:

01 02 03 04 05

even if I put $pagnmbr = 2, or $pagnmbr = 3 .. pop up only the first 5 group items. It never shows up next node-set of

06 07 08 09 10

and so on ..

Please you guys help me with it !!! Thank you very much in advance.

回答1:

Given:

XML

<group>
    <item> 01 </item>
    <item> 02 </item>
    <item> 03 </item>
    <item> 04 </item>
    <item> 05 </item>
    <item> 06 </item>
    <item> 07 </item>
    <item> 08 </item>
    <item> 09 </item>
    <item> 10 </item>
    <item> 11 </item>
    <item> 12 </item>
    <item> 13 </item>
    <item> 14 </item>
    <item> 15 </item>
    <item> 16 </item>
    <item> 17 </item>
    <item> 18 </item>
    <item> 19 </item>
    <item> 20 </item>
    <item> 21 </item>
    <item> 22 </item>
    <item> 23 </item>
    <item> 24 </item>
    <item> 25 </item>
    <item> 26 </item>
</group>

the following stylesheet:

XSLT 1.0

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

<xsl:param name="pageSize" select="5" />
<xsl:param name="pageNumber" select="3" />

<xsl:template match="/group">
    <html>
        <body>
            <div id="page{$pageNumber}">
                <ul>
                    <xsl:variable name="start" select="$pageSize * ($pageNumber - 1) + 1" />
                    <xsl:for-each select="item[position() >= $start and position() &lt; $start + $pageSize]">     
                        <li>
                            <xsl:value-of select="."/>
                        </li>
                    </xsl:for-each>
                </ul>
            </div>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

will return:

Result

<html>
    <body>
        <div id="page3">
            <ul>
            <li> 11 </li>
            <li> 12 </li>
            <li> 13 </li>
            <li> 14 </li>
            <li> 15 </li>
            </ul>
        </div>
    </body>
</html>


标签: xml xslt