Don’t call me DOM

16 December 2004

Creating nice relative URIs with XSLT

Filed under:

I needed today a way to create nice relative URI paths in XSLT. The point was, given a relative URI A foo/bar/toto, and another relative URI B baz/tutu supposed to be relative to the same base, calculate the URI path from A to B, in this case ../../baz/tutu.

Although it isn’t very complicated, I figured that other people may need this very same facility, since it’s a classical problem when using XSLT e.g. to build a Web site. Plus, I tried to make sure it would generate “nice” paths, for instance avoiding that the path generated to go from foo/bar/toto to foo/baz be ../../foo/baz instead of the simpler ../baz.

So I published the code as a template in a separate XSLT style sheet, and added it to my XSLT Toolbox.

The code comes with an integrated Unit Test, handy for completing/adapting the code.

Picture of Dominique Hazael-MassieuxDominique Hazaël-Massieux (dom@w3.org) is part of the World Wide Web Consortium (W3C) Staff; his interests cover a number of Web technologies, as well as the usage of open source software in a distributed work environment.