【发布时间】:2014-03-08 18:03:40
【问题描述】:
我是 XML 转换的新手,目前正在处理 XSLT 文件。我想在我在第一个模板中创建的表中将每个“人”元素放置到不同的行及其子节点(名称、acct-no、fav-color)到每行的不同列。我正在应用与“person”元素匹配的第二个模板,因此可以为每个 person 元素插入一个表格行及其单元格。
这是我的 XML 文件。
<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
<?xml-stylesheet href="people.xsl" type="text/xsl"?>
<people>
<list-name>Favorite Colors</list-name>
<person>
<name>
<first>Bob</first>
<last>Toddson</last>
</name>
<acct-no>327598</acct-no>
<fav-color hex="#ff0000">Red</fav-color>
</person>
<person>
<name>
<first>Red</first>
<last>McBlue</last>
</name>
<acct-no>209890</acct-no>
<fav-color hex="#00ff00">Green</fav-color>
</person>
<person>
<name>
<first>Tammy</first>
<last>Yu</last>
</name>
<acct-no>978541</acct-no>
<fav-color hex="#7fff00">Chartreuse</fav-color>
</person>
<person>
<name>
<first>Phillip</first>
<last>Cardwell</last>
</name>
<acct-no>258929</acct-no>
<fav-color hex="#d2b48c">Tan</fav-color>
</person>
</people>
还有 XSL 文件。
<xsl:template match="/">
<head>
<title> People Report: <xsl:value-of select="/people/list-name" />
</title>
</head>
<body>
<center>
<h1> People Report: <xsl:value-of select="/people/list-name" /> </h1>
</center>
<table border="1" align="center">
<tr>
<td>Last Name</td>
<td>First Name</td>
<td>Account Number</td>
<td>Favorite Color</td>
</tr>
<xsl:apply-templates select="/people/person" />
</table>
</body>
</html>
</xsl:template>
<xsl:template match="person">
<tr>
<td>
<xsl:value-of select="name/last" />
</td>
<td>
<xsl:value-of select="name/first" />
</td>
<td>
<xsl:value-of select="acct-no" />
</td>
<td>
<xsl:value-of select="fav-color" />
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
我当前的输出仅将所有 person 元素及其子节点放在表的第一行第一列中。我认为我的问题是由于没有使用 for-each 命令和里面的 apply-templates,所以它可以迭代每个 person 元素。正如我在 SE 的相关帖子中所读到的,apply-templates 命令将适用于每个 person 元素,因此在我的情况下不需要使用 for-each。我已经挣扎了几个小时了,我看不到或不明白我在哪里遗漏了一些东西,所以任何帮助都将不胜感激。
预期输出:
我得到的输出:
先谢谢了。
【问题讨论】:
-
你能发布你想要达到的输出吗?
-
"我当前的输出仅将所有 person 元素及其子节点放在表的第一行第一列中。" 你确定吗?使用您的输入和样式表,我得到了一个 4x4 表格,其中所有内容都在其位置。