用xslt 把xml转换成html的几个实例(1)

最近在忙着给公司xslt转档,刚开始学,碰到了很多问题。也翻阅过很多资料,但是很少有实例的书籍(如果哪位朋友能介绍几本有实例的书籍,推荐几本吧,不胜感激)

下面是我碰到的一些问题,帖出来,只求大家能更好的学习。希望对大伙有帮助。

5-3.xslt 文件

1 <?xml version="1.0"?>
2  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
4 xmlns:myDiscount="urn:myDiscount">
5
6 <msxsl:script language="C#" implements-prefix="myDiscount">
7 <![CDATA[
8 public string ReturnDate(string CheckIn,int CXLDay)
9 {
10 DateTime t = Convert.ToDateTime(CheckIn);
11 DateTime xx =t.AddDays(-(CXLDay+1)); //CheckIn日减去CXLDay天数
12 return xx.ToShortDateString();
13 }
14
15 int tiDBL =0;
16 public void tiDBLsresum()
17 {
18 tiDBL =0;
19 }
20 public void tiDBLsum(int n)
21 {
22 tiDBL+=n;
23 }
24 public int tiDBLsum_rt()
25 {
26 return tiDBL;
27 }
28
29 int tiDBL2 =0;
30 public void tiDBLsresum2()
31 {
32 tiDBL2 =0;
33 }
34 public void tiDBLsum2(int n)
35 {
36 tiDBL2+=n;
37 }
38 public int tiDBLsum2_rt()
39 {
40 return tiDBL2;
41 }
42
43
44 ]]>
45 </msxsl:script>
46
47 <xsl:output method="html" indent="yes"/>
48 <xsl:template match="/">
49 <table>
50 <tr>
51 <!--第一个和最后一个的值-->
52 <td>
53 <xsl:for-each select="Response/SearchAvailResponse/Hotel/CXLPolicys/CXLPolicy">
54 <xsl:sort select ="CXLDay" order ="descending"/>
55
56 <!--第二个值-->
57 <!--<xsl:value-of select ="myDiscount:tiDBLsum(1)"/>
58 <xsl:if test ="myDiscount:tiDBLsum_rt()=2">
59 <xsl:value-of select="myDiscount:ReturnDate(http://www.cnblogs.com/CheckIn,CXLDay)" />
60 </xsl:if>-->
61
62 <!--第一个值-->
63 <!--<xsl:if test ="myDiscount:tiDBLsum_rt()=0">
64 <xsl:value-of select="myDiscount:ReturnDate(http://www.cnblogs.com/CheckIn,CXLDay)" />
65 <xsl:value-of select ="myDiscount:tiDBLsum(1)"/>
66 </xsl:if>-->
67
68 <xsl:value-of select ="myDiscount:tiDBLsum(1)"/>
69 </xsl:for-each>
70 <xsl:for-each select="Response/SearchAvailResponse/Hotel/CXLPolicys/CXLPolicy">
71 <xsl:sort select ="CXLDay" order ="descending"/>
72
73 <xsl:if test ="myDiscount:tiDBLsum2_rt()=0">
74
75 <xsl:value-of select="CXLDay" />---
76 <xsl:value-of select="myDiscount:ReturnDate(http://www.cnblogs.com/CheckIn,CXLDay)" />
77 </xsl:if>
78 <br></br>
79 <xsl:value-of select ="myDiscount:tiDBLsum2(1)"/>
80 <xsl:if test ="myDiscount:tiDBLsum2_rt()=myDiscount:tiDBLsum_rt()">
81 <xsl:value-of select="CXLDay" />---
82 <xsl:value-of select="myDiscount:ReturnDate(http://www.cnblogs.com/CheckIn,CXLDay)" />
83 </xsl:if>
84 </xsl:for-each>
87 </td>
88 </tr>
      </table>
91 </xsl:template>
92 </xsl:stylesheet>

xml 文件

1 <CXLPolicys>
2 <CXLPolicy>
3 <CXLDay>3</CXLDay>
4 <CXLRemark>3 Night</CXLRemark>
5 </CXLPolicy>
6 <CXLPolicy>
7 <CXLDay>4</CXLDay>
8 <CXLRemark>2 Night</CXLRemark>
9 </CXLPolicy>
10 <CXLPolicy>
11 <CXLDay>5</CXLDay>
12 <CXLRemark>1 Night</CXLRemark>
13 </CXLPolicy>
14 <CXLPolicy>
15 <CXLDay>1</CXLDay>
16 <CXLRemark>Full Charge</CXLRemark>
17 </CXLPolicy>
18 </CXLPolicys>

aspx 文件

(运行这个aspx文件就可以看到上面转档后的结果)

1 using System;
2 using System.Collections;
3 using System.Configuration;
4 using System.Data;
5 using System.Web;
6 using System.Web.Security;
7 using System.Web.UI;
8 using System.Web.UI.HtmlControls;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.WebControls.WebParts;
11 using System.IO;
12 using System.Xml;
13 using System.Xml.Xsl;
14 using System.Xml.XPath;
15
16 public partial class _5_3 : System.Web.UI.Page
17 {
18 protected void Page_Load(object sender, EventArgs e)
19 {
20
21 string xmlPath = HttpContext.Current.Server.MapPath("./Search a Hotel Response.xml");
22 string xslPath = HttpContext.Current.Server.MapPath("./5-3.xsl");
23
24 XPathDocument xpathDoc = new XPathDocument(xmlPath);
25 XslCompiledTransform transform = new XslCompiledTransform();
26
27 //Load the XSL stylsheet into the XslCompiledTransform object
28 XsltSettings axslset = new XsltSettings();
29 axslset.EnableScript = true;
30 transform.Load(xslPath, axslset, new XmlUrlResolver());
31
32 XsltArgumentList argsList = new XsltArgumentList();
33
34 System.Text.StringBuilder t = new System.Text.StringBuilder();
35
36 transform.Transform(xpathDoc, argsList, new System.IO.StringWriter(t));
37 string resultString = t.ToString();
38
39 Response.Write(resultString);
40
41 }
42 }
posted @ 2011-05-05 09:11  小2010  阅读(563)  评论(0编辑  收藏  举报