0

Need (again) some help on paginate a rather simple xml table data displaying only one record at a time, but as a stacked nodes like this:

 |================|
 |      FNAME     |
 |----------------| 
 |      Smith     |   
 |----------------| 
 |     LNAME      | 
 |----------------| 
 |     Milton     |
 |----------------|
 |       AGE      | 
 |----------------| 
 |       44       | 
 |----------------|
 |     ADDRESS    |  
 |----------------|
 |5th smmr st,mntb| 
 |----------------| 
 |      CITY      | 
 |----------------|  
 |    Portland    |  
 |================| 
<<  < pag 1/6 >  >>
  =================

and with "first prev page next last" links at a bottom of a table for one to advance over next prev first and last record; as it is showcased up there in a picture. Those <a href links are, of course, bound by few js functions which I also showed further down...
This is rather an old question raised up here a couple of years ago: XML table columns vertically stacked within rows but hasn't got those moving around links ...
Xml file is as follows:

<persns> 
 <prsn> 
  <fname>Smith</fname> 
  <lname>Milton</lname> 
  <age>44</age> 
  <addrss>5th summer st, mntb</addrss>
  <city>Portland</city>
 </prsn>
 <prsn> 
  <fname>Ken</fname> 
  <lname>Jackson</lname> 
  <age>37</age> 
  <addrss>19th Penfield ave, brtcl</addrss>
  <city>Kelowna</city>
 </prsn>
 <prsn> 
  <fname>Susan</fname> 
  <lname>Arkland</lname> 
  <age>48</age> 
  <addrss>34th Mansfield st, sgtp</addrss>
  <city>Raleigh</city>
 </prsn>
 <prsn> 
  <fname>Smith</fname> 
  <lname>Milton</lname> 
  <age>44</age> 
  <addrss>5th summer st, mntb</addrss>
  <city>Portland</city>
 </prsn>
 <prsn> 
  <fname>Ken</fname> 
  <lname>Jackson</lname> 
  <age>37</age> 
  <addrss>19th Penfield ave, brtcl</addrss>
  <city>Kelowna</city>
 </prsn>
 <prsn> 
  <fname>Susan</fname> 
  <lname>Arkland</lname> 
  <age>48</age> 
  <addrss>34th Mansfield st, sgtp</addrss>
  <city>Raleigh</city>
 </prsn>
</persns>

and xslt is also like this:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:variable name="pags" select="count(//prsn)"/>
<xsl:template match="prsns">
 <xsl:apply-templates select="prsn[position()]"/>
</xsl:template>
<xsl:template match="prsn">
<xsl:variable name="pag" select="position()"/>
 <table border="1" id='content{$pag}' width="170" height="170" style="text-align:center;margin-left:297px; border-collapse:collapse; margin-top:22px;">
 <tr><th>Frst Name</th></tr><tr><td><xsl:value-of select="."/></td></tr>  
 <tr><th>Last Name</th></tr><tr><td><xsl:value-of select="."/></td></tr> 
 <tr><th>Age</th></tr> <tr><td><xsl:value-of select="."/> </td></tr>
 <tr><th>Address</th></tr> <tr><td><xsl:value-of select="."/> </td></tr>
  <tr><th>City</th></tr><tr><td><xsl:value-of select="."/></td></tr>
   <!-- pagination area -->
 <tr>   
 <td colspan="4" style="background-color: #FCF3CF"> <!-- #FCF3CF -->
 <div class="bpagn" style="margin-top:3.99px;height:21px;margin-left:23px; margin-right:30px">
  <xsl:choose>
   <xsl:when test="$pag = 1">
   <xsl:text>pag</xsl:text><xsl:text> </xsl:text>
  <xsl:value-of select="$pag"/>
  <xsl:value-of select="'/'"/>      
  <xsl:value-of select="$pags"/>      
  <xsl:text>&#x20;</xsl:text>
  <!--<xsl:value-of select="''"/> -->
 <a href="#{$pag+1}" onclick="nextPage({$pag+1})">&#8250;</a> <!--  >  -->
 <xsl:text>&#x20;</xsl:text>
 <a href="#{$pags}" onclick="lastPage({$pags})">&#187;</a>  <!-- >> -->
   </xsl:when>
   <xsl:when test="$pag = $pags">
    <xsl:value-of select="' '"/>
 <a href="#{$pags - ($pags -1)}" onclick="firstPage({$pags - ($pags -1)})">&#171;</a>  <!--  << -->
 <a href="#{$pag - 1}" onclick="prevPage({$pag - 1})">&#8249;</a>  <!--  < -->
   <xsl:text> </xsl:text> <xsl:text>pag</xsl:text>  
  <xsl:value-of select="' '"/> 
  <xsl:value-of select="$pag"/>
  <xsl:value-of select="'/'"/>  
  <xsl:value-of select="$pags"/>
 </xsl:when>
 <xsl:otherwise>
 <a href="#" onclick=" ">&#171;</a>   <!--  << -->
 <a href="#{$pag - 1}" onclick="prevPage({$pag - 1})">&#8249;</a>  <!--  < -->
   <xsl:text>pag</xsl:text>
   <xsl:value-of select="' '"/>      
   <xsl:value-of select="$pag"/>      
   <xsl:value-of select="'/'"/> 
   <xsl:value-of select="$pags"/>
   <xsl:value-of select="' '"/>
 <a href="#{$pag+1}" onclick="nextPage({$pag+1})">&#8250;</a>  <!--  >  -->
  <a href="#" onclick="">&#187;</a> <!--  >> -->
   </xsl:otherwise>
  </xsl:choose>
  </div>
  </td>
 </tr>
</table>
</xsl:template>
</xsl:stylesheet>

I've got also a small js script through which paging advance is employed:

function nextPage(num)
{
document.getElementById("content"+num).style.display=""
 num--
document.getElementById("content"+num).style.display="none"
}

function prevPage(num)
{
document.getElementById("content"+num).style.display=""
 num++
document.getElementById("content"+num).style.display="none"
}

function lastPage(num)
{
document.getElementById("content"+num).style.display=""
 num = num-(num-1) //2 
document.getElementById("content"+num).style.display="none"
}

function firstPage(num)
{
document.getElementById("content"+num).style.display=""
 num += num + num // 2 (num-1)
document.getElementById("content"+num).style.display="none"
}

Of course all these goes through a some index.html where everything should be displayed over the web. Got a couple of problems though:

  • xslt stylesheet's condition is wrong: <xsl:template match="prsns"> <xsl:apply-templates select="prsn[position()]"/> </xsl:template>
  • and also individual table cells displaying is also wrong
as I need to display each and every xml node value for one html table row Aș such the whole displaying is something like: [html table rows wrong display ](https://i.stack.imgur.com/kEw6Q.png) So you guys please help me with this so I can successfully complete my task Thank you in advance Regards
mirexS
  • 27
  • 4
  • Do you know what HTML you want to generate? If you do, please show your XML source and the HTML output you want to produce. If you don't, then please rephrase this as an HTML question rather than an XSLT question. Don't try to write any XSLT code until you know what HTML output you want it to produce. – Michael Kay Nov 26 '22 at 22:04

1 Answers1

0

The XSLT matches on the wrong element name (e.g. <xsl:template match="prsns"> should be <xsl:template match="persns">), then I think you need to add some HTML structure and make sure you output the script; I couldn't make sense of the last and first page functions so I adjusted them; I also used XSLT 3 instead of 2:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all"
  expand-text="yes">

  <xsl:output method="html" indent="yes" html-version="5"/>

  <xsl:template match="/" name="xsl:initial-template">
    <html>
      <head>
        <title>Test</title>
        <script xsl:expand-text="no">
function nextPage(num)
{
document.getElementById("content"+num).style.display=""
 num--
document.getElementById("content"+num).style.display="none"
}

function prevPage(num)
{
document.getElementById("content"+num).style.display=""
 num++
document.getElementById("content"+num).style.display="none"
}

function lastPage(num)
{
document.getElementById("content"+pages).style.display=""
document.getElementById("content"+num).style.display="none"
}

function firstPage(num)
{
document.getElementById("content"+1).style.display=""
document.getElementById("content"+num).style.display="none"
}          
        </script>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

<xsl:variable name="pags" select="count(//prsn)"/>
<xsl:template match="persns">
 <script>
   var pages = {$pags};
 </script>
 <xsl:apply-templates select="prsn"/>
</xsl:template>
<xsl:template match="prsn">
<xsl:variable name="pag" select="position()"/>
 <table border="1" id='content{$pag}' width="170" height="170" style="text-align:center;margin-left:297px; border-collapse:collapse; margin-top:22px; {if (position() != 1) then 'display: none;' else 'display: table;'}">
 <tr><th>Frst Name</th></tr><tr><td><xsl:value-of select="fname"/></td></tr>  
 <tr><th>Last Name</th></tr><tr><td><xsl:value-of select="lname"/></td></tr> 
 <tr><th>Age</th></tr> <tr><td><xsl:value-of select="age"/> </td></tr>
 <tr><th>Address</th></tr> <tr><td><xsl:value-of select="addrss"/> </td></tr>
  <tr><th>City</th></tr><tr><td><xsl:value-of select="city"/></td></tr>
   <!-- pagination area -->
 <tr>   
 <td colspan="4" style="background-color: #FCF3CF"> <!-- #FCF3CF -->
 <div class="bpagn" style="margin-top:3.99px;height:21px;margin-left:23px; margin-right:30px">
  <xsl:choose>
   <xsl:when test="$pag = 1">
   <xsl:text>pag</xsl:text><xsl:text> </xsl:text>
  <xsl:value-of select="$pag"/>
  <xsl:value-of select="'/'"/>      
  <xsl:value-of select="$pags"/>      
  <xsl:text>&#x20;</xsl:text>
  <!--<xsl:value-of select="''"/> -->
 <a href="#{$pag+1}" onclick="nextPage({$pag+1}); return false;"> &#8250; </a> <!--  >  -->
 <xsl:text>&#x20;</xsl:text>
 <a href="#{$pags}" onclick="lastPage({$pag}); return false;"> &#187; </a>  <!-- >> -->
   </xsl:when>
   <xsl:when test="$pag = $pags">
    <xsl:value-of select="' '"/>
 <a href="#{$pags - ($pags -1)}" onclick="firstPage({$pag}); return false;"> &#171; </a>  <!--  << -->
 <a href="#{$pag - 1}" onclick="prevPage({$pag - 1}); return false;"> &#8249; </a>  <!--  < -->
   <xsl:text> </xsl:text> <xsl:text>pag</xsl:text>  
  <xsl:value-of select="' '"/> 
  <xsl:value-of select="$pag"/>
  <xsl:value-of select="'/'"/>  
  <xsl:value-of select="$pags"/>
 </xsl:when>
 <xsl:otherwise>
 <a href="#" onclick="return false;"> &#171; </a>   <!--  << -->
 <a href="#{$pag - 1}" onclick="prevPage({$pag - 1}); return false;"> &#8249; </a>  <!--  < -->
   <xsl:text>pag</xsl:text>
   <xsl:value-of select="' '"/>      
   <xsl:value-of select="$pag"/>      
   <xsl:value-of select="'/'"/> 
   <xsl:value-of select="$pags"/>
   <xsl:value-of select="' '"/>
 <a href="#{$pag+1}" onclick="nextPage({$pag+1}); return false"> &#8250; </a>  <!--  >  -->
  <a href="#" onclick="return false;"> &#187; </a> <!--  >> -->
   </xsl:otherwise>
  </xsl:choose>
  </div>
  </td>
 </tr>
</table>
</xsl:template>
</xsl:stylesheet>

Output e.g.

<!DOCTYPE HTML><html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Test</title><script>
function nextPage(num)
{
document.getElementById("content"+num).style.display=""
 num--
document.getElementById("content"+num).style.display="none"
}

function prevPage(num)
{
document.getElementById("content"+num).style.display=""
 num++
document.getElementById("content"+num).style.display="none"
}

function lastPage(num)
{
document.getElementById("content"+pages).style.display=""
document.getElementById("content"+num).style.display="none"
}

function firstPage(num)
{
document.getElementById("content"+1).style.display=""
document.getElementById("content"+num).style.display="none"
}          
        </script></head>
   <body><script>
   var pages = 6;
 </script><table border="1" id="content1" width="170" height="170" style="text-align:center;margin-left:297px; border-collapse:collapse; margin-top:22px; display: table;">
         <tr>
            <th>Frst Name</th>
         </tr>
         <tr>
            <td>Smith</td>
         </tr>
         <tr>
            <th>Last Name</th>
         </tr>
         <tr>
            <td>Milton</td>
         </tr>
         <tr>
            <th>Age</th>
         </tr>
         <tr>
            <td>44</td>
         </tr>
         <tr>
            <th>Address</th>
         </tr>
         <tr>
            <td>5th summer st, mntb</td>
         </tr>
         <tr>
            <th>City</th>
         </tr>
         <tr>
            <td>Portland</td>
         </tr>
         <tr>
            <td colspan="4" style="background-color: #FCF3CF">
               <div class="bpagn" style="margin-top:3.99px;height:21px;margin-left:23px; margin-right:30px">pag 1/6 <a href="#2" onclick="nextPage(2); return false;"> › </a> <a href="#6" onclick="lastPage(1); return false;"> » </a></div>
            </td>
         </tr>
      </table>
      <table border="1" id="content2" width="170" height="170" style="text-align:center;margin-left:297px; border-collapse:collapse; margin-top:22px; display: none;">
         <tr>
            <th>Frst Name</th>
         </tr>
         <tr>
            <td>Ken</td>
         </tr>
         <tr>
            <th>Last Name</th>
         </tr>
         <tr>
            <td>Jackson</td>
         </tr>
         <tr>
            <th>Age</th>
         </tr>
         <tr>
            <td>37</td>
         </tr>
         <tr>
            <th>Address</th>
         </tr>
         <tr>
            <td>19th Penfield ave, brtcl</td>
         </tr>
         <tr>
            <th>City</th>
         </tr>
         <tr>
            <td>Kelowna</td>
         </tr>
         <tr>
            <td colspan="4" style="background-color: #FCF3CF">
               <div class="bpagn" style="margin-top:3.99px;height:21px;margin-left:23px; margin-right:30px"><a href="#" onclick="return false;"> « </a><a href="#1" onclick="prevPage(1); return false;"> ‹ </a>pag 2/6 <a href="#3" onclick="nextPage(3); return false"> › </a><a href="#" onclick="return false;"> » </a></div>
            </td>
         </tr>
      </table>
      <table border="1" id="content3" width="170" height="170" style="text-align:center;margin-left:297px; border-collapse:collapse; margin-top:22px; display: none;">
         <tr>
            <th>Frst Name</th>
         </tr>
         <tr>
            <td>Susan</td>
         </tr>
         <tr>
            <th>Last Name</th>
         </tr>
         <tr>
            <td>Arkland</td>
         </tr>
         <tr>
            <th>Age</th>
         </tr>
         <tr>
            <td>48</td>
         </tr>
         <tr>
            <th>Address</th>
         </tr>
         <tr>
            <td>34th Mansfield st, sgtp</td>
         </tr>
         <tr>
            <th>City</th>
         </tr>
         <tr>
            <td>Raleigh</td>
         </tr>
         <tr>
            <td colspan="4" style="background-color: #FCF3CF">
               <div class="bpagn" style="margin-top:3.99px;height:21px;margin-left:23px; margin-right:30px"><a href="#" onclick="return false;"> « </a><a href="#2" onclick="prevPage(2); return false;"> ‹ </a>pag 3/6 <a href="#4" onclick="nextPage(4); return false"> › </a><a href="#" onclick="return false;"> » </a></div>
            </td>
         </tr>
      </table>
      <table border="1" id="content4" width="170" height="170" style="text-align:center;margin-left:297px; border-collapse:collapse; margin-top:22px; display: none;">
         <tr>
            <th>Frst Name</th>
         </tr>
         <tr>
            <td>Smith</td>
         </tr>
         <tr>
            <th>Last Name</th>
         </tr>
         <tr>
            <td>Milton</td>
         </tr>
         <tr>
            <th>Age</th>
         </tr>
         <tr>
            <td>44</td>
         </tr>
         <tr>
            <th>Address</th>
         </tr>
         <tr>
            <td>5th summer st, mntb</td>
         </tr>
         <tr>
            <th>City</th>
         </tr>
         <tr>
            <td>Portland</td>
         </tr>
         <tr>
            <td colspan="4" style="background-color: #FCF3CF">
               <div class="bpagn" style="margin-top:3.99px;height:21px;margin-left:23px; margin-right:30px"><a href="#" onclick="return false;"> « </a><a href="#3" onclick="prevPage(3); return false;"> ‹ </a>pag 4/6 <a href="#5" onclick="nextPage(5); return false"> › </a><a href="#" onclick="return false;"> » </a></div>
            </td>
         </tr>
      </table>
      <table border="1" id="content5" width="170" height="170" style="text-align:center;margin-left:297px; border-collapse:collapse; margin-top:22px; display: none;">
         <tr>
            <th>Frst Name</th>
         </tr>
         <tr>
            <td>Ken</td>
         </tr>
         <tr>
            <th>Last Name</th>
         </tr>
         <tr>
            <td>Jackson</td>
         </tr>
         <tr>
            <th>Age</th>
         </tr>
         <tr>
            <td>37</td>
         </tr>
         <tr>
            <th>Address</th>
         </tr>
         <tr>
            <td>19th Penfield ave, brtcl</td>
         </tr>
         <tr>
            <th>City</th>
         </tr>
         <tr>
            <td>Kelowna</td>
         </tr>
         <tr>
            <td colspan="4" style="background-color: #FCF3CF">
               <div class="bpagn" style="margin-top:3.99px;height:21px;margin-left:23px; margin-right:30px"><a href="#" onclick="return false;"> « </a><a href="#4" onclick="prevPage(4); return false;"> ‹ </a>pag 5/6 <a href="#6" onclick="nextPage(6); return false"> › </a><a href="#" onclick="return false;"> » </a></div>
            </td>
         </tr>
      </table>
      <table border="1" id="content6" width="170" height="170" style="text-align:center;margin-left:297px; border-collapse:collapse; margin-top:22px; display: none;">
         <tr>
            <th>Frst Name</th>
         </tr>
         <tr>
            <td>Susan</td>
         </tr>
         <tr>
            <th>Last Name</th>
         </tr>
         <tr>
            <td>Arkland</td>
         </tr>
         <tr>
            <th>Age</th>
         </tr>
         <tr>
            <td>48</td>
         </tr>
         <tr>
            <th>Address</th>
         </tr>
         <tr>
            <td>34th Mansfield st, sgtp</td>
         </tr>
         <tr>
            <th>City</th>
         </tr>
         <tr>
            <td>Raleigh</td>
         </tr>
         <tr>
            <td colspan="4" style="background-color: #FCF3CF">
               <div class="bpagn" style="margin-top:3.99px;height:21px;margin-left:23px; margin-right:30px"> <a href="#1" onclick="firstPage(6); return false;"> « </a><a href="#5" onclick="prevPage(5); return false;"> ‹ </a> pag 6/6</div>
            </td>
         </tr>
      </table>
   </body>
</html>

Online sample using SaxonJS.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • @mirexS, in the output you can see that e.g. `` has the inline CSS style property `display: none;` so my code as posted certainly doesn't display all records initially or at once. – Martin Honnen Nov 27 '22 at 12:58
  • @mirexS, see also the link to online sample, one table at a time is shown if you use the "transform" button of the fiddle and look at the rendered output in the right bottom pane. – Martin Honnen Nov 27 '22 at 13:00
  • oh.. ok, right, but when I put diplay:none; it doesn't show up a thing! Tryied a couple of times.. Deleted the cache also.. Nothing shows up. And when I revert display: it does show up everything well grouped by table... – mirexS Nov 27 '22 at 13:07
  • I'll go over the code for another quick check.. and I'll get back.. – mirexS Nov 27 '22 at 13:15
  • Which XSLT processor are you using? The XSLT I posted computes the `display` in the expression `if (position() != 1) then 'display: none;' else 'display: table;'` of the line ``. I don't remember for sure whether `if (exp) then exp else exp` is XPath 2 and 3 or 3 only.
    – Martin Honnen Nov 27 '22 at 13:24
  • https://www.w3.org/TR/xpath20/#id-conditionals shows that `if (exp) then exp else exp` is in XPath 2 so the posted code should work as posted to ensure the first table is visible and the following ones are not initially. – Martin Honnen Nov 27 '22 at 13:30
  • alright, it worked! Thank you very much! yes, a couple of little details which ain't it noticed! Worked well, thanks again! – mirexS Nov 27 '22 at 13:30