Raf's Lab

Tuesday May 18, 2010

Calling Web Services from XSLT

One of the little known features of XML and XSLT functions is the document() function, which allows a remote document to be loaded and its tree to be navigable as well as the main document. This allows the ability to call remote Web Services and automatically iterate through the result XML returned
e.g. Using the below Google GeoCoding Service allows us to send a Web Service call to Google and place their resultant result back into the document

<xsl:stylesheet xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ws="http://earth.google.com/kml/2.0" exclude-result-prefixes="ws" version="2.0">



<xsl:template match="/">

        
<mini>

                
<xsl:apply-templates />

        
</mini>

</xsl:template>



<xsl:template match="event/postcode">

    
<xsl:variable name="gglResponses">

            
<xsl:copy-of select="document( 'http://maps.google.com/maps/api/geocode/xml?address=CV344SA,UK&amp;sensor=false' )" />

    
</xsl:variable>

    

        
<xsl:value-of select="$gglResponses/GeocodeResponse/result/geometry/location/lat" />

        
<xsl:value-of select="$gglResponses/GeocodeResponse/result/geometry/location/lng" />

</xsl:template>

</xsl:stylesheet>



This obviously only works with Web Services that are GET based, and therefore wouldn't work on POST based requests, such as SOAP

Comments:

Post a Comment:
  • HTML Syntax: Allowed

Calendar

Search

Twitter

Feeds

Links

Navigation

Referrers