Composing URL’s in XSLT

Composing a complex URL can be very ugly in XSLT, using a very long formula of concatenations and encodings.
E.g the REST URL for this YQL query:

select * from html where url=”http://www.doorstroming.net/index.php/actua/49-qnationalisme-is-nationalismeq.html” and
xpath=’//*[contains(concat( ” “, @class, ” ” ), concat( ” “, “MsoNormal”, ” ” ))]’

Is ugly as sin:

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Fwww.doorstroming.net%2Findex.php%2Factua%2F49-qnationalisme-is-nationalismeq.html%22%20and%20%20%20%20%20%20xpath%3D’%2F%2F*%5Bcontains(concat(%20%22%20%22%2C%20%40class%2C%20%22%20%22%20)%2C%20concat(%20%22%20%22%2C%20%22MsoNormal%22%2C%20%22%20%22%20))%5D’&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

Two possible improvements:

Dedicated URL composition template

Create a template that can be called like this:

<xsl:call-template name=”url”>
<xsl:with-param name=”decom”>
<url>
<base>http://query.yahooapis.com/v1/public/yql</base&gt;
<query>
<name>q</name>
<value><xsl:value-of select=”$query”/></value>
</query>
<query>
<name>diagnostics</name>
<value>false</value>
</query>
<query>
<name>env</name>
<value>store://datatables.org/alltableswithkeys</name>
</query>
</url>
</xsl:with-param>
</xsl:call-template>

I’m not elaborating this template here. It should be quite straightforward, but there may be a more generic and even more elegant solution:

String Replacement Template

Using the replace template that was created in the XML Translation project.

<xsl:call-template name=”replace”>
<xsl:with-param name=”string”>
<xsl:text>http://query.yahooapis.com/v1/public/yql?q=$query&diagnostics=true&env=$env</xsl:text&gt;
</xsl:with-param>
<xsl:with-param name=”parameters”>
<query><xsl:value-of select=”encode-for-uri($query)”/></query>
<env><xsl:value-of select=”encode-for-uri(‘store://datatables.org/alltableswithkeys’)”/></env>
</xsl:with-param>
</xsl:call-template>

This is the replace template:

<xsl:template name=”replace”>
<xsl:param name=”string”/>
<xsl:param name=”parameters”/>
<xsl:choose>
<xsl:when test=”count($parameters/*) &gt; 0″>
<xsl:variable name=”string2″>
<xsl:value-of select=”replace($string,concat(‘\$’,name($parameters/*[1])),normalize-space($parameters/*[1]))”/>
</xsl:variable>
<xsl:call-template name=”replace”>
<xsl:with-param name=”string” select=”$string2″/>
<xsl:with-param name=”parameters”>
<xsl:copy-of select=”$parameters/*[position() &gt; 1]”/>
</xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select=”$string”/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s