I'm using Python 2.7 to convert an XML response (from a REST call to Atlassian Fisheye) into an HTML table.
The XML data is conveniently presented in "row" elements, and also includes the relevant "headings". And, right now, I can produce a great looking table, except that the header row is printed last, as the headings are specified at the end of the XML document.
What's the simplest way of making sure they're at the top? Can I do this with XSLT or do I need to manipulate the XML document first before converting it to HTML? If the latter, what's the easiest/neatest way to reorder the XML elements?
The code I'm using to convert the XML to HTML is:
from lxml import etree
def xml_to_html(text):
source = etree.fromstring(text)
xslt_doc = etree.parse("change-report.xslt")
xslt_transformer = etree.XSLT(xslt_doc)
output_doc = xslt_transformer(source)
print(str(output_doc))
output_doc.write("change-report.html", pretty_print=True)
The XSLT I'm using looks like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table><xsl:apply-templates/></table>
</body>
</html>
</xsl:template>
<xsl:template match="headings">
<tr><xsl:apply-templates select="heading"/></tr>
</xsl:template>
<xsl:template match="heading">
<th><xsl:value-of select="."/></th>
</xsl:template>
<xsl:template match="row">
<tr><xsl:apply-templates select="item"/></tr>
</xsl:template>
<xsl:template match="item">
<td><xsl:value-of select="."/></td>
</xsl:template>
</xsl:stylesheet>
The input XML (returned by Fisheye's REST API) looks like this:
<?xml version="1.0" ?>
<tabularQueryResult>
<row>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">167</item>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">trunk/build.gradle</item>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">ABC-1835 Include RPM building code</item>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">u4538</item>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">2018-03-13T11:43:15Z</item>
</row>
<row>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">166</item>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">trunk/settings.gradle</item>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">ABC-1863 Added new subproject.</item>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">a2345</item>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">2018-03-06T13:31:15Z</item>
</row>
<row>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">165</item>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">trunk/build.gradle</item>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">ABC-1826 Refactoring.</item>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">u4538</item>
<item xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">2018-02-28T10:56:15Z</item>
</row>
<headings>
<heading>csid</heading>
<heading>path</heading>
<heading>comment</heading>
<heading>author</heading>
<heading>date</heading>
</headings>
</tabularQueryResult>