XSL怎样转换数据库中的以xml格式保存的记录,并以表格形式展现到页面给用户

wudidongs 2008-04-14 11:22:06
在数据库A中有表B,表B 的字段为StudentID,StaffID,Evaluate2.Evaluate2字段里存有XML格式的记录,但格式不太一样,如:
第一条记录:
<F2 Name="记实测评" Score="-68">
<F21 Name="必修项">
<F211 Name="上课出勤情况">
</F211>
<F212 Name="班集体活动参与情况">
</F212>
<F213 Name="集体公益劳动">
</F213>
<F214 Name="社会实践">
</F214>
<F215 Name="院校安排的活动参与情况">
</F215>
</F21>
<F22 Name="选修与附加项" Score="32">
<F221 Name="政治思想、道德品质" Score="24">
</F221>
<F222 Name="学习、科技竞赛及论文发表" Score="8">
<Item Name="省级大学生学习、科技竞赛三等奖以上奖励" ContentID="20" Score="4" />
<Item Name="省级大学生学习、科技竞赛三等奖以上奖励" ContentID="20" Score="4" />
</F222>
<F223 Name="社会活动">
</F223>
<F224 Name="文娱体育活动">
</F224>
<F225 Name="社会工作">
</F225>
</F22>
<F23 Name="负分值" Score="-100">
</F23>
<F24 Name="细则中未有提到的加分或减分">
</F24>
</F2>
第二条记录:
<F2 Name="记实测评" Score="-75">
<F21 Name="必修项" Score="25">
<F211 Name="上课出勤情况" Score="5">
</F211>
<F212 Name="班集体活动参与情况" Score="5">
</F212>
<F213 Name="集体公益劳动" Score="5">
</F213>
<F214 Name="社会实践" Score="5">
</F214>
<F215 Name="院校安排的活动参与情况" Score="5">
</F215>
</F21>
<F22 Name="选修与附加项">
<F221 Name="政治思想、道德品质">
</F221>
<F222 Name="学习、科技竞赛及论文发表">
</F222>
<F223 Name="社会活动">
</F223>
<F224 Name="文娱体育活动">
</F224>
<F225 Name="社会工作">
</F225>
</F22>
<F23 Name="负分值" Score="-100">
<Item Name="受警告处分一次" ContentID="16" Score="40" />
<Item Name="留校查看一次" ContentID="18" Score="60" />
</F23>
<F24 Name="细则中未有提到的加分或减分">
</F24>
要做成什么样的XSLT模板才能使XML记录以表格形式呈现到页面呢?要求是XSLT模板能动态转换数据库中任何一条记录,而且当某学生有的加分或减分项才显示,没有就不用显示.请各位高手详细指点,必谢!
...全文
116 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyaspnet 2008-04-15
  • 打赏
  • 举报
回复
输出XML文件时, 只需要改动<?xml-stylesheet type="text/xsl" href="你想要的XSL文件" ?>即可
wudidongs 2008-04-15
  • 打赏
  • 举报
回复
不过还有个疑问,就是把每次从数据库中调出的不同的XML与固定的XSL连接呢?我只会静态的,劳烦指教
wudidongs 2008-04-15
  • 打赏
  • 举报
回复
解决了,谢谢啊
大飞飞虫 2008-04-14
  • 打赏
  • 举报
回复
必须针对你的XML写一个xsl文件

<xsl:output method="html" encoding="gb2312" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
就会输出为HTML了.用户通过IE打开就OK

如果要有表格的话,用html的<table>,td,tR标记就可以解决呀

如果要别人写很难的,自己研究一下xsl的语法吧
skyaspnet 2008-04-14
  • 打赏
  • 举报
回复
改好了, 我这测试通过, 样式部分我做了一个层次关系, 看起来会比较有层次感一些:


<?xml version="1.0" encoding="utf-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<body style="font-family:Arial,helvetica,sans-serif;font-size:12pt;
background-color:#EEEEEE">
<xsl:for-each select="root/element">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
分数为:<xsl:value-of select="attribute::Score"/></span>
- 详细情况如下:
</div>
</xsl:for-each>
<xsl:for-each select="root/element/element2">
<xsl:if test="@Score != '0'"><div style="background-color:teal;color:white;padding:4px;text-indent:30px;">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::DesCription"/></span>
----分数为:<xsl:value-of select="attribute::Score"/></span>:
</div></xsl:if>
<xsl:for-each select="element3">
<xsl:if test="@Score != '0'"><div style="background-color:teal;color:white;padding:4px;text-indent:50px;">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white "><xsl:value-of select="attribute::Description"/></span>
----分数为:<xsl:value-of select="attribute::Score"/></span>:
</div></xsl:if>
</xsl:for-each>
</xsl:for-each>

</body>
</html>

wudidongs 2008-04-14
  • 打赏
  • 举报
回复
是啊,还是要根据存在数据库的XML记录生成动态的XSLT,呵呵,麻烦您再改改
skyaspnet 2008-04-14
  • 打赏
  • 举报
回复
也可以改, 你确定是这个XML文件的格式吗, 最好确定一下, 不然我改起来就太麻烦了, 最好一次改好
wudidongs 2008-04-14
  • 打赏
  • 举报
回复
我的XML文件可能写的不规范,这样转换起来难度比较大,我改成了:
<?xml version="1.0" encoding="utf-8"?>
<root>
<element Name="F2" Description="记实测评" Score="8">
<element2 Name="F21" DesCription="必修项" Score="0">
<element3 Name="F211" Description="上课出勤情况" Score="0">
</element3>
<element3 Name="F212" Description="班集体活动参与情况" Score="0">
</element3>
<element3 Name="F213" Description="集体公益劳动" Score="0">
</element3>
<element3 Name="F214" Description="社会实践" Score="0">
</element3>
<element3 Name="F215" Description="院校安排的活动参与情况" Score="0">
</element3>
</element2>
<element2 Name="F22" DesCription="选修与附加项" Score="8">
<element3 Name="F221" Description="全国三好学生" ContentID="1" Score="6" />
<element3 Name="F221" Description="校级优秀团员" ContentID="3" Score="2" />
</element2>
<element2 Name="F23" DesCription="负分值" Score="0">
</element2>
<element2 Name="F24" DesCription="细则中未有提到的加分或减分" Score="0">
</element2>
</element>
</root>
skyaspnet 2008-04-14
  • 打赏
  • 举报
回复
我看了一下, 这两个XML文件的结构是一样的:

使用同一个XSL文件作为模板即可, 也就是说使用上面发的XSL代码即可,

我这已经调试成功, 你试一下, 祝你好运
skyaspnet 2008-04-14
  • 打赏
  • 举报
回复
如果没有问题的话, 我就改第二个了
skyaspnet 2008-04-14
  • 打赏
  • 举报
回复
这是第一个的XSL文件,我这已经调试成功了, 你试一下:

<?xml version="1.0" encoding="utf-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<body style="font-family:Arial,helvetica,sans-serif;font-size:12pt;
background-color:#EEEEEE">
<xsl:for-each select="F2">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
分数为:<xsl:value-of select="attribute::Score"/></span>
- 详细情况如下:
</div>
</xsl:for-each>
<xsl:for-each select="F2/F21/F211">
<xsl:if test="@Score != ''"><div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::Name"/></span>
----分数为:<xsl:value-of select="attribute::Score"/></span>:
</div></xsl:if>
</xsl:for-each>
<xsl:for-each select="F2/F21/F212">
<xsl:if test="@Score != ''"><div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::Name"/></span>
----分数为:<xsl:value-of select="attribute::Score"/></span>:
</div>
</xsl:if>
</xsl:for-each>
<xsl:for-each select="F2/F21/F213">
<xsl:if test="@Score != ''"><div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::Name"/></span>
----分数为:<xsl:value-of select="attribute::Score"/></span>:
</div></xsl:if>
</xsl:for-each>
<xsl:for-each select="F2/F21/F214">
<xsl:if test="@Score != ''"><div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::Name"/></span>
----分数为:<xsl:value-of select="attribute::Score"/></span>:
</div></xsl:if>
</xsl:for-each>
<xsl:for-each select="F2/F21/F215">
<xsl:if test="@Score != ''"><div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::Name"/></span>
----分数为:<xsl:value-of select="attribute::Score"/></span>:
</div></xsl:if>
</xsl:for-each>



<xsl:for-each select="F2/F22/F221">
<xsl:if test="@Score != ''"><div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::Name"/></span>
----分数为:<xsl:value-of select="attribute::Score"/></span>:
</div></xsl:if>
</xsl:for-each>

<xsl:for-each select="F2/F22/F222">
<xsl:if test="@Score != ''"><div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::Name"/></span>
----分数为:<xsl:value-of select="attribute::Score"/>具体分数为:</span>:
</div></xsl:if>
</xsl:for-each>
<xsl:for-each select="F2/F22/F222/Item">
<xsl:if test="@Score != ''"><div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::Name"/></span>
----------分数为:<xsl:value-of select="attribute::Score"/></span>:
</div></xsl:if>
</xsl:for-each>
<xsl:for-each select="F2/F22/F223">
<xsl:if test="@Score != ''"><div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::Name"/></span>
----分数为:<xsl:value-of select="attribute::Score"/></span>:
</div></xsl:if>
</xsl:for-each>

<xsl:for-each select="F2/F22/F224">
<xsl:if test="@Score != ''"><div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::Name"/></span>
----分数为:<xsl:value-of select="attribute::Score"/></span>:
</div></xsl:if>
</xsl:for-each>

<xsl:for-each select="F2/F22/F225">
<xsl:if test="@Score != ''"><div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::Name"/></span>
----分数为:<xsl:value-of select="attribute::Score"/></span>:
</div></xsl:if>
</xsl:for-each>

<xsl:for-each select="F2/F23">
<xsl:if test="@Score != ''"><div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::Name"/></span>
----分数为:<xsl:value-of select="attribute::Score"/></span>:
</div></xsl:if>
</xsl:for-each>


<xsl:for-each select="F2/F24">
<xsl:if test="@Score != ''"> <div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::Name"/></span>
----分数为:<xsl:value-of select="attribute::Score"/>具体分数为:</span>:
</div></xsl:if>
</xsl:for-each>
<xsl:for-each select="F2/F24/Item">
<xsl:if test="@Score != ''"><div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold;color:white">
<span style="font-weight:bold;color:white"><xsl:value-of select="attribute::Name"/></span>
----------分数为:<xsl:value-of select="attribute::Score"/></span>:
</div></xsl:if>
</xsl:for-each>

</body>
</html>

skyaspnet 2008-04-14
  • 打赏
  • 举报
回复
这里给个建议, 第一条记录我已经按你的要求改好了, 但是需要你在生成XML文件里做一个小修改

就是当没有分数时, 例如: <F211 Name="上课出勤情况">

改成 <F211 Name="上课出勤情况" Score="">

你看行不, 可以的话我把代码贴上来

62,241

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧