Pages

Tuesday, August 21, 2012

Modularizing XSLT code (extract file name from path)

Few days back I came across a requirement where I had to determine the name of a file given the absolute path. Had it been java or any other popular programming language I could just tokenize the path with the directory separator character ("/" or "") and taken the last token. However doing it in XSLT 1.0 was bit tricky. A recursive function (templates in case of XSLT 1.0 ) call was required."substring-after" function gives the part of the string after the first occurrence of a given delimiter.

[sourcecode language="xml"]
<xsl:template name="fileName">
<xsl:param name="path" />
<xsl:choose>
<xsl:when test="contains($path,'')">
<xsl:call-template name="fileName">
<xsl:with-param name="path" select="substring-after($path,'')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($path,'/')">
<xsl:call-template name="fileName">
<xsl:with-param name="path" select="substring-after($path,'/')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$path" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>

[/sourcecode]



And to call above code , we have to write the following fragment
[sourcecode language="xml"]
<xsl:call-template name="fileName">
<xsl:with-param name="path" select="D:codetest.txt"/>
</xsl:call-template>
[/sourcecode]

If XMLT 2.0 is available the above job is much easier using base-uri()

25 comments:

  1. ... [Trackback]

    [...] There you will find 23381 more Infos: neilghosh.com/2012/08/20/modularizing-xslt-code-extract-file-name-from-path/trackback/ [...]

    ReplyDelete
  2. Grow Tall and Loss your weight Easily. Visit us at http://goo.gl/DQOdmg and Get it!

    Kami merupakan Apotek Obat Online Terlengkap yang menjual berbagai macam obat peninggi badan dan obat pelangsing badan dengan jamianan keaslian produk. Kunjungi kami di http://goo.gl/DQOdmg dan dapatkan peninggi dan pelangsing badan dengan harga Grosir…

    ReplyDelete
  3. הדרכים המומלצות לעשיית כסף מהבית הדרכים המומלצות לעשיית כסף מהבית

    הדרכים המומלצות לעשיית כסף מהבית

    ReplyDelete
  4. password resetter

    [...]check below, are some totally unrelated websites to ours, nonetheless, they're most trustworthy sources that we use[...]

    ReplyDelete
  5. car crashes

    [...]just beneath, are many completely not related websites to ours, even so, they're surely worth going over[...]

    ReplyDelete
  6. car crashes

    [...]one of our visitors just lately proposed the following website[...]

    ReplyDelete
  7. best of vines

    [...]the time to study or visit the material or websites we've linked to beneath the[...]

    ReplyDelete
  8. My Source These kind of post are always inspiring and I prefer to check out quality content so I happy to unearth many first-rate point here in the post, writing is simply huge, thank you for the post

    These kind of post are always inspiring and I prefer to check out quality content so I happy to unearth many first-rate point here in the post, writing is simply huge, thank you for the post

    ReplyDelete
  9. Website maintenance

    It's nearly impossible to find educated people about this subject, however, you sound like you know what you're talking about! Thanks

    ReplyDelete
  10. The Birch of the Shadow

    I feel there might be considered a number of duplicates, but an exceedingly handy record! I have tweeted this. Several thanks for sharing!

    ReplyDelete
  11. Souls in the Waves

    Good Morning, I just stopped in to visit your website and considered I would say I appreciated myself.

    ReplyDelete
  12. The Silent Shard

    This tends to likely be really handy for a few within your work opportunities I intend to do not only with my blog but

    ReplyDelete
  13. The Birch of the Shadow

    I feel there may possibly be considered a handful of duplicates, but an exceedingly helpful list! I have tweeted this. Numerous thanks for sharing!

    ReplyDelete
  14. Healing's Dragon

    to search out problems to further improve my site!I suppose its ok to help make utilization of a few of the principles!!

    ReplyDelete
  15. Whispering Misty

    So sorry you may pass up the workshop!

    ReplyDelete
  16. The Silent Shard

    This can almost certainly be pretty beneficial for a few of the employment I decide to you should not only with my blog site but

    ReplyDelete
  17. obat sipilis denature indonesia

    Obat sipilis ampuh dari CV. Denature Indonesia merupakan salah satu produk herbal terpercaya untuk penyembuhan penyakit sipilis dengan cepat dan aman tanpa efek samping ataupun ketergantungan obat. Hal tersebut telah dibuktikan dengan terdaftarnya prod…

    ReplyDelete
  18. Souls in the Waves

    Excellent Morning, I just stopped in to visit your site and thought I'd say I loved myself.

    ReplyDelete
  19. Without a secure wireless network, a hacker can see what you're doing
    on your network and could install malicious software leaving you open to identity theft.
    It's not clear if that cheaper model will be available in the US though.
    4) Unlike WEP, WPA has a 48-bit IV, which enables nearly 500 trillion key combinations.

    ReplyDelete
  20. Really enjoyed this update, is there any way I can get an alert email every time you write a fresh post?

    ReplyDelete
  21. They can often offer many great advantages that some
    of the smaller vinyl banners cannot. Go through these
    fields and fill in what you need to create your banner category.
    In this menu, you will need to click the Banners link.

    ReplyDelete
  22. Hey guys !

    I hanker after to launch my blog : http://flyfire06.legtux.org
    This is a trifling news and tips blogs
    So lets obstruct him in ;)

    Set up a acute epoch :)

    ReplyDelete
  23. The Birch of the Shadow

    I believe there might become a several duplicates, but an exceedingly handy list! I've tweeted this. Quite a few thanks for sharing!

    ReplyDelete
  24. Thang May Kieng Can Thoi Trang In Phu Nu

    ReplyDelete
  25. Attractive element of content. I just stumbled upon your blog and in accession capital to claim that I acquire actually enjoyed account your blog posts.
    Any way I will be subscribing on your augment and even I achievement you get entry to persistently fast.

    ReplyDelete