xml文件
<?xml version="1.0" encoding="utf-8" ?>
<paper title="小寒考试系统" name="大三历史期末考试" start
time
="2008-1-28 09:00" time="120" scorevalue="100" score="0">
<student name="小寒" id="041124096"/>
<questions title="单选题" scorevalue="20"
count
="1" score="0">
<danxuan id="1" subject="历史" chapter="第三章" d
if
ficulty="1" scorevalue="20" score="0">
<content>诸葛亮姓什么?</content>
<choices>
<choice
key
="1">诸</choice>
<choice key="2">诸葛</choice>
<choice key="3">诸葛亮</choice>
<choice key="4">亮</choice>
</choices>
<answer>2</answer>
<studentanswer></studentanswer>
</danxuan>
</questions>
<questions title="多选题" scorevalue="20" count="1" score="0">
<duoxuan id="2" subject="历史" chapter="第三章" difficulty="1" scorevalue="20" score="0">
<content>三国是指那三国?</content>
<choices>
<choice key="1">魏国</choice>
<choice key="2">吴国</choice>
<choice key="3">辽国</choice>
<choice key="4">蜀国</choice>
</choices>
<answer>1,2,4</answer>
<studentanswer></studentanswer>
</duoxuan>
</questions>
<questions title="判断题" scorevalue="20" count="1" score="0">
<panduan id="3" subject="历史" chapter="第三章" difficulty="1" scorevalue="20" score="0">
<content>刘备建立了蜀国?</content>
<choices>
<choice key="0">0</choice>
<choice key="1">1</choice>
</choices>
<answer>1</answer>
<studentanswer></studentanswer>
</panduan>
</questions>
<questions title="填空题" scorevalue="20" count="1" score="0">
<tiankong id="4" subject="历史" chapter="第三章" difficulty="1" scorevalue="20" score="0">
<content> <![cdata[
三国里的五虎上将是指关羽,$_4.1_$,$_4.2_$,$_4.3_$,赵云。
]]>
</content>
<answers>
<answer key="1">张飞</answer>
<answer key="2">魏延</answer>
<answer key="3">马超</answer>
</answers>
<studentanswers>
<studentanswer key="1"></studentanswer>
<studentanswer key="2"></studentanswer>
<studentanswer key="3"></studentanswer>
</studentanswers>
</tiankong>
</questions>
<questions title="
简答题
" scorevalue="20" count="1" score="0">
<jianda id="5" subject="历史" chapter="第三章" difficulty="1" scorevalue="20" score="0">
<content>为什么诸葛亮没能统一三国?</content>
<answer>因为魏国统一了三国。</answer>
<studentanswer></studentanswer>
</jianda>
</questions>
</paper>
xslt文件
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/xsl/trans
for
m"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:rules="http://www.netguy.cn/xslt"
exclude-result-prefixes="rules msxsl">
<xsl:output method="html"/>
<msxsl:script implements-prefix="rules"
lang
uage="
c#
">
<![cdata[
public
static
string
changetextbox(string content)
{
// define a regular expression for repeated w
ord
s.
regex rx =
new
regex(@"\$_\s\s\s_\$",
regexoptions.compiled | regexoptions.ignorecase);
// find matches.
matchcollection matches = rx.matches(content);
// report on
each
match.
foreach
(match match in matches)
{
string word = match.value;
content=content.replace(word,"<input name=\""+word.
substr
ing(2,word.length-4)+"\" type=\"text\">");
}
return
content;
}
]]>
</msxsl:script>
<xsl:template match="paper">
<html xmlns="http://www.w3.org/1999/xhtml" >
<
head
>
<title>
<xsl:value-of select="@title"/>
</title>
</head>
<body>
<p class="head">
<p class="name"><xsl:value-of select="@name"/></p>
<p class="info">
姓名:<xsl:value-of select="student/@name"/>
学号:<xsl:value-of select="student/@id"/>
开考时间:<xsl:value-of select="@starttime"/>
时间:<xsl:value-of select="@time"/>
总分:<xsl:value-of select="@scorevalue"/>
</p>
</p>
<xsl:apply-templates select="questions"/>
</body>
</html>
</xsl:template>
<xsl:template match="questions">
<p class="title">
<xsl:number format="i."/>
<xsl:value-of select="@title"/>
<xsl:text>。(共</xsl:text>
<xsl:value-of select="@count"/>
<xsl:text>题,</xsl:text>
<xsl:value-of select="@scorevalue"/>
<xsl:text>分)</xsl:text>
</p>
<xsl:apply-templates select="danxuan"/>
<xsl:apply-templates select="duoxuan"/>
<xsl:apply-templates select="panduan"/>
<xsl:apply-templates select="tiankong"/>
<xsl:apply-templates select="jianda"/>
</xsl:template>
<xsl:template match="danxuan">
<p class="danxuan">
<p class="content">
<xsl:number format="1."/>
<xsl:value-of select="content"/>
</p>
<p class="choices">
<ul>
<xsl:for-each select="choices/choice">
<li>
<xsl:number format="a."/>
<input name="{http://www.cnblogs.com/@id}" type="radio" value="{@key}"/>
<xsl:value-of select="."/>
</li>
</xsl:for-each>
</ul>
</p>
</p>
</xsl:template>
<xsl:template match="duoxuan">
<p class="duoxuan">
<p class="content">
<xsl:number format="1."/>
<xsl:value-of select="content"/>
</p>
<p class="choices">
<ul>
<xsl:for-each select="choices/choice">
<li>
<xsl:number format="a."/>
<input name="{http://www.cnblogs.com/@id}" type="checkbox" value="{@key}"/>
<xsl:value-of select="."/>
</li>
</xsl:for-each>
</ul>
</p>
</p>
</xsl:template>
<xsl:template match="panduan">
<p class="panduan" style="
width
:
300
px">
<p class="content" style="
float
:left
;width:70%">
<xsl:number format="1."/>
<xsl:value-of select="content"/>
</p>
<p class="choices" style="float
:right
;width:25%">
<input name="{@id}" type="radio" value="1"/>y
<input name="{@id}" type="radio" value="0" />n
</p>
</p>
</xsl:template>
<xsl:template match="tiankong">
<p class="tiankong">
<xsl:number format="1."/>
<xsl:value-of select="rules:changetextbox(string(content))" disable-output-esc
api
ng="yes"/>
</p>
</xsl:template>
<xsl:template match="jianda">
<p class="jianda">
<p class="content">
<xsl:number format="1."/>
<xsl:value-of select="content"/>
</p>
<p class="input">
<textarea name="{@id}" cols="70" rows="8"></textarea>
</p>
</p>
</xsl:template>
</xsl:stylesheet>
生成html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>小寒考试系统</title>
</head>
<body>
<div class="head">
<div class="name">大三历史期末考试</div>
<div class="info">
姓名:小寒
学号:041124096
开考时间:2008-1-28 09:00
时间:120
总分:100</div>
</div>
<div class="title" xmlns="">i.单选题。(共1题,20分)</div>
<div class="danxuan" xmlns="">
<div class="content">1.诸葛亮姓什么?</div>
<div class="choices">
<ul>
<li>a.<input name="1" type="radio" value="1">诸</li>
<li>b.<input name="1" type="radio" value="2">诸葛</li>
<li>c.<input name="1" type="radio" value="3">诸葛亮</li>
<li>d.<input name="1" type="radio" value="4">亮</li>
</ul>
</div>
</div>
<div class="title" xmlns="">ii.多选题。(共1题,20分)</div>
<div class="duoxuan" xmlns="">
<div class="content">1.三国是指那三国?</div>
<div class="choices">
<ul>
<li>a.<input name="2" type="checkbox" value="1">魏国</li>
<li>b.<input name="2" type="checkbox" value="2">吴国</li>
<li>c.<input name="2" type="checkbox" value="3">辽国</li>
<li>d.<input name="2" type="checkbox" value="4">蜀国</li>
</ul>
</div>
</div>
<div class="title" xmlns="">iii.判断题。(共1题,20分)</div>
<div class="panduan" style="width:300px" xmlns="">
<div class="content" style="float:left;width:70%">1.刘备建立了蜀国?</div>
<div class="choices" style="float:right;width:25%"><input name="3" type="radio" value="1">y
<input name="3" type="radio" value="0">n
</div>
</div>
<div class="title" xmlns="">iv.填空题。(共1题,20分)</div>
<div class="tiankong" xmlns="">1.
三国里的五虎上将是指关羽,<input name="4.1" type="text">,<input name="4.2" type="text">,<input name="4.3" type="text">,赵云。
</div>
<div class="title" xmlns="">v.简答题。(共1题,20分)</div>
<div class="jianda" xmlns="">
<div class="content">1.为什么诸葛亮没能统一三国?</div>
<div class="input"><textarea name="5" cols="70" rows="8"></textarea></div>
</div>
</body>
</html>
以上就是使用xlst将xml转换html的示例代码的详细内容。