Re: [xsl] relative path from one node to another (XSLT 2.0 solution)

Subject: Re: [xsl] relative path from one node to another (XSLT 2.0 solution)
From: "Richard Lewis" <richardlewis@xxxxxxxxxxxxxx>
Date: Fri, 20 May 2005 11:44:04 +0100
On Fri, 20 May 2005 09:19:52 +0100, "Richard Lewis"
<richardlewis@xxxxxxxxxxxxxx> said:
> I wonder if you'd had any thoughts on the problem I described of
> implementing a mechanism for linking from one output document to another
> using a
> <link section="tom">See Tom's page</link>
> element in the source tree?
> 
> Such an element should transformed to an HTML <a> tag with the correct
> 'href' attribute to link from the output document in which it occurs to
> the output document for the section with id 'tom'.
> 
OK, I've managed to work out a solution:

First thing I did was to give the document (root) node an id attribute
(id="ROOT") to make it behave a bit more like a section (polymorphism!).
Then I made a named template which takes the id values of two sections
(one may be 'ROOT') and returns the file system path between them:

<xsl:template name="relative-fs-path">
  <xsl:param name="from-id" />
  <xsl:param name="to-id" />

  <xsl:for-each select="//*[@id=$from-id]/ancestor::*[(name()='section'
  or name()='document') and (@display='page')]">
    <xsl:if test="@id!='ROOT'">../</xsl:if>
  </xsl:for-each>
  <xsl:for-each
  select="//*[@id=$to-id]/ancestor-or-self::*[(name()='section' or
  name()='document') and (@display='page')]">
    <xsl:if test="@id!='ROOT' and position()!=last()"><xsl:value-of
    select="@id" />/</xsl:if>
  </xsl:for-each>
</xsl:template>

This generates non-relative paths; e.g. the path from tom.html to
harry.html (see beginning of thread) instead of being simply
"harry.html" is "../../staff/permanent/harry.html".

I use this for getting the file names for xsl:result-document, for
getting the correct path to link from section to another and for the
paths to the images directories and from the site's main menu to the
section documents.

I don't expect this is an ideal solution, but it works. Any comments
would be welcome!

Cheers,
Richard

Current Thread