xsl 中的分页 [英] pagination in xsl
问题描述
我有一个 xml 文件和一个 xsl 文件,我为它编写了一个生成 html 的文件.我的 xml 文件是:
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>
和我的 xsl 文件:
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, ' ')" />
<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>
xml中的信息太长,应该在一些页面中显示.
Information in xml are too long, that should be display in some pages.
我需要使用分页.我搜索并我找到了这个页面(关于Xslt Paging 示例)帮助我做到这一点.
I need to use pagination. I searched and I find this page (about Xslt Paging example) to help me do this.
它没有任何 xml 文件,我无法完全理解该怎么做(我是 xsl 新手).
It didn't have any xml files and I can't completely understand what to do (I'm new in xsl).
我可以在没有 umbraco 的情况下做到这一点吗?
Can I do this without umbraco or not?
推荐答案
I.这是一个展示如何实现分页"的小例子
给定这个源 XML 文档:
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>
并且给定 3
的页面大小,这个 XSLT 1.0 转换:
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>
生产:
<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>
请注意:
仅使用纯 XSLT 1.0 不可能通过一次转换生成多个结果文档.
It isn't possible by only using pure XSLT 1.0 to produce more than one result document by a single transformation.
您可以使用 XSLT 2.0 及其 xsl:result-document
指令,或实现 EXSLT 的
扩展元素的 XSLT 1.0 处理器.另一种选择是为每个输出页面启动单独的 XSLT 1.0 转换.
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.
二.使用 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>
当此转换应用于同一个 XML 文档时(上图).它成功地创建了四个结果文档.
Saxon 8.7.01 发布了这个:
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-DDNfxsl-xslt2data
umList.xml
Building tree for file:/C:CVS-DDNfxsl-xslt2data
umList.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
这篇关于xsl 中的分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!