I m using Python 2 7 to convert an XML response from a REST call to Atlassian Fisheye into an HTML table

0 votes

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>

Oct 4, 2018 in Python by eatcodesleeprepeat
• 4,710 points
1,446 views

1 answer to this question.

0 votes

You don't have a template matching tabularQueryResult in your XSLT, which means the built-in template rules will apply. This will just select the child nodes of tabularQueryResult in document order. As headings is after row in the input XML, they come out last. (The fact you have a template matching headings before the template matching rows really makes no difference whatsoever).

To solve this, just add a template for tabularQueryResult and explicitly select the order you want.

<xsl:template match="tabularQueryResult">
  <xsl:apply-templates select="headings" />
  <xsl:apply-templates select="row" />
</xsl:template>
answered Oct 4, 2018 by Priyaj
• 58,020 points

Related Questions In Python

0 votes
1 answer

How to convert an integer to a string using Python?

Here is an easy solution: def numberToBase(n, b): ...READ MORE

answered Nov 23, 2018 in Python by Nymeria
• 3,560 points

edited Dec 12, 2018 by Nymeria 1,162 views
0 votes
1 answer

How to have values from a List1 into Columns Names and List 2 Values as Index for an Empty DataFrame

Try like this, it will give you ...READ MORE

answered Jul 10, 2019 in Python by Cherukuri
• 33,030 points
1,046 views
0 votes
1 answer

How to import an image into a csv file using python?

I am sure you are aware CSV ...READ MORE

answered Jun 25, 2020 in Python by Bhanu Kumar
7,932 views
0 votes
1 answer
0 votes
1 answer

Re-order XML for HTML printing

You don't have a template matching tabularQueryResult in your ...READ MORE

answered Oct 8, 2018 in Python by Priyaj
• 58,020 points
989 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer

How to change python version into 3.7.2 for entire pycharm jetbrains IDE projects?

Navigate to the Project page, select the configured interpreters ...READ MORE

answered Feb 27, 2019 in Python by Priyaj
• 58,020 points
3,369 views
0 votes
1 answer

Convert string list of dict from csv into JSON object in python

You can use the ast module Ex: import ast s = """[{'10': ...READ MORE

answered Sep 12, 2018 in Python by Priyaj
• 58,020 points
2,557 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP