我正在尝试使用 key 函数将 xml 转换为 html 看起来像:
但我不知道如何制作两个表,因为我的 if 函数将书籍放在另一个之下。XML 看起来像:
<library>
<books>
<book aut="JKR">
<title>Harry Potter and the Sorcerer's Stone</title>
<quantity>5</quantity>
</book>
</books>
<books>
<book aut="JKR">
<title>example</title>
<quantity>3</quantity>
</book>
<book aut="AC">
<title>example</title>
<quantity>2</quantity>
</books>
<authors>
<author id="JKR">J.K.Rowling</author>
<author id="AC"> Agatha Christie</author>
<authors>
并且是片段 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" indent="yes" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />
<xsl:output method="html" encoding="iso-8859-2" />
<xsl:key name="idauthor" match="author" use="@id"/>
<xsl:template match="/">
<html>
<head>
<meta content="text/html" />
</head>
<body>
<table border="1">
<xsl:apply-templates select="//book"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="book">
<xsl:if test="key('idauthor',@author)">
<tr> author:
<td> <xsl:value-of select="title" /></td>
<td> <xsl:value-of select="quantity" /></td>
</tr>
</xsl:if >
</xsl:template>
</xsl:stylesheet>