pagination in xsl

2019-02-19 23:23发布

I have an xml file and also an xsl file which I wrote for it to generate html. my xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<expert_questions>
    <question Id="10">
        <source_ip>192.168.150.1</source_ip>
        <port>545</port>
        <packet_size>1400</packet_size>
        <more_details>
            <time>13:42</time>
            <count>100</count>
            <comment>more details</comment>
        </more_details>
    </question>
    <question Id="...">
       .
       .
       .
    </question>
</expert_questions>

and my xsl file:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    version="1.0">
        <xsl:output omit-xml-declaration="yes" indent="yes" />
        <xsl:template match="/">
            <xsl:for-each select="expert_questions">
                <table border="1" cellspacing="0" cellpadding="0">
                    <tr class ="table-title">
                        <th class="th">source ip</th>
                        <th class="th">port</th>
                        <th class="th">packet size</th>
                        <th class="th">more details</th>
                    </tr>
                    <xsl:for-each select="question">
                        <tr>
                            <xsl:attribute name="id">
                                <xsl:value-of select="@Id" />
                            </xsl:attribute>

                            <td><xsl:value-of select="source_ip"></xsl:value-of></td> 
                            <td><xsl:value-of select="port"></xsl:value-of></td>
                            <td><xsl:value-of select="packet_size"></xsl:value-of></td>
                            <td>   
                                <xsl:for-each select="more_details">
                                        <xsl:attribute name="title">

                                            <xsl:value-of select="concat('Time: ', time, '&#10;')" />
                                            <xsl:value-of select="concat('Count: ', count)" />
                                            <xsl:value-of select="concat('Comment: ', comment)" />
                                        </xsl:attribute>
                                        <xsl:text>more details</xsl:text>
                                    </a>
                                </xsl:for-each>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </xsl:for-each>
        </xsl:template>

    </xsl:stylesheet>

Information in xml are too long, that should be display in some pages.

I need to use pagination. I searched and I find this page (about Xslt Paging example) to help me do this.

It didn't have any xml files and I can't completely understand what to do (I'm new in xsl).

Can I do this without umbraco or not?

1条回答
We Are One
2楼-- · 2019-02-19 23:52

I. Here is a small example showing how to implement "paging"

Given this source XML document:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

and given a pagesize of 3, this XSLT 1.0 transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*" name="identity">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="num[position() mod 3 = 1]">
  <page>
    <xsl:apply-templates mode="inGroup"
      select=".|following-sibling::num[not(position()>2)]"/>
  </page>
 </xsl:template>

 <xsl:template match="num"/>

 <xsl:template match="num" mode="inGroup">
  <xsl:call-template name="identity"/>
 </xsl:template>
</xsl:stylesheet>

produces:

<nums>
   <page>
      <num>01</num>
      <num>02</num>
      <num>03</num>
   </page>
   <page>
      <num>04</num>
      <num>05</num>
      <num>06</num>
   </page>
   <page>
      <num>07</num>
      <num>08</num>
      <num>09</num>
   </page>
   <page>
      <num>10</num>
   </page>
</nums>

Do note:

It isn't possible by only using pure XSLT 1.0 to produce more than one result document by a single transformation.

You can either use XSLT 2.0 and its xsl:result-document instruction, or an XSLT 1.0 processor that implements EXSLT's <exsl:document> extension element. Another alternative is to initiate a separate XSLT 1.0 transformation for every output page.


II. Paging with XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pPageSize" select="3"/>

 <xsl:template match="/*">
  <xsl:for-each-group select="num" group-adjacent="position() idiv $pPageSize">
     <xsl:result-document href="file:///c:/temp/delete/page{current-grouping-key()+1}">
      <html>
        <xsl:apply-templates mode="inGroup" select="current-group()"/>
      </html>
     </xsl:result-document>
    </xsl:for-each-group>
 </xsl:template>

 <xsl:template match="num" mode="inGroup">
  <p><xsl:value-of select="."/></p>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the same XML document (above). it successfully creates four result documents.

And Saxon 8.7.01 issues this:

Saxon 9.1.0.7J from Saxonica

Java version 1.6.0_17

Stylesheet compilation time: 223 milliseconds

Processing file:/C:\CVS-DDN\fxsl-xslt2\data\numList.xml

Building tree for file:/C:\CVS-DDN\fxsl-xslt2\data\numList.xml using class net.sf.saxon.tinytree.TinyBuilder

Tree built in 2 milliseconds

Tree size: 23 nodes, 20 characters, 0 attributes

Loading net.sf.saxon.event.MessageEmitter

Writing to file:/c:/temp/delete/page1

Writing to file:/c:/temp/delete/page2

Writing to file:/c:/temp/delete/page3

Writing to file:/c:/temp/delete/page4

Execution time: 73 milliseconds

Memory used: 21482856

NamePool contents: 21 entries in 20 chains. 6 prefixes, 7 URIs
查看更多
登录 后发表回答