Re-order XML for HTML printing

0 votes

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>

Oct 8, 2018 in Python by eatcodesleeprepeat
• 4,710 points
993 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" />
answered Oct 8, 2018 by Priyaj
• 58,020 points

Related Questions In Python

+1 vote
1 answer

What is the correct order to learn concepts in Python for machine learning?

Machine Learning is a vast domain. It ...READ MORE

answered Jul 25, 2018 in Python by Abhi
• 3,720 points
1,062 views
0 votes
1 answer

Escaping strings for use in XML

You can try the following: from xml.dom.minidom import ...READ MORE

answered Apr 15, 2019 in Python by SDeb
• 13,300 points
1,035 views
0 votes
0 answers
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,443 views
0 votes
1 answer
0 votes
1 answer

I'm using Python 2.7 to convert an XML response (from a REST call to Atlassian Fisheye) into an HTML table.

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

answered Oct 4, 2018 in Python by Priyaj
• 58,020 points
1,452 views
+1 vote
1 answer

Why is openpyxl is required for loading excel format files?

Well, it sounds like openpyxl is not ...READ MORE

answered Aug 8, 2018 in Python by Priyaj
• 58,020 points
1,055 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