利用xsl文件对xml文件倒序排序并显示

来源:互联网 发布:知乎有趣的问题 编辑:程序博客网 时间:2024/05/17 22:33

xml文件是要靠xsl样式文件来显示的,以达到读者正常浏览的目的。有点类似于样式表的意思。

1xml文件Comment.xml

<?xml version="1.0" encoding="utf-8"?>
<AllComment>
   
<Comment NewsId="152">
     
<CommentContent>3333333333</CommentContent>
     
<AddTime>2007-5-28 22:41:08</AddTime>
   
</Comment>
   
<Comment NewsId="153">
     
<CommentContent>4444</CommentContent>
     
<AddTime>2007-5-28 22:41:48</AddTime>
   
</Comment>
   
<Comment NewsId="154">
     
<CommentContent>hhhhhhhhhh</CommentContent>
     
<AddTime>2007-5-28 22:41:52</AddTime>
   
</Comment>
   
<Comment NewsId="151">
     
<CommentContent>fff</CommentContent>
     
<AddTime>2007-5-28 22:42:03</AddTime>
   
</Comment>
   
<Comment NewsId="154">
     
<CommentContent>bbbbb</CommentContent>
     
<AddTime>2007-5-28 23:04:23</AddTime>
   
</Comment>
</AllComment>

 2、xsl文件Comment.xsl

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl
="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="strNewsId"/>
<xsl:template match="/">
    
<html>
      
<body>
        
<FONT face="Verdana" size="3">
          
<TABLE cellspacing="5" cellpadding="1">
            
<TR bgcolor="#AAAAAA">
              
<TD class="header">
                
<B>评论内容</B>
              
</TD>
              
<TD class="header">
                
<B>发表时间</B>
              
</TD>
            
</TR>
            
<xsl:for-each select="//AllComment/Comment[@NewsId=$strNewsId]">
             
<xsl:sort select="AddTime" order="descending" />//倒序排序
              
<TR bgcolor="#DDDDDD">
                
<TD>
                  
<xsl:value-of select="CommentContent"/>
                
</TD>
                
<TD>
                  
<xsl:value-of select="AddTime"/>
                
</TD>
              
</TR>
            
</xsl:for-each>
          
</TABLE>
        
</FONT>
        
<!--
        This is an XSLT template file. Fill in this area with the
        XSL elements which will transform your XML to XHTML.
    
-->
      
</body>
    
</html>
</xsl:template>

</xsl:stylesheet> 

 

3、aspx文件DisplayComment.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DisplayComment.aspx.cs" Inherits="NewsFiles_DisplayComment" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>显示评论</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:Xml ID="Xml1" runat="server" DocumentSource="../XmlFiles/Comment.xml" TransformSource="../XmlFiles/Comment.xsl"></asp:Xml>
    
</div>
    
</form>
</body>
</html>

 

4、.aspx.cs文件

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

public partial class NewsFiles_DisplayComment : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
string NewsId = Request.QueryString["NewsId"].ToString().Trim();

        XPathDocument xmlDoc 
= new XPathDocument(Server.MapPath("../XmlFiles/Comment.xml"));

        XslTransform xmlTrans 
= new XslTransform();
        xmlTrans.Load(Server.MapPath(
"../XmlFiles/Comment.xsl"));
        XsltArgumentList xsltArgList 
= new XsltArgumentList();
        xsltArgList.AddParam(
"strNewsId""", NewsId);
        Response.ContentType 
= "text/html";
        xmlTrans.Transform(xmlDoc, xsltArgList, Response.OutputStream);
    }

}

原创粉丝点击