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,670 points
13 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 56,120 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,500 points

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

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
419 views
0 votes
1 answer

Shortest path from source to and from a negative cycle using Bellman Ford in Python

class NegativeWeightFinder: def __init__(self, graph: nx.Graph): ...READ MORE

answered Nov 13, 2018 in Python by Nymeria
• 3,500 points
47 views
0 votes
1 answer

How to get the return value from a thread using python?

You don't need to change your existing ...READ MORE

answered Dec 3, 2018 in Python by Nymeria
• 3,500 points

edited Dec 19, 2018 by Nymeria 3,842 views
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
• 56,120 points
11 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
22 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 in Python by Priyaj
• 56,120 points
29 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
• 56,120 points
253 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.