XSL分页

以RSS为例

<?xml version="1.0" encoding="utf-8"?>
<!--Copyright(C) 2003-2007 Hexsoft.org, All Right Reserved.-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
<xsl:output omit-xml-declaration="yes"/>
 
<!--Declare Current Page-->
 
<xsl:param name="current">1</xsl:param>
 
<!--Declare Page Size-->
 
<xsl:param name="page">10</xsl:param>
 
<!--HTML Template-->
 
<xsl:template match="/">
  
<html xmlns="http://www.w3.org/1999/xhtml">
   
<head>
    
<title>XSL Paging</title>
    
<style type="text/css">*{font-family:Tahoma;font-size:9pt}a{padding:1px}div{text-indent:12px}</style>
    
<script type="text/javascript">function paging(page){var xmlDocument=document.XMLDocument;var xslDocument=document.XSLDocument;xslDocument.selectNodes("xsl:stylesheet/xsl:template[@match='/']").removeAll();xslDocument.selectSingleNode("xsl:stylesheet/xsl:param[@name='current']").text=page;document.body.innerHTML=xmlDocument.transformNode(xslDocument);}</script>
   
</head>
   
<body>
    
<xsl:apply-templates/>
   
</body>
  
</html>
 
</xsl:template>
 
<!--RSS Template-->
 
<xsl:template match="rss">
  
<xsl:apply-templates select="channel[position() &gt;= ($current - 1) * $page and position() &lt;= $current * $page]"/>
  
<xsl:call-template name="page"/>
 
</xsl:template>
 
<!--Channel Template-->
 
<xsl:template match="channel">
  
<href="{link}">
   
<xsl:value-of select="title"/>
  
</a>
  
<div>
   
<xsl:value-of select="description"/>
  
</div>
 
</xsl:template>
 
<!--Page Template-->
 
<xsl:template name="page">
  
<xsl:for-each select="channel[position() mod $page = 1]">
   
<href="javascript :paging({position()})">
    
<xsl:value-of select="position()"/>
   
</a>
  
</xsl:for-each>
 
</xsl:template>
</xsl:stylesheet>


更新:支持firefox

<?xml version="1.0" encoding="utf-8"?>
<!--Copyright&copy; 2003-2007 Hexsoft.org, All Right Reserved.-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
<xsl:output method="html" indent="no" omit-xml-declaration="yes" />
 
<!--Declare Current Page-->
 
<xsl:param name="page">1</xsl:param>
 
<!--HTML Template-->
 
<xsl:template match="/">
  
<html xmlns="http://www.w3.org/1999/xhtml">
   
<head>
    
<title>
     
<xsl:value-of select="comment()" />
    
</title>
    
<style type="text/css">
body
{
 font-family: Tahoma;
 font-size: 9pt;
}
a
{
 padding-right: 1px;
}
b
{
 padding-right: 1px;
}
p
{
 margin: 0px 0px 0px 16px;
}
    
</style>
    
<script type="text/javascript">
function loadXml(xmlSource)
{
 var xmlDocument = document.implementation.createDocument("", "", null);
 xmlDocument.async = false;
 xmlDocument.load(xmlSource);
 return xmlDocument;
}
function loadXsl(xmlDocument)
{
 var xslDocument = new XSLTProcessor();
 xslDocument.importStylesheet(xmlDocument);
 return xslDocument;
}
function paging(page)
{
 if(window.ActiveXObject)
 {
  var xmlDocument = document.XMLDocument;
  var xslDocument = document.XSLDocument;
  xslDocument.getElementsByTagName("xsl:param")[0].text = page;
  var template = xslDocument.getElementsByTagName("xsl:template")[0];
  if(template.attributes[0].value == "/")
  {
   template.parentNode.removeChild(template);
  }
  document.getElementById("rss").parentNode.innerHTML = xmlDocument.transformNode(xslDocument);
 }
 else
 {
  var xmlDocument = loadXml(window.location);
  var xslDocument = loadXml("page.xsl");
  xslDocument.getElementsByTagName("param")[0].textContent = page;
  xslDocument.documentElement.removeChild(xslDocument.getElementsByTagName("template")[0]);
  var rss = document.getElementById("rss");
  rss.parentNode.replaceChild(loadXsl(xslDocument).transformToFragment(xmlDocument, document), rss);
 }
}
    
</script>
   
</head>
   
<body>
    
<xsl:apply-templates />
   
</body>
  
</html>
 
</xsl:template>
 
<!--RSS Template-->
 
<xsl:template match="rss">
  
<div id="rss">
   
<xsl:apply-templates select="channel[position() &gt;= 10 * ($page - 1) and position() &lt;= 10 * $page]" />
   
<xsl:apply-templates select="channel[position() mod 10 = 1]" mode="page" />
  
</div>
 
</xsl:template>
 
<!--Channel Template-->
 
<xsl:template match="channel">
  
<href="{link}">
   
<xsl:value-of select="title" />
  
</a>
  
<p>
   
<xsl:value-of select="description" />
  
</p>
 
</xsl:template>
 
<!--Page Template-->
 
<xsl:template match="channel" mode="page">
  
<xsl:choose>
   
<xsl:when test="position() != $page">
    
<href="javascript :paging({position()})">
     
<xsl:value-of select="position()" />
    
</a>
   
</xsl:when>
   
<xsl:otherwise>
    
<b>
     
<xsl:value-of select="position()" />
    
</b>
   
</xsl:otherwise>
  
</xsl:choose>
 
</xsl:template>
</xsl:stylesheet>
posted @ 2007-08-09 10:36  宿远  阅读(332)  评论(0)    收藏  举报