首页 > 应用开发 > IT综合 > 利用xsl文件对xml文件倒序排序并显示 (转载)
xml文件是要靠xsl样式文件来显示的,以达到读者正常浏览的目的。有点类似于样式表的意思。
1xml文件Comment.xml
xml version="1.0" encoding="utf-8"?>
<AllComment>
<Comment NewsId="152">
<CommentContent>3333333333CommentContent>
<AddTime>2007-5-28 22:41:08AddTime>
Comment>
<Comment NewsId="153">
<CommentContent>4444CommentContent>
<AddTime>2007-5-28 22:41:48AddTime>
Comment>
<Comment NewsId="154">
<CommentContent>hhhhhhhhhhCommentContent>
<AddTime>2007-5-28 22:41:52AddTime>
Comment>
<Comment NewsId="151">
<CommentContent>fffCommentContent>
<AddTime>2007-5-28 22:42:03AddTime>
Comment>
<Comment NewsId="154">
<CommentContent>bbbbbCommentContent>
<AddTime>2007-5-28 23:04:23AddTime>
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);
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10086843/viewspace-925087/ ,如需转载,请注明出处,否则将追究法律责任。