当我对每个错误或我认为不正确的内容不正确时,它会显示一个空图表。当我拥有我认为适合每个人的权利时,它不会显示任何格式。
这是我的XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="cat-department.xsl" type = "text/xsl" ?>
<department xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
<foo:person>
<title>Assistant Professor of Sports and Recreation</title>
<firstname>Hobbes</firstname>
<lastname>Cat</lastname>
<office>Cougar Center, KM 103</office>
<phone>765-123-5678</phone>
<email>hobbes@cat.net</email>
</foo:person>
<foo:person>
<title>Professor of Feline Economics</title>
<firstname>Felix</firstname>
<lastname>The Cat</lastname>
<office>Main Building, KO 170</office>
<phone>765-555-1234</phone>
<email>felix@cat.net</email>
</foo:person>
<foo:person>
<title>Professor of Feline Gastronomy</title>
<firstname>Garfield</firstname>
<lastname>Cat</lastname>
<office>East Building, KE 255</office>
<phone>765-123-4321</phone>
<email>garfield@cat.net</email>
</foo:person>
<foo:person>
<title>Adjunct Instructor</title>
<firstname>Kingston</firstname>
<lastname>Cougar</lastname>
<office>Main Building, KO 210D</office>
<phone>765-789-3456</phone>
<email>cougark@iu.edu</email>
</foo:person>
</department>
这是我的XSL:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2> The last names Sorted are</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>First Name</th>
<th>Last Name</th>
<th>Phone </th>
<th>Email</th>
</tr>
<xsl:for-each select="department/foo:person">
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="office"/></td>
<td><xsl:value-of select="phone"/></td>
<td><xsl:value-of select="email"/></td>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
当我在每个中添加随机乱码时,它会显示一个空图。当我提出我认为正确的内容时,它会显示我的xml文件中的文本,但是没有任何格式化。
答案 0 :(得分:0)
您已省略<tr>...</tr>
中的for-each
。
</tr>
<xsl:for-each select="department/foo:person">
<tr> <!-- HERE -->
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="office"/></td>
<td><xsl:value-of select="phone"/></td>
<td><xsl:value-of select="email"/></td>
</tr> <!-- AND HERE -->
</xsl:for-each>
</table>
答案 1 :(得分:0)
XSLT中存在多个问题。
xmlns:foo="http://www.foo.org/"
,因此尚未在XSLT中声明名称空间xmlns:bar="http://www.bar.org"
和foo:person
。<th>Office</th>
丢失,导致一列没有html输出中的标题。for-each
内,您需要为每个<tr>....</tr>
数据添加foo:person
,否则输出表将会变得一团糟。xsl:sort
来实现这一目标。我在lastname
上添加了排序。以下是更新的XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org"> <!-- Namespaces added -->
<xsl:output method="html" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<html>
<body>
<h2> The last names Sorted are</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>First Name</th>
<th>Last Name</th>
<th>Office</th> <!-- Added header element -->
<th>Phone </th>
<th>Email</th>
</tr>
<xsl:for-each select="department/foo:person">
<xsl:sort data-type="text" order="ascending" select="lastname" /> <!-- sorting applied on lastname -->
<tr> <!-- Added row for the data -->
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="firstname"/></td>
<td><xsl:value-of select="lastname"/></td>
<td><xsl:value-of select="office"/></td>
<td><xsl:value-of select="phone"/></td>
<td><xsl:value-of select="email"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
HTML输出