XSLT,XML:HTML Table 内容未显示
XSLT, XML: HTML Table content not displaying
我有这个XML:
<table title = "Fuzzy Bunnies">
<tgroup cols = "2">
<tbody>
<colspec colname = "Bunny Name"></colspec>
<colspec colname = "Bunny Traits"></colspec>
<row>
<entry>
Bugs Bunny
</entry>
<entry>
Likes to mess with people. Known for saying "What's up, Doc?"
</entry>
</row>
<row>
<entry>
Easter Bunny
</entry>
<entry>
Brings candy and/or hides eggs on Easter.
</entry>
</row>
<row>
<entry>
Little Bunny Foo Foo
</entry>
<entry>
Used to pick up field mice and bop them on the head. Turned into a goon by the good fairy.
</entry>
</row>
<row>
<entry>
Roger Rabbit
</entry>
<entry>
Toon who was framed.
</entry>
</row>
<row>
<entry>
Peter Rabbit
</entry>
<entry>
Ridiculously cute. Loses his jacket and shoes stealing veggies.
</entry>
</row>
</tbody>
</tgroup>
</table>
和这个 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="@*|node()">
<html>
<head>
<style>
body{font-family:Arial;font-size:.95em;}
p{font-family:Arial;font-size:.95em;text-align:left;}
h1{font-size:1.25em;color:Navy;font-weight:bold;}
h2{font-size:1em;}
h3{font-size:.75em;font-weight:bold;}
.caption{font-weight:bold;text-align:center;}
.graphic{text-align:center;}
table{border-collapse:collapse;}
table,th,td{border:1px solid gray;}
th{color:white;background-color:Navy;}
td{font-size:.9em;}
</style>
</head>
<body>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</body>
</html>
</xsl:template>
<xsl:template match = "table">
<table>
<tr>
<th colspan="2">Fuzzy Bunnies</th>
</tr>
<tr>
<th>Bunny Name</th>
<th>Bunny Traits</th>
</tr>
<xsl-apply-templates select = "row"/>
</table>
</xsl:template>
<xsl:template match = "row">
<tr>
<xsl:apply-templates select="entry"/>
</tr>
</xsl:template>
<xsl:template match = "entry">
<td>
<xsl:value-of select ="."/>
</td>
</xsl:template>
</xsl:stylesheet>
我想要一个 HTML table,其中包含模板中列出的标题,并且每个条目都在其自己的单元格中。当我在 Firefox 或 IE 中打开文件时,我得到 table header,但没有内容。
有几点需要注意。第一,这是一个更大的 XML 文件的一大块。我从 XSLT 中删除的唯一内容是不属于 table 的节点的模板。同样,对于 XML,我已将其缩减为 table。另一个是 XML 是基于我无法更改的标准格式化的,因此任何更改都需要对 XSLT 进行。
我查看了其他 table 个问题,我认为这不是重复的。
嗯,你有非常奇怪的模式和 select 表达式,例如为什么你要为所有节点 match="@*|node()"
输出一个完整的 HTML 文档结构?通常你想为 match="/"
或 match="/*"
.
这样做
至于问题,row
元素不是 table
的子元素,而是嵌套在结构的更下方,因此在 table
模板中执行 <xsl-apply-templates select = "row"/>
不会处理行,您需要 select=".//row"
.
当然,您的 "cut out of the XSLT is templates for nodes that aren't part of the table" 部分可能还有其他问题,如果您不确保处理到达 table
元素,则永远不会使用该模板.
我有这个XML:
<table title = "Fuzzy Bunnies">
<tgroup cols = "2">
<tbody>
<colspec colname = "Bunny Name"></colspec>
<colspec colname = "Bunny Traits"></colspec>
<row>
<entry>
Bugs Bunny
</entry>
<entry>
Likes to mess with people. Known for saying "What's up, Doc?"
</entry>
</row>
<row>
<entry>
Easter Bunny
</entry>
<entry>
Brings candy and/or hides eggs on Easter.
</entry>
</row>
<row>
<entry>
Little Bunny Foo Foo
</entry>
<entry>
Used to pick up field mice and bop them on the head. Turned into a goon by the good fairy.
</entry>
</row>
<row>
<entry>
Roger Rabbit
</entry>
<entry>
Toon who was framed.
</entry>
</row>
<row>
<entry>
Peter Rabbit
</entry>
<entry>
Ridiculously cute. Loses his jacket and shoes stealing veggies.
</entry>
</row>
</tbody>
</tgroup>
</table>
和这个 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="@*|node()">
<html>
<head>
<style>
body{font-family:Arial;font-size:.95em;}
p{font-family:Arial;font-size:.95em;text-align:left;}
h1{font-size:1.25em;color:Navy;font-weight:bold;}
h2{font-size:1em;}
h3{font-size:.75em;font-weight:bold;}
.caption{font-weight:bold;text-align:center;}
.graphic{text-align:center;}
table{border-collapse:collapse;}
table,th,td{border:1px solid gray;}
th{color:white;background-color:Navy;}
td{font-size:.9em;}
</style>
</head>
<body>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</body>
</html>
</xsl:template>
<xsl:template match = "table">
<table>
<tr>
<th colspan="2">Fuzzy Bunnies</th>
</tr>
<tr>
<th>Bunny Name</th>
<th>Bunny Traits</th>
</tr>
<xsl-apply-templates select = "row"/>
</table>
</xsl:template>
<xsl:template match = "row">
<tr>
<xsl:apply-templates select="entry"/>
</tr>
</xsl:template>
<xsl:template match = "entry">
<td>
<xsl:value-of select ="."/>
</td>
</xsl:template>
</xsl:stylesheet>
我想要一个 HTML table,其中包含模板中列出的标题,并且每个条目都在其自己的单元格中。当我在 Firefox 或 IE 中打开文件时,我得到 table header,但没有内容。
有几点需要注意。第一,这是一个更大的 XML 文件的一大块。我从 XSLT 中删除的唯一内容是不属于 table 的节点的模板。同样,对于 XML,我已将其缩减为 table。另一个是 XML 是基于我无法更改的标准格式化的,因此任何更改都需要对 XSLT 进行。
我查看了其他 table 个问题,我认为这不是重复的。
嗯,你有非常奇怪的模式和 select 表达式,例如为什么你要为所有节点 match="@*|node()"
输出一个完整的 HTML 文档结构?通常你想为 match="/"
或 match="/*"
.
至于问题,row
元素不是 table
的子元素,而是嵌套在结构的更下方,因此在 table
模板中执行 <xsl-apply-templates select = "row"/>
不会处理行,您需要 select=".//row"
.
当然,您的 "cut out of the XSLT is templates for nodes that aren't part of the table" 部分可能还有其他问题,如果您不确保处理到达 table
元素,则永远不会使用该模板.