Viewing XML-files with optimization results in a browser

12 March 2019, 15:44
Stanislav Korotky
2
2 461

MetaTrader 5 provides an option to save optimization results into XML-file. Later it can be loaded in an external program which supports XML, such as Excel or other spreadsheet application. Unfortunately, there can be situations when such programs are unavailable due to their specificity. On the other hand, a web browser is most likely present on every PC, where MetaTrader runs. So, it would be great just to open the XML-file in the browser. If you do it straight away, you'll see something like this.

XML-file with optimization report in a browser

The browser reports that it does not know how to render this page and shows its raw representation. Though it's readable, it's not easy to percept.

Actually, the browser gives us a clue how to remedy the situation. It says:

This XML file does not appear to have any style information associated with it.

So, we need to provide the style information somehow.

Most appropriate way to do so is to use so-called XSLT-transformation. To apply it to an XML file one can make a small edit in the file. Any text editor will suit.

For example, if a standard MetaTrader 5 optimization report starts like this: 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
...

one can add one single line to it:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="worksheet.xsl"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
...

save, and reload in the browser. This will activate specified stylesheet (the file worksheet.xsl in the example) on this XML file. Of course, the XSL-file must be placed beforehand in the same folder where the XML-file is located.

The optimization report is actually an Excel spreadsheet, so there exist many ready-made stylesheets already which can do the job. One of them, worksheet.xsl, is attached below.

The problem with this approach is that not all browsers will allow the XSL-file to load from your local disk. Specificaly, one of most popular browsers - Chrome  - blocks this operation. If you try to load the edited XML in Chrome, you'll get nothing except an error in the developer console:  

Unsafe attempt to load URL file:///.../worksheet.xsl from frame with URL file:///.../ReportOptimizer-xsl.xml. 'file:' URLs are treated as unique security origins.

This "feature" is widely accepted in technical circles as one of most odious decision of Chrome developers. No other browser apply this limitation, all of them support local file loading initiated from local pages (so if you use not Chrome, but other browser, you'll get proper HTML for XML on this stage; as for the others, we need to find a workaround). With this solution Google breaks consistency with other "styling" files (Cascading Style Sheets, CSS), which are also often loaded from the same origin as hosted web-page, and Chrome treats local disk the same origin in this case. All other browsers do the same, of course.

To solve the problem with Chrome, we can run a web-server locally and access both XML and XSL files via HTTP protocol. Today one can find many lightweight and small web-servers for personal use, which are easy to install on PCs.

In either way - if you use proper web browser (not a Chrome) or if you load XML/XSL files via a web-server - you'll get the following page as a result of XSLT transformation.

Web-page with optimization report (built using XSLT)

Not bad.

Yet there is another solution. It utilizes abovementioned CSS. Instead of applying XSL to XML-file, we can apply CSS to XML. For doing so, one should edit the XML in the following way:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/css" href="xml.css"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
...

Here we added the reference to CSS-file xml.css. It's attached below as well.

If you realod the XML-file in the browser you'll see something like this:

Web-page with optimization report (built using CSS)

It looks great.

This seamlessly works with local files and does not require a local web-server.

Please, note that the attached files are actually worksheet.xsl and xml.css. The additional extension ".txt" was added because the blog does not allow to attach files of original types. After downloading you should remove the excessive extensions.

  
Files:
Share it with friends: