新手请教xsl问题,急盼回复!

peihexian 2006-04-14 08:54:53
xml文件内容:

<?xml version="1.0" encoding="GB2312"?>
<xml_data_contents>
<data type="data_table">
<row id="1">
<org_name>单位1</org_name>
<age1820>958</age1820>
<age1820_percent>4.91</age1820_percent>
<age2130>5447</age2130>
<age2130_percent>27.93</age2130_percent>
<age3140>7028</age3140>
<age3140_percent>36.04</age3140_percent>
<age4150>5317</age4150>
<age4150_percent>27.27</age4150_percent>
<age5155>739</age5155>
<age5155_percent>3.79</age5155_percent>
<age56>12</age56>
<age56_percent>0.06</age56_percent>
<age_sum>19501</age_sum>
<age_sum_percent>92.98</age_sum_percent>
</row>
<row id="2">
<org_name>单位2</org_name>
<age1820>154</age1820>
<age1820_percent>10.46</age1820_percent>
<age2130>435</age2130>
<age2130_percent>29.55</age2130_percent>
<age3140>498</age3140>
<age3140_percent>33.83</age3140_percent>
<age4150>329</age4150>
<age4150_percent>22.35</age4150_percent>
<age5155>56</age5155>
<age5155_percent>3.80</age5155_percent>
<age56>0</age56>
<age56_percent>0.00</age56_percent>
<age_sum>1472</age_sum>
<age_sum_percent>7.02</age_sum_percent>
</row>
</data>
<data type="sum_table">
<row id="-1">
<org_name>合计</org_name>
<age1820>1112</age1820>
<age1820_percent>5.30</age1820_percent>
<age2130>5882</age2130>
<age2130_percent>28.05</age2130_percent>
<age3140>7526</age3140>
<age3140_percent>35.88</age3140_percent>
<age4150>5646</age4150>
<age4150_percent>26.92</age4150_percent>
<age5155>795</age5155>
<age5155_percent>3.79</age5155_percent>
<age56>12</age56>
<age56_percent>0.06</age56_percent>
<age_sum>20973</age_sum>
<age_sum_percent>100.00</age_sum_percent>
</row>
</data>
</xml_data_contents>

...全文
242 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
peihexian 2006-04-14
  • 打赏
  • 举报
回复
以下为xsl文件内容:
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="html" version="1.0" encoding="GB2312" indent="yes"/>
<xsl:template match="/">
<table class='sort-table' id='table-1' border='0' width='100%' cellspacing='1' cellpadding='0'>
<thead>
</thead>
<tbody id='show'>
<xsl:for-each select="xml_data_contents/data/row[@id>0]">
<tr>
<td height='18' bgcolor='#FFFFFF'><p style='margin-left: 10px; margin-right: 20px'/> <xsl:value-of select="org_name"/></td>
<td bgcolor='#FFFFFF' height='18' align='center'> <xsl:value-of select="age1820"/></td>
<td bgcolor='#FFFFFF' height='18' align='center'><font color='#BB0000'> <xsl:value-of select="age1820_percent"/></font></td>
<td bgcolor='#FFFFFF' height='18' align='center'> <xsl:value-of select="age2130"/></td>
<td bgcolor='#FFFFFF' height='18' align='center'><font color="#BB0000"> <xsl:value-of select="age2130_percent"/></font></td>
<td bgcolor='#FFFFFF' height='18' align='center'> <xsl:value-of select="age3140"/></td>
<td bgcolor='#FFFFFF' height='18' align='center'><font color='#BB0000'> <xsl:value-of select="age3140_percent"/></font></td>
<td align="center" bgcolor="#FFFFFF"> <xsl:value-of select="age4150"/></td>
<td width='5%' height='18' align='center' bgcolor='#FFFFFF'><font color="#BB0000"> <xsl:value-of select="age4150_percent"/></font></td>
<td width='6%' align='center' bgcolor='#FFFFFF'> <xsl:value-of select="age5155"/></td>
<td width='5%' align='center' bgcolor='#FFFFFF'><font color="#BB0000"> <xsl:value-of select="age5155_percent"/></font></td>
<td width='5%' align='center' bgcolor='#FFFFFF'> <xsl:value-of select="age56"/></td>
<td width='5%' align='center' bgcolor='#FFFFFF'><font color="#BB0000"> <xsl:value-of select="age56_percent"/></font></td>
<td width='6%' align='center' bgcolor='#FFFFFF'><xsl:value-of select="age_sum"/></td>
<td width='6%' align='center' bgcolor='#FFFFFF'><font color="#BB0000"><xsl:value-of select="age_sum_percent"/></font></td>
</tr>
</xsl:for-each>
</tbody>
</table>
<xsl:call-template name="sum_template">
</xsl:call-template>
</xsl:template>

<xsl:template name="sum_template">
<xsl:for-each select="xml_data_contents/data/row[@id=-1]"> <table class='sort-table' id='table-sum' border='0' width='100%' cellspacing='1' cellpadding='0'>
<thead></thead>
<tbody>
<tr>
<td height='18' bgcolor='#FFFFFF'><p style='margin-left: 10px; margin-right: 20px'/> <xsl:value-of select="org_name"/></td>
<td bgcolor='#FFFFFF' height='18' align='center'> <xsl:value-of select="age1820"/></td>
<td bgcolor='#FFFFFF' height='18' align='center'><font color='#BB0000'> <xsl:value-of select="age1820_percent"/></font></td>
<td bgcolor='#FFFFFF' height='18' align='center'> <xsl:value-of select="age2130"/></td>
<td bgcolor='#FFFFFF' height='18' align='center'><font color="#BB0000"> <xsl:value-of select="age2130_percent"/></font></td>
<td bgcolor='#FFFFFF' height='18' align='center'> <xsl:value-of select="age3140"/></td>
<td bgcolor='#FFFFFF' height='18' align='center'><font color='#BB0000'> <xsl:value-of select="age3140_percent"/></font></td>
<td align="center" bgcolor="#FFFFFF"> <xsl:value-of select="age4150"/></td>
<td width='5%' height='18' align='center' bgcolor='#FFFFFF'><font color="#BB0000"> <xsl:value-of select="age4150_percent"/></font></td>
<td width='6%' align='center' bgcolor='#FFFFFF'> <xsl:value-of select="age5155"/></td>
<td width='5%' align='center' bgcolor='#FFFFFF'><font color="#BB0000"> <xsl:value-of select="age5155_percent"/></font></td>
<td width='5%' align='center' bgcolor='#FFFFFF'> <xsl:value-of select="age56"/></td>
<td width='5%' align='center' bgcolor='#FFFFFF'><font color="#BB0000"> <xsl:value-of select="age56_percent"/></font></td>
<td width='6%' align='center' bgcolor='#FFFFFF'><xsl:value-of select="age_sum"/></td>
<td width='6%' align='center' bgcolor='#FFFFFF'><font color="#BB0000"><xsl:value-of select="age_sum_percent"/></font></td>
</tr>
</tbody>

</table>
</xsl:for-each> </xsl:template>

</xsl:stylesheet>

我的目的是想把<xml_data_contents>当中的<data type="data_table">中的内容格式化到一个html table中,然后把<xml_data_contents>中的<data type="sum_table">中的内容格式化另一个html table中去

上面的xsl在java中的dom4j中用来格式化xml时会报错:
org.dom4j.IllegalAddException: The node "org.dom4j.tree.DefaultEleme
nt@790192 [Element: <table attributes: []/>]" could not be added to the branch "
null" because: Cannot add another element to this Document as it already has a r
oot element of: table

请大家帮忙看看正确的xsl该怎么写,谢谢!
是是非非 2006-04-14
  • 打赏
  • 举报
回复
接分来了~
skyhaha 2006-04-14
  • 打赏
  • 举报
回复
是不是给分啊
peihexian 2006-04-14
  • 打赏
  • 举报
回复
原因找到了,我的xsl写法没有问题,因为产生的结果中有两个<table>标记,dom4j认为这样的结果是不well format的,所以在xsl产生数据的最外头加一个div就没事了。

随便来个人我把帖子结了吧。

8,909

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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