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,670 points
12 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" />
answered Oct 8, 2018 by Priyaj
• 56,120 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 ANMOL
• 3,620 points
31 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 in Python by SDeb
• 9,660 points
31 views
+2 votes
2 answers

Error while printing hello world in python.

You must be trying this command in ...READ MORE

answered Mar 31, 2018 in Python by GandalfDwhite
• 1,320 points
33 views
+2 votes
2 answers

How to use BeatifulSoup for webscraping?

your programme is fine until you start ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,710 points
16 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
24 views
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
• 56,120 points
14 views
0 votes
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
• 56,120 points
73 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.