我有以下XML代码,我尝试使用XSLT来显示。如何选择字段名并返回表中的所有值。我尝试过,但它不会工作,只是只返回第一个记录('用户名'和'用户')。请帮忙,非常感谢。
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>
看看这个模板。我已经尽可能地按照你的模板去做了。我只添加了一个模板(并且为了可读性清理了输出)。
(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>