Raf's Lab

Wednesday May 19, 2010

Making XML look pretty

One of the main issues with pasting XML into a blogging tool or web page, is that the resulting HTML can become very difficult to read e.g.

<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>



As compared to

<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>

By making use of the XSLT transformation taken from http://www2.informatik.hu-berlin.de/~obecker/XSLT we can prettify our XML and make it much easier to read.

Comments:

Post a Comment:
  • HTML Syntax: Allowed

Calendar

Search

Twitter

Feeds

Links

Navigation

Referrers