| |||||||||
Adding filename information to XML during XSLT
Adding filename information to XML during XSLTMy current blogging system constitute a.xsl document and a series of .xml documents and an ANT script. One day I tasked myself with adding links to each
article linking to various social bookmarking services. This was problematic as these links are partly constructed on the basis of the file they are linking to.
Here is a step by step guide on how I made this possible.
Step 1: Getting meta information into the xslThe first step to realizing the social bookmark links was to enable the.xsl file to know what file is being processed. As the xsl have no such meta-information
about the current process, the only thing to do is to call the xslt with the filename and filedirectory as arguments. Hence the build.xml
ANT script becomes:
<xslt basedir="${dir.xml}"
includes="**/*.xml"
...
filenameparameter="filename"
filedirparameter="filedir">
Then this information is reachable inside the .xsl file by defining parameters. My article.xsl header now looks like
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="filename"></xsl:param> <xsl:param name="filedir">.</xsl:param>You can then get hold of this meta information using The current file is http://foo.com/<xsl:value-of select="$filedir"/>/<xsl:value-of select="$filename"/> Step 2: Search and replace the xml contentWhile the$filename and $filedir works, $filename holds the filename with the .xml extension rather than the .html
extension we need for our URLs. Apparently there is no native search/replace function in xslt so you must define one yourself. I'm no XML expert, so
please correct me if I'm wrong in my assumptions. Paul Prescod's
2001 xml recepi was what I needed.
<!-- START - reusable replace-string function -->
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="contains($text, $from)">
<xsl:variable name="before" select="substring-before($text, $from)"/>
<xsl:variable name="after" select="substring-after($text, $from)"/>
<xsl:variable name="prefix" select="concat($before, $to)"/>
<xsl:value-of select="$before"/>
<xsl:value-of select="$to"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$after"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- END - reusable replace-string function -->
Step 3: Define the html filenameEquipped with the meta information on the filename and a search/replace function we can now define a parameter holding the current HTML url<!-- calculate the html path --> <xsl:param name="URLHtmlFile">http://firstclassthoughts.co.uk/<xsl:value-of select="$filedir"/>/<xsl:call-template name="replace-string"> <xsl:with-param name="text"><xsl:value-of select="$filename"/></xsl:with-param> <xsl:with-param name="from">.xml</xsl:with-param><xsl:with-param name="to" >.html</xsl:with-param> </xsl:call-template> </xsl:param>The formatting is pretty horrible, but if you are careless with your newlines then you get unintended whitespace in the filename. Now you can access the url as The current page is <xsl:value-of select="$URLHtmlFile"/>and you can construct your url's like
<!-- digg reddit -->
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
<xsl:template match="link">
<xsl:element name="a">
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:attribute name="href">http://digg.com/submit?phase=2&url=<xsl:value-of select="$URLHtmlFile"/>%26title%3D<xsl:value-of select="$docTitle"/></xsl:attribute>
<img src="{$relpath}/webimg/share_digg.png" alt="save url at digg"/>
</xsl:element>
</xsl:template>
<!-- digg reddit -->
Happy XML'ing...
CommentsIf you have any comments to this article, please drop me a mail at firstclassthoughts at gmail dot com please indicate if I can't publish whole or parts of your comment on the site.If you like this site consider Help spread the wordShare this post on your favorite social bookmarking sites:
The most recent contributions 28/07/09 Magic in mathematics II Fun with the number cyclic numbers, and specifically with 142857 as it is the smallest of such numbers. 13/07/09 My top 8 time-saving Firefox shortcuts This article presents my favorite top 8 time-saving shortcuts in Firefox 3.0 and Firefox 3.5. Get to know these and you'll be saving a lot of time. They have been ordered by "the element of most surprise" 20/05/09 Board Game Jungle speed / Arriba Review of the cool game "Jungle Speed" aka. "Arriba". 16/05/09 Danish Twin words "Twin words" are words that not only have multiple meanings, they must be composed next to each other in meaningful sentences. This article explores the concept of twin words. Nothing of interest? Try browsing the entire article archive... | |||||||||