Sum-up of XSLT to XHTML
1:Used XML and XSLT
-XML
2:Set in XML
Add the code below to XML doc
3:Use JavaScript
4:Use C#
Reference:http://msdn.microsoft.com/zh-cn/library/system.xml.xsl.xslcompiledtransform.transform.aspx
-XML
1
<?xml version="1.0" encoding="utf-8" ?>
2
<!--<?xml-stylesheet type="text/xsl" href="XSLTFile1.xslt"?>-->
3
4
<data>
5
<books>
6
<Name>SQL</Name>
7
<price>20.0</price>
8
</books>
9
<books>
10
<Name>CLR via C#</Name>
11
<price>120.0</price>
12
</books>
13
<books>
14
<Name>C#高级编程</Name>
15
<price>80.0</price>
16
</books>
17
<books>
18
<Name>C#级编程</Name>
19
<price>180.0</price>
20
</books>
21
<books>
22
<Name>C#编程</Name>
23
<price>60.0</price>
24
</books>
25
</data>
-XSLT
<?xml version="1.0" encoding="utf-8" ?>2
<!--<?xml-stylesheet type="text/xsl" href="XSLTFile1.xslt"?>-->3

4
<data>5
<books>6
<Name>SQL</Name>7
<price>20.0</price>8
</books>9
<books>10
<Name>CLR via C#</Name>11
<price>120.0</price>12
</books>13
<books>14
<Name>C#高级编程</Name>15
<price>80.0</price>16
</books>17
<books>18
<Name>C#级编程</Name>19
<price>180.0</price>20
</books>21
<books>22
<Name>C#编程</Name>23
<price>60.0</price>24
</books>25
</data> 1
<?xml version="1.0" encoding="utf-8"?>
2
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
4
5
<xsl:output method="xml" indent="yes"/>
6
7
<xsl:template match="/">
8
<!--<xsl:copy>
9
<xsl:apply-templates select="@* | node()"/>
10
</xsl:copy>-->
11
<html>
12
<body>
13
<h2>My books to read:</h2>
14
<table border="1">
15
<tr bgcolor="green">
16
<th>Title</th>
17
<th>Price</th>
18
</tr>
19
<!--here we can use xpath to set conditions-->
20
<xsl:for-each select="data/books">
21
<xsl:sort select="Name" lang="zh-cn"/>
22
<!--also can set conditions here-->
23
<xsl:if test="price > 60">
24
<tr>
25
<td>
26
<xsl:value-of select="Name"/>
27
</td>
28
<td>
29
<xsl:value-of select="price"/>
30
</td>
31
</tr>
32
</xsl:if>
33
</xsl:for-each>
34
</table>
35
36
<h2>My books to read:</h2>
37
<table border="1">
38
<tr bgcolor="green">
39
<th>Title</th>
40
<th>Price</th>
41
</tr>
42
<xsl:for-each select="data/books">
43
<xsl:sort select="Name" lang="zh-cn"/>
44
<xsl:choose>
45
<xsl:when test="price > 60 and price < 100">
46
<tr>
47
<td>
48
<xsl:value-of select="Name"/>
49
</td>
50
<td >
51
<xsl:value-of select="price"/>
52
</td>
53
</tr>
54
</xsl:when>
55
<xsl:when test="price > 100">
56
<tr>
57
<td>
58
<xsl:value-of select="Name"/>
59
</td>
60
<td bgcolor="ff00ff">
61
<xsl:value-of select="price"/>
62
</td>
63
</tr>
64
</xsl:when>
65
<xsl:otherwise>
66
<tr>
67
<td>
68
<xsl:value-of select="Name"/>
69
</td>
70
<td bgcolor="#cccccc">
71
It's cheaper than $60
72
</td>
73
</tr>
74
</xsl:otherwise>
75
</xsl:choose>
76
</xsl:for-each>
77
</table>
78
79
<xsl:apply-templates></xsl:apply-templates>
80
81
</body>
82
</html>
83
</xsl:template>
84
85
<xsl:template match="data/books">
86
<p>
87
<xsl:apply-templates select="Name"/>
88
<xsl:apply-templates select="price"/>
89
</p>
90
</xsl:template>
91
92
<xsl:template match="Name">
93
Name: <span style="color:#ff0000">
94
<xsl:value-of select="."/>
95
</span>
96
<br />
97
</xsl:template>
98
99
<xsl:template match="price">
100
Price: <span style="color:#00ff00">
101
<xsl:value-of select="."/>
102
</span>
103
<br />
104
</xsl:template>
105
106
</xsl:stylesheet>
107
<?xml version="1.0" encoding="utf-8"?>2
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"3
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">4

5
<xsl:output method="xml" indent="yes"/>6

7
<xsl:template match="/">8
<!--<xsl:copy>9
<xsl:apply-templates select="@* | node()"/>10
</xsl:copy>-->11
<html>12
<body>13
<h2>My books to read:</h2>14
<table border="1">15
<tr bgcolor="green">16
<th>Title</th>17
<th>Price</th>18
</tr>19
<!--here we can use xpath to set conditions-->20
<xsl:for-each select="data/books">21
<xsl:sort select="Name" lang="zh-cn"/>22
<!--also can set conditions here-->23
<xsl:if test="price > 60">24
<tr>25
<td>26
<xsl:value-of select="Name"/>27
</td>28
<td>29
<xsl:value-of select="price"/>30
</td>31
</tr>32
</xsl:if>33
</xsl:for-each>34
</table>35

36
<h2>My books to read:</h2>37
<table border="1">38
<tr bgcolor="green">39
<th>Title</th>40
<th>Price</th>41
</tr>42
<xsl:for-each select="data/books">43
<xsl:sort select="Name" lang="zh-cn"/>44
<xsl:choose>45
<xsl:when test="price > 60 and price < 100">46
<tr>47
<td>48
<xsl:value-of select="Name"/>49
</td>50
<td >51
<xsl:value-of select="price"/>52
</td>53
</tr>54
</xsl:when>55
<xsl:when test="price > 100">56
<tr>57
<td>58
<xsl:value-of select="Name"/>59
</td>60
<td bgcolor="ff00ff">61
<xsl:value-of select="price"/>62
</td>63
</tr>64
</xsl:when>65
<xsl:otherwise>66
<tr>67
<td>68
<xsl:value-of select="Name"/>69
</td>70
<td bgcolor="#cccccc">71
It's cheaper than $6072
</td>73
</tr>74
</xsl:otherwise>75
</xsl:choose>76
</xsl:for-each>77
</table>78

79
<xsl:apply-templates></xsl:apply-templates>80

81
</body>82
</html>83
</xsl:template>84

85
<xsl:template match="data/books">86
<p>87
<xsl:apply-templates select="Name"/>88
<xsl:apply-templates select="price"/>89
</p>90
</xsl:template>91

92
<xsl:template match="Name">93
Name: <span style="color:#ff0000">94
<xsl:value-of select="."/>95
</span>96
<br />97
</xsl:template>98

99
<xsl:template match="price">100
Price: <span style="color:#00ff00">101
<xsl:value-of select="."/>102
</span>103
<br />104
</xsl:template>105

106
</xsl:stylesheet>107

2:Set in XML
Add the code below to XML doc
1
<?xml-stylesheet type="text/xsl" href="XSLTFile1.xslt"?>
<?xml-stylesheet type="text/xsl" href="XSLTFile1.xslt"?>3:Use JavaScript
1
window.onload = function () {
2
//Load XML
3
var xml = new ActiveXObject("Microsoft.XMLDOM");
4
xml.async = false;
5
xml.load("XMLFile1.xml");
6
7
//Load XSL
8
var xsl = new ActiveXObject("Microsoft.XMLDOM");
9
xsl.async = false;
10
xsl.load("XSLTFile1.xslt");
11
12
//Transform
13
div1.innerHTML = xml.transformNode(xsl);
14
}
window.onload = function () {2
//Load XML3
var xml = new ActiveXObject("Microsoft.XMLDOM");4
xml.async = false;5
xml.load("XMLFile1.xml");6

7
//Load XSL8
var xsl = new ActiveXObject("Microsoft.XMLDOM");9
xsl.async = false;10
xsl.load("XSLTFile1.xslt");11

12
//Transform13
div1.innerHTML = xml.transformNode(xsl);14
}4:Use C#
1
protected void Page_Load(object sender, EventArgs e)
2
{
3
XmlDocument xmlDoc = new XmlDocument();
4
xmlDoc.Load(Server.MapPath("~/xml/XMLFile1.xml"));
5
6
XslCompiledTransform xslTrans = new XslCompiledTransform();
7
xslTrans.Load(Server.MapPath("~/xml/XSLTFile1.xslt"));
8
9
StringWriter writer = new StringWriter();
10
11
xslTrans.Transform(xmlDoc, null, writer);
12
div1.InnerHtml = writer.ToString();
13
writer.Close();
14
}
protected void Page_Load(object sender, EventArgs e)2
{3
XmlDocument xmlDoc = new XmlDocument();4
xmlDoc.Load(Server.MapPath("~/xml/XMLFile1.xml"));5

6
XslCompiledTransform xslTrans = new XslCompiledTransform();7
xslTrans.Load(Server.MapPath("~/xml/XSLTFile1.xslt"));8

9
StringWriter writer = new StringWriter();10

11
xslTrans.Transform(xmlDoc, null, writer);12
div1.InnerHtml = writer.ToString();13
writer.Close();14
}Reference:http://msdn.microsoft.com/zh-cn/library/system.xml.xsl.xslcompiledtransform.transform.aspx
字体是难看了点...不高兴调了~


浙公网安备 33010602011771号