An XSLT stylesheet is an XML document that contains templates. Each template matches a pattern of nodes and specifies the output to generate:
<?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" indent="yes"/>
<!-- Match the root element -->
<xsl:template match="/library">
<html>
<body>
<h1>Book Catalogue</h1>
<table border="1">
<tr><th>ID</th><th>Title</th><th>Author</th><th>Year</th></tr>
<xsl:apply-templates select="book"/>
</table>
</body>
</html>
</xsl:template>
<!-- Match each book element -->
<xsl:template match="book">
<tr>
<td><xsl:value-of select="@id"/></td>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="author"/></td>
<td><xsl:value-of select="year"/></td>
</tr>
</xsl:template>
</xsl:stylesheet>
Apply the stylesheet from the command line using xsltproc (part of libxslt):
sudo apt install xsltproc
xsltproc library.xsl library.xml > output.html
Use XSLT in Python with lxml:
from lxml import etree
xml_tree = etree.parse("library.xml")
xsl_tree = etree.parse("library.xsl")
transform = etree.XSLT(xsl_tree)
result = transform(xml_tree)
print(str(result))