XML преобразовать в HTML с помощью XSLT
Я пытаюсь использовать функцию ключа для преобразования 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>
1 ответ
Если ваш вклад имеет authors
индекс, и вы хотите сообщить по автору, то вы должны начать с этого.
<xsl:apply-templates select="authors/author"/>
Затем вы хотите применить ключ к книгам, а не к авторам:
<xsl:key name="books-by" match="book" use="@aut"/>
Наконец, для каждого автора вы хотите выбрать книги по идентификатору автора:
<table border="1">
<xsl:apply-templates select="key('books-by', @id)" />
</table>
Следующее преобразование
<?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="books-by" match="book" use="@aut"/>
<xsl:template match="/library">
<html>
<head>
<meta content="text/html" />
</head>
<body>
<xsl:apply-templates select="authors/author"/>
</body>
</html>
</xsl:template>
<xsl:template match="author">
<h2>
<xsl:value-of select="."/>
</h2>
<table border="1">
<xsl:apply-templates select="key('books-by', @id)" />
</table>
</xsl:template>
<xsl:template match="book">
<tr>
<td> <xsl:value-of select="position()" /></td>
<td> <xsl:value-of select="title" /></td>
<td> <xsl:value-of select="quantity" /></td>
</tr>
</xsl:template>
</xsl:stylesheet>
применяется к вашему (исправленному) вводу,
<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>
</book>
</books>
<authors>
<author id="JKR">J.K.Rowling</author>
<author id="AC"> Agatha Christie</author>
</authors>
</library>
производит следующий HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta content="text/html">
</head>
<body>
<h2>J.K.Rowling</h2>
<table border="1">
<tr>
<td>1</td>
<td>Harry Potter and the Sorcerer's Stone</td>
<td>5</td>
</tr>
<tr>
<td>2</td>
<td>example</td>
<td>3</td>
</tr>
</table>
<h2> Agatha Christie</h2>
<table border="1">
<tr>
<td>1</td>
<td>example</td>
<td>2</td>
</tr>
</table>
</body>
</html>