我有一个作业要求我从 xml 中获取所有数据并将其显示到一个表中,该表显示一个带有“td”Titulo、Autor、Año 的“th”。此“th”必须每 5 行重复一次,但如果最后一行不包含来自 xml 的任何数据,则此“th”不应出现在最后一个位置。
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="biblioteca.xsl"?>
<biblioteca>
<libro>
<titulo>La vida está en otra parte</titulo>
<autor>Milan Kundera</autor>
<fechaPublicacion año="1973"/>
</libro>
<libro>
<titulo>Pantaleón y las visitadoras</titulo>
<autor fechaNacimiento="28/03/1936">Mario Vargas Llosa</autor>
<fechaPublicacion año="1973"/>
</libro>
<libro>
<titulo>Conversación en la catedral</titulo>
<autor fechaNacimiento="28/03/1936">Mario Vargas Llosa</autor>
<fechaPublicacion año="1969"/>
</libro>
<libro>
<titulo>La vida está en otra parte</titulo>
<autor>Milan Kundera</autor>
<fechaPublicacion año="1973"/>
</libro>
<libro>
<titulo>Pantaleón y las visitadoras</titulo>
<autor fechaNacimiento="28/03/1936">Mario Vargas Llosa</autor>
<fechaPublicacion año="1973"/>
</libro>
<libro>
<titulo>Conversación en la catedral</titulo>
<autor fechaNacimiento="28/03/1936">Mario Vargas Llosa</autor>
<fechaPublicacion año="1969"/>
</libro>
<libro>
<titulo>La vida está en otra parte</titulo>
<autor>Milan Kundera</autor>
<fechaPublicacion año="1973"/>
</libro>
<libro>
<titulo>Pantaleón y las visitadoras</titulo>
<autor fechaNacimiento="28/03/1936">Mario Vargas Llosa</autor>
<fechaPublicacion año="1973"/>
</libro>
<libro>
<titulo>Conversación en la catedral</titulo>
<autor fechaNacimiento="28/03/1936">Mario Vargas Llosa</autor>
<fechaPublicacion año="1969"/>
</libro>
<libro>
<titulo>Conversación en la catedral</titulo>
<autor fechaNacimiento="28/03/1936">Mario Vargas Llosa</autor>
<fechaPublicacion año="1969"/>
</libro>
</biblioteca>
这是我的 xsl 代码:
<?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" encoding="UTF-8"/>
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" type="text/css" href="estilo.css"/>
</head>
<body>
<h1>Biblioteca</h1>
<hr style="text-align:left;"/>
<br/>
<table>
<tr id="s" bgcolor="#FFB6C1">
<th>Titulo</th>
<th>Autor</th>
<th>Año</th>
</tr>
<xsl:for-each select="biblioteca/libro">
<tr>
<td><xsl:value-of select="titulo"/></td>
<td><xsl:value-of select="autor"/></td>
<td><xsl:value-of select="fechaPublicacion/@año"/></td>
</tr>
<xsl:if test="position() mod 5 = 0">
<tr bgcolor="#FFB6C1">
<th>Titulo</th>
<th>Autor</th>
<th>Año</th>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我猜它需要一个 xsl:如果在那里 if last()="th" 不会显示但我搜索了几个小时却找不到解决方案,或者如果我找到了,我没有理解,因为是太复杂了,没有理解代码(我正在学习)。
你没有发布预期的结果,所以我在这里有点猜测。
如果你想每隔5点数据的行后重复标题行,那么最简单的解决方案,恕我直言,是把它之前的每一行是一组5行的第一:
XSLT 1.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/biblioteca">
<html>
<body>
<h1>Biblioteca</h1>
<table border="1">
<xsl:variable name="header-row">
<tr>
<th>Titulo</th>
<th>Autor</th>
<th>Año</th>
</tr>
</xsl:variable>
<xsl:for-each select="libro[position() mod 5 = 1]">
<xsl:copy-of select="$header-row"/>
<xsl:for-each select=". | following-sibling::libro[position() < 5]">
<tr>
<td>
<xsl:value-of select="titulo"/>
</td>
<td>
<xsl:value-of select="autor"/>
</td>
<td>
<xsl:value-of select="fechaPublicacion/@año"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
应用于您的 XML 示例,这将产生:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句