好的,我是XML和XSL的新手。最后,我想根据用户输入使用过滤器填充此表,但我认为我将从整个表开始。
以下是我的XML文件的一部分:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="XSLstylesheet.xsl"?>
<data-set>
<rep>
<RepID>12345</RepID>
<Rep>SomeName</Rep>
<Dept>PS</Dept>
<Station>1</Station>
<ClassDate>41593</ClassDate>
<Agency>AGency</Agency>
<EmAppr>y</EmAppr>
</rep>
<rep>
<RepID>98765</RepID>
<Rep>Another Name</Rep>
<Dept>HC</Dept>
<Station>2</Station>
<ClassDate>41593</ClassDate>
<Agency>Next Agency</Agency>
<EmAppr>y</EmAppr>
</rep>
...
</rep>
</data-set>
所以这是我的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="/">
<html>
<body>
<h2>Representatives</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Rep</th>
<th>Rep ID</th>
<th>Department</th>
<th>Station</th>
<th>Start Date</th>
<th>Agency</th>
<th>Out of Approval Queue</th>
</tr>
<xsl:for-each select="data-set/rep">
<tr>
<td><xsl:value-of select="data-set/Rep"/></td>
<td><xsl:value-of select="data-set/RepID"/></td>
<td><xsl:value-of select="data-set/Dept"/></td>
<td><xsl:value-of select="data-set/Station"/></td>
<td><xsl:value-of select="data-set/ClassDate"/></td>
<td><xsl:value-of select="data-set/Agency"/></td>
<td><xsl:value-of select="data-set/EmAppr"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
当我查看它时,表格会显示正确的行数和列数,但绝对没有文字。这就像它识别记录但不填充它。我哪里做错了?我正在撕开我的头发。
答案 0 :(得分:2)
你的xpath错了。改变
<td><xsl:value-of select="data-set/Rep"/></td>
到
<td><xsl:value-of select="Rep"/></td>
您已访问节点data-set/rep
,因此您无需再次添加data-set
。您当前的代码正在data-set/data-set/rep
访问每个节点,这看起来像是一个错字。
对示例
中的所有for-each语句执行此操作<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Representatives</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Rep</th>
<th>Rep ID</th>
<th>Department</th>
<th>Station</th>
<th>Start Date</th>
<th>Agency</th>
<th>Out of Approval Queue</th>
</tr>
<xsl:for-each select="data-set/rep">
<tr>
<td>
<xsl:value-of select="Rep"/>
</td>
<td>
<xsl:value-of select="RepID"/>
</td>
<td>
<xsl:value-of select="Dept"/>
</td>
<td>
<xsl:value-of select="Station"/>
</td>
<td>
<xsl:value-of select="ClassDate"/>
</td>
<td>
<xsl:value-of select="Agency"/>
</td>
<td>
<xsl:value-of select="EmAppr"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>