我有以下XML代码,我尝试使用XSLT进行显示。如何选择fieldname并返回表中的所有值。我试过但它不起作用,只返回第一条记录('用户名'和'用户')。请多多帮助,谢谢。
XML代码
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="xml_list_new.xsl"?>
<root>
<record>
<fieldname>username</fieldname>
<value>user</value>
<fieldname>ipaddress</fieldname>
<value>127.0.0.1</value>
<fieldname>lastaction</fieldname>
<value>2011-06-13 00:53:05</value>
<fieldname>sessionid</fieldname>
<value>h0atrutpu5vgrvuf3ipledfbvl1</value>
</record>
<record>
<fieldname>username</fieldname>
<value>admin</value>
<fieldname>ipaddress</fieldname>
<value>127.0.0.1</value>
<fieldname>lastaction</fieldname>
<value>2011-06-12 00:43:53</value>
<fieldname>sessionid</fieldname>
<value>nnbdofsdq4sd7e3def3hnid9</value>
</record>
<record>
<fieldname>username</fieldname>
<value>ccm</value>
<fieldname>ipaddress</fieldname>
<value>127.0.0.1</value>
<fieldname>lastaction</fieldname>
<value>2011-06-10 00:30:22</value>
<fieldname>sessionid</fieldname>
<value>di34bndffgrt48cmkl08d8e4</value>
</record>
</root>
XSLT代码
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method='html'/>
<xsl:param name="title">XML List</xsl:param>
<xsl:template match="/">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title><xsl:value-of select="$title"/></title>
<style type="text/css">
<![CDATA[
caption { font-weight: bold; }
th {
background: #2C3033;
color: #fff;
font-family: arial;
font-weight: bold;
font-size: 12px;
}
tr {
font-family: arial;
color: #000;
font-size: 12px;
background: #E0E0E0;
}
]]>
</style>
</head>
<body>
<div class="center" style="width: 800px; margin: 0 auto">
<table border="0" style="width: 800px">
<caption><xsl:value-of select="$title"/></caption>
<thead>
<tr>
<xsl:for-each select="root/record">
<th >
<xsl:value-of select="fieldname"/>
</th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:for-each select="root/record">
<tr>
<td ><xsl:value-of select="value"/></td>
</tr>
</xsl:for-each>
</tbody>
</table>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:0)
看看这个模板。我试图尽可能地遵循你的模板。我只添加了一个模板(并清理了输出以便于阅读)。
[XSTL 1.0]
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<table>
<thead>
<tr>
<xsl:for-each select="root/record[1]/fieldname">
<th>
<xsl:value-of select="."/>
</th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:for-each select="root/record">
<tr>
<xsl:apply-templates select="value"/>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:template>
<xsl:template match="value">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
</xsl:stylesheet>
生成下表:
<table>
<thead>
<tr>
<th>username</th>
<th>ipaddress</th>
<th>lastaction</th>
<th>sessionid</th>
</tr>
</thead>
<tbody>
<tr>
<td>user</td>
<td>127.0.0.1</td>
<td>2011-06-13 00:53:05</td>
<td>h0atrutpu5vgrvuf3ipledfbvl1</td>
</tr>
<tr>
<td>admin</td>
<td>127.0.0.1</td>
<td>2011-06-12 00:43:53</td>
<td>nnbdofsdq4sd7e3def3hnid9</td>
</tr>
<tr>
<td>ccm</td>
<td>127.0.0.1</td>
<td>2011-06-10 00:30:22</td>
<td>di34bndffgrt48cmkl08d8e4</td>
</tr>
</tbody>
</table>