xsl-list
[Top] [All Lists]

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

2005-05-20 03:44:04

On Fri, 20 May 2005 09:19:52 +0100, "Richard Lewis"
<richardlewis(_at_)fastmail(_dot_)co(_dot_)uk> 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="//*[(_at_)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="//*[(_at_)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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--