这样格式的xml怎么转成xsl

lion_lh 2003-12-10 09:30:28
xml文件内容
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="ss.xsl"?>
<CATALOG>
<CD>
<com_name>区公司</com_name>
<report_type>固定报表</report_type>
<counts>5</counts>
</CD>
<CD>
<com_name>区公司</com_name>
<report_type>经营分析</report_type>
<counts>1</counts>
</CD>
<CD>
<com_name>分公司1</com_name>
<report_type>经营分析</report_type>
<counts>2</counts>
</CD>
</CATALOG>

需要转化成如下的html输出

公司名 固定报表当天访问次数 经营分析报表访问次数 合计访问次数

区公司 5 1 6
分公司1 0 2 2
...

合计 5 3 8

...全文
75 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
cer 2003-12-13
  • 打赏
  • 举报
回复
别外这个怎样处理:
求救:在xml中的标题中有"/"字符,在查询时怎样处理?如
<title>
深入閱讀 "幸福之鐘/The Blessing Bell/幸福の鐘"
</title>
我查询的语句怎样写:
doc.SelectSingleNode("//item[title='']")
是用\转意吗?但在C#中他不让用啊
qiri07 2003-12-12
  • 打赏
  • 举报
回复
<xsl:for-each select="">
<xsl:value-of select="">

有本xsl的书,就很容易
snowcloud2002 2003-12-12
  • 打赏
  • 举报
回复
用 liuzxit(八爪)
heaven119 2003-12-12
  • 打赏
  • 举报
回复
现在问题不是在这里.主要是你的公司名是不是固定的,如果是固定的几个那就好办了,
如果不是,那么得对公司名有重复的元素内容进行过滤,难点在这里,
楼主最好把你的schema拿出来看看,我已经写了个schema.但公司名是固定的几个.
如果象我上面说的公司名不是固定的,恐怕只用xslt无法做,只有结合javascript才行.
楼主请你把它的详细情况说一下,我会为这个问题专门写一篇文章的.
我的qq 15770732
email: haiwei_wang@tom.com
cer 2003-12-12
  • 打赏
  • 举报
回复
同意上面的
liuzxit 2003-12-12
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="gb2312" indent="yes"/>

<xsl:template match="CATALOG">
<table border="1"><tr><td>公司名</td><td>固定报表当天访问次数</td><td>经营分析报表访问次数</td><td>合计访问次数</td></tr>
<xsl:for-each select="CD">
<xsl:variable name="row"><xsl:value-of select="position()" /></xsl:variable>
<xsl:if test="position() mod 2=1">
<tr><td><xsl:value-of select="com_name" /></td>
<td><xsl:value-of select="counts" /></td>
<td><xsl:value-of select="../CD[position()=$row + 1]/counts" /></td>
<td><xsl:value-of select="counts + ../CD[position()=$row + 1]/counts" /></td>
</tr>
</xsl:if>
</xsl:for-each>
<tr><td>合计:</td>
<td><xsl:value-of select="sum(./CD[position() mod 2 =1]/counts)" /></td>
<td><xsl:value-of select="sum(./CD[position() mod 2 =0]/counts)" /></td>
<td><xsl:value-of select="sum(./CD/counts)" /></td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>


这一段在满足以下前提条件可实现你的结果:
1.按公司排序(当然这个可以通过xsl实现)
2.每个公司都必须要有[固定报表]和[经营分析]两个节点,如果counts节点是空那也要用0补上,不然数据会对不上或显示出NaN
guoyan19811021 2003-12-12
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>
<BODY STYLE="font-family:宋体; font-size:9pt;background-color:#ffffff">
<xsl:for-each select="//CD">
<DIV STYLE="background-color:ffffcc; color:white; padding:4px">
<table >
<tr STYLE=" FONT-SIZE: 10pt">
<td>
<xsl:value-of select="com_name" />
</td>
<td>
<xsl:value-of select="report_type" />
</td>
<td>
<xsl:value-of select="counts" />
</td>
</tr>
</table>
</DIV>
</xsl:for-each>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
cer 2003-12-12
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="CATALOG">
<html>
<head>
<title/>
</head>
<body>
<table>
<tr>
<td width="80">
公司名
</td>

<td width="200">
固定报表当天访问次数
</td>

<td width="200">
经营分析报表访问次数
</td>
<td>
合计访问次数
</td>
</tr>
<xsl:apply-templates select="CD[report_type='固定报表']"/>
<tr>
<td width="80">
合计
</td>
<td width="200">
<xsl:value-of select="sum(./CD[report_type='固定报表']/counts)"/>
</td>
<td width="200">
<xsl:value-of select="sum(./CD[report_type='经营分析']/counts)"/>
</td>
<td>
<xsl:value-of select="sum(./CD/counts)"/>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="CD[report_type='固定报表']">
<tr>
<td>
<xsl:value-of select="./com_name"/>
</td>
<td>
<xsl:value-of select="./counts"/>
</td>
<td>
<xsl:value-of select="../CD[com_name=./com_name and report_type='经营分析']/counts"/>
</td>
<td>
<xsl:value-of select="./counts+../CD[com_name=./com_name and report_type='经营分析']/counts"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
我这样试的,但不能得到当前用的com_name,我试过用"xsl:variable"将他存贮起来,但xsl中没有局部变量.

=================
同意 liuzxit(八爪魚) 的说话
liuzxit 2003-12-12
  • 打赏
  • 举报
回复
楼上的说很容易,我保证他写不出来

我不知怎样才能把com_name不重复的读出来,所以没办法写出来,如果xml里有更多的信息能解决这个问题或许可以解决
snowcloud2002 2003-12-10
  • 打赏
  • 举报
回复
up

8,906

社区成员

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

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