Thursday, June 23, 2005

Using Java to perform URL encoding inside XSLT

I originally posted this on the JASIG wiki, I have found the following tip useful and it was also tricky to find so I thought I would reproduce it here also. It concerns how to make use of Java functionality inside XSL. I originally found this information on this site: Using Java in XSL.

Although not generally encouraged there are times when it is easier to let Java do some of the work rather than trying to write an elegant XSLT solution. The main disadvantage of using Java inside XSL is that the XSL created immediately becomes Java XSLT engine specific.

The following example is derived from a real problem when I wanted to pass an URL as a parameter to a newsfeed aggregator application through an external link from a portal. Without first being URL encoded the following two examples will not pass easily as parameters. Admittedly, I could URL encode these before I store them inside the XML but I think this makes the XML less human readable and rather pollutes the data for other uses.


<?xml version="1.0" ?>
<feeds>
<feed>
<title>Moreover Engineering News</title>
<url>http://p.moreover.com/cgi-local/page?index_engineering%20rss</url>
</feed>
<feed>
<title>SOSIG: Conferences in Anthropology</title>
<url>http://www.sosig.ac.uk/grapevine/Query?view=rss_confBySubject&sub_id=a</url>
</feed>
</feeds>

The following XSLT makes use of Java to perform the URL encoding process without the need to perform any pre/post processing outside of the XSL transformation itself. To perform this transformation you must use the Java Xalan transformation engine. My portal software, uPortal, uses Xalan by default but if you'd like to see the results of this transformation externally to a portal then see: Using Xalan from the Command Line alternatively if you are using Ant you could use the Xslt/Style Task (which itself uses Xalan).


<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xalan"
exclude-result-prefixes="xalan">
<xsl:output method="html" />

<xsl:template match="/">
<xsl:apply-templates select="feeds" />
</xsl:template>

<xsl:template match="feeds">
<html>
<head>
<title>Blah..</title>
</head>
<body>
<xsl:apply-templates select="feed" />
</body>
</html>
</xsl:template>

<xsl:template match="feed">
<xsl:variable xmlns:encoder="xalan://java.net.URLEncoder"
name="feedlink"
select="encoder:encode( string( url ) ) "
/>
<xsl:element name="a">
<xsl:attribute name="href">http://somewhere.edu/index.jsp?rss=
<xsl:value-of select="$feedlink" /></xsl:attribute>
<xsl:value-of select="title" />
</xsl:element>
<br />
</xsl:template>

</xsl:stylesheet>

Note: The line spacing in the above stylesheet was modified slightly for presentation purposes.

2 comments:

madin said...

Late in the 19th century it was pocket and chatelaine watches that were most popular, but Louis replica Cartier watches saw a future in timepieces that could be worn on the wrist.Invest in an heirloom to cherish forever with a timepiece from the replica Ballon Bleu de Cartier watches watch collection.A fusion of cutting-edge technology and heritage craftsmanship, Cartier’s fine watches carry the prestige of the brand’s illustrious history and exceptional quality.

madin said...

replica Cartier watches is a noted jewelry brand with luxurious elegance and technical innovation. It has massive selection of product lines ranging from jewellery, bridal, accessories, leather goods to timepieces. In our leading online shop, we are particularly specialized in selling Swiss replica Cartier watches. There is no denying the fact that Cartier watches are within the dream list of many women. However, they come with astronomical prices which are far beyond the reach of many people. In consideration of this problem, we miniforwatch website provide the final products of Swiss replica Cartier watches, which are much more authentic than that of our competitors, and at a fraction of the cost of originals. Furthermore, we always take attention to details, high-end materials, fine watchmaking and advanced design.