ITeye博客

xsl递归解晰xml

BBjava · 2011-11-16 15:21 · xml&xsl · Web前端 · Markdown

xml:

<?xml version="1.0" encoding="UTF-8"?>
<formDayreports>
	<formDayreport id="1" lastCash="1000.0" in="100.0" out="100.0" finalCash="1000.0" subject="1" subjectName="a1"/>
	<formDayreport id="2" lastCash="1000.0" in="100.0" out="100.0" finalCash="1000.0" subject="1" subjectName="a1"/>
	<formDayreport id="3" lastCash="1000.0" in="100.0" out="100.0" finalCash="1000.0" subject="1" subjectName="a1"/>
	<formDayreport id="4" lastCash="1000.0" in="100.0" out="100.0" finalCash="1000.0" subject="1" subjectName="a1"/>
	<formDayreport id="5" lastCash="1000.0" in="100.0" out="100.0" finalCash="1000.0" subject="1" subjectName="a1"/>
	<formDayreport id="6" lastCash="1000.0" in="100.0" out="100.0" finalCash="1000.0" subject="1" subjectName="a1"/>
	<formDayreport id="7" lastCash="1000.0" in="100.0" out="100.0" finalCash="1000.0" subject="1" subjectName="a1">
		<formDayreport id="9" lastCash="1000.0" in="100.0" out="100.0" finalCash="1000.0" subject="1" subjectName="a1">
			<formDayreport id="9" lastCash="1000.0" in="100.0" out="100.0" finalCash="1000.0" subject="1" subjectName="a1">
			<formDayreport id="9" lastCash="1000.0" in="100.0" out="100.0" finalCash="1000.0" subject="1" subjectName="a1">
			<formDayreport id="9" lastCash="1000.0" in="100.0" out="100.0" finalCash="1000.0" subject="1" subjectName="a1">
			
		</formDayreport>
		</formDayreport>
		</formDayreport>
		</formDayreport>
	</formDayreport>
	<formDayreport id="8" lastCash="1000.0" in="100.0" out="100.0" finalCash="1000.0" subject="1" subjectName="a1">
		<formDayreport id="10" lastCash="1000.0" in="100.0" out="100.0" finalCash="1000.0" subject="1" subjectName="a1"/>
	</formDayreport>
</formDayreports>

可以看到上面是一个递归的xml文档。

xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:template match="/">
		<xsl:apply-templates select="formDayreports" />
	</xsl:template>

	<xsl:template match="formDayreports">
		<table border="1" height="100%" width="100%">
			<tr height="30">
				<td width="100%" align="center">
					日报表查询
			</td>
			</tr>
			<tr>
				<td>
					<xsl:call-template name="head" />
				</td>
			</tr>
			<tr  height="*">
				<td>
					
					<xsl:for-each select="formDayreport">
						<tr>
							<td>
								<table width="100%" height="100%" border="1">
									<xsl:apply-templates select="." />
								</table>
							</td>
						</tr>
						
		
					</xsl:for-each>
						
				</td>
			</tr>

		</table>
	</xsl:template>

	<xsl:template match="formDayreport">
		
			<tr>
				<td width="5%">
					<xsl:value-of select="@id"></xsl:value-of>
				</td>

				<td width="5%">
					<xsl:value-of select="@subject"></xsl:value-of>
				</td>
				<td width="5%">
					<xsl:value-of select="@subjectName"></xsl:value-of>
				</td>
				<td width="5%">
					<xsl:value-of select="@lastCash"></xsl:value-of>
				</td>
				<td width="5%">
					<xsl:value-of select="@in"></xsl:value-of>
				</td>
				<td width="5%">
					<xsl:value-of select="@out"></xsl:value-of>
				</td>
				<td width="*">
					<xsl:value-of select="@finalCash"></xsl:value-of>
				</td>
			</tr>
			<tr>
				<td>
					<xsl:apply-templates select="formDayreport" />
								
				</td>
			</tr>
		
	</xsl:template>

	<xsl:template name="head">
		<table width="100%" height="100%" border="1">
			<tr>
				<td width="5%">科目编号</td>
				<td width="5%">科目名称</td>
				<td width="5%">昨日余额</td>
				<td width="5%">借</td>
				<td width="5%">贷</td>
				<td width="*">今日余额</td>
			</tr>
		</table>
	</xsl:template>
</xsl:stylesheet>

html:

<html>
	<head>
		<title>My JSP 'darreport-list.jsp' starting page</title>
<script src="comUtil.js"></script>
<script language = "javascript">
function parseXml() {
	var datasXslDoc = commLoad("subjectTree.xsl");
	//var sUrl = "${ctx}/formDayreportsubject.action?numPerPage=10&pageNum=1";
	//var dataDoc = commHttpSend(sUrl, null, false, null, null);
	var datasDoc = commLoad("abc.xml");
	var showStr = datasDoc.documentElement.transformNode(datasXslDoc);
	document.getElementById("divContent").innerHTML = showStr;
}
</script>
	</head>

	<body>
		<div id="divContent"></div>
		<input type="button" name="显示" onclick="parseXml(1)">
	</body>
</html>

还有一个文件:commUtil.js,我将单独把它放到本条目第一编内。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。