2016-07-02

The importance of XML in web services

‘I want the output from your webservice in JSON not in XML format’ I said to the software vendor techie.
‘Yes of course’ he responded but ‘why not XML? It is simpler to work with and you can use XSLT’.
‘Yes’, I thought, ‘and that crappy XSL language, I never use that’.


We at the Läskeblask Brewery Inc. has bought a service supplying us with Alcohol Awareness Data sheets, required by law for the alcoholic beverages we sell. A data sheet accompanying the sales order when we ship alcoholic goods. When we create the sales order we call this web service with the product number, the web service responds with an XML list of links to Alcohol Awareness data sheets in different languages, which should be displayed in a menu from which the salesman could click on the right sheet.The  problem I had, we had forgot or not thought about this menu, I realised we needed an application in between the sales application and the web service, that’s why I called the vendor techie, to get more details about the web service.


A few hours later it hit me, a browser could probably do an XSL transformation, so I googled around, and sure enough modern browsers can do that. I went to w3schools.com and taught myself enough XSL for the task  in an hour.
I had this XML from the web service:
  
The first thing I realized I needed a proper XML header (is the place to specify the XSL script), so I added one:
<?xml-stylesheet version="1.0" type="text/xsl" href="xsl1.xml"?>


The href points to my XSL script (xsl1.xml). Next I found out XSL can only work with well formed XML data, and this XML was not well formed, If you take a close look at the <ExternalURL> tag above you see it contains solitary ampersands (&) and this is not allowed in XML ‘&’ is written ‘&amp;’ in well formed XML. For this PoC I manually edited my XML script :




Now I had well formed XML script pointing to my XSL script (I started with pinched snippets from w3school and of course googled around, it took me some hours). This is the XSL script:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/"> <!-- this template is for the entire XML file -->
 <html>
 <body>
<xsl:for-each select="ListResultOfMaterial">
   <h2>Alcohol Awareness Data Sheets for: <xsl:value-of select="userPartNumber"/><xsl:text> - </xsl:text><xsl:value-of select="userPartName"/></h2>
   <table border="1">
     <tr bgcolor="#9acd32">
<th>Country</th>
       <th>Language</th>
<th>Issue_Date</th>
       <th>ExternalUrl (for reference only, the URLs are used as links in the Country and Language cells) </th>
     </tr>
     <xsl:for-each select="Document">
<xsl:variable name="cmlink">
          <xsl:value-of select="ExternalUrl"/>
       </xsl:variable>
<xsl:variable name="greendt">
          <xsl:value-of select="substring(IssueDate,1,10)"/>
       </xsl:variable>
       <tr>
     <td><a href="{$cmlink}"><xsl:value-of select="CountryCode" /></a></td>
     <td><a href="{$cmlink}"><xsl:value-of select="LanguageCode" /></a></td>
         <td><xsl:value-of select="substring(IssueDate,1,10)"/></td>
         <td>
<xsl:choose> <!-- if-then-else (if is only simple if statements) -->
  <xsl:when test="$greendt = '2008-03-04'">
     <xsl:attribute name="style">font-family: Helvetica; color: green; font-size: 8pt;</xsl:attribute>
  </xsl:when>
  <xsl:otherwise>
     <xsl:attribute name="style">font-family: Helvetica; color: red; font-size: 8pt;</xsl:attribute>
  </xsl:otherwise>
</xsl:choose>
  <xsl:value-of select="ExternalUrl"/>
 </td>
     </tr>
     </xsl:for-each>
   </table>
</xsl:for-each>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>


And now I fired off


C:\temp\kem.xml


In Chrome.  Nothing happened, absolutely nothing. After a lot of swearing and a very long while and of course googling around, I found out some Chrome developer had decided it was unsafe to invoke an XSL script residing on my PC! So I invoked a better browser IE 10!!!
And there it was a beautifully transformed XML script:
Clicking any of the two first columns in the second row gives you the Alcohol Awareness Data Sheet for the Pan-Galactic Gargle Blaster drink.
I now have ‘the in between menu’ without any web whatsoever just a simple XSL script. Now I only have to persuade the software vendors to change the webservice and host my XSL script.

There is probably better ways to do this, if you know one please let me know. I happily buy you a tin of Pan-Galactic Gargle Blaster when you stop at the Läskeblask Brewery the next time.

No comments:

Post a Comment