[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Returning the file name of the input file


Subject: Re: [xsl] Returning the file name of the input file
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 28 Aug 2002 12:37:51 +0100

Hi Ismael,

> I have an xml file which refers to other xml files that can refer to other
> xml files, etc.
>
> E.g.
>
>         <package type="expr" href="../../package1.xml"/>
>         <package href="../../package2.xml"/>
>         <package type="expr" href="../../package3.xml"/>
>
> Out of this xml file I generate a list of all references of type
> 'expr'. So I search through all xml files and generate a list of all
> references of type 'expr'. The result file is used by a Java program
> to execute another process. The problem is because relative paths
> are used the Java program can't find the files (the Java program is
> started from another place). Therefore I would like to translate the
> relative paths into absolute paths. But I don't find any function in
> xsl to do this, nor do I see a solution to solve this with xsl.

You're correct that there isn't a function in XPath/XSLT to do this
(at least not in this version; in XPath 2.0 there's a base-uri()
function which can provide you with the base URI of a node, and a
resolve-uri() function which takes a base URI and a relative URI and
resolves them into a single URI, exactly what you need :).

In the meantime, you can pass the base URI of the initial file that
contains the references to other files into the stylesheet as a
parameter, and when you traverse to other XML files (containing their
own pointers) you can update the parameter with a new base URI. You
can construct the URIs by stringing together the substring up to and
including the last / with the relative URI that you've got, so you get
paths like:

  /some/absolute/path/../../relative/path/../../another/relative/path

and so on. I believe that these URLs work despite looking weird (I
remember doing this before and it working, but I'm not 100% sure). If
they don't, then you can create a "resolve-uri" template to combine a
base and relative URI.

The string processing is a bit tedious, but not too bad. For example,
to get the substring up to and including the last / (which is the
fundamental thing you need to do for all this URI processing) you can
use:

<xsl:template name="get-directory">
  <xsl:param name="uri" />
  <xsl:if test="contains($uri, '/')">
    <xsl:value-of select="concat(substring-before($uri, '/'), '/')" />
    <xsl:call-template name="get-directory">
      <xsl:with-param name="uri"
                      select="substring-after($uri, '/')" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

If you need more help, ask away.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread
Keywords