xsl-list
[Top] [All Lists]

RE: [xsl] remove relative path

2006-05-17 07:42:15
Thanks Jon, I have this working now, thanks for your pointers.

Gav...

-----Original Message-----
From: Jon Gorman [mailto:jonathan(_dot_)gorman(_at_)gmail(_dot_)com]
Sent: Wednesday, 17 May 2006 8:30 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] remove relative path

On 5/16/06, Gav.... <brightoncomputers(_at_)brightontown(_dot_)com(_dot_)au> 
wrote:
Hi All,

I have looked through the list, archives etc and come up some code based
on
what I summized from it, but of course it does not work.

For an OS project, I'm trying to remove all ../ from the supplied @src
path.
Obviously they can be nested values too such as  ../../../

So I need to remove all these leaving just the filename. (I can then
prepend
A predetermined path onto it.)

I have a template :-

<xsl:template name="removedotdots">
          <xsl:param name="path"/>
          <xsl:variable name="removedirs"
select="substring-after(string($path),'../')"/>
          <xsl:variable name="removeagain"
select="starts-with(string($removedirs),'../')"/>
          <xsl:if test="$removeagain">
                  <xsl:call-template name="removedotdots">
                          <xsl:with-param name="path"
select="substring-after(string($removedirs),'../')"/>
                  </xsl:call-template>
          </xsl:if>
          </xsl:template>

But what if it's five levels?  Fifteen?  Why do this approach?
Recursion is your friend with XSLT.

XSLT 2.0 has better ways, and probably so does XSLT 1.0.  But right
off the top of my head even without thinking, I can come up with a
recursive way:

Something like:


<xsl:template match="path">

<filename>
<xsl:call-template name="getFilename">
<xsl:with-param name="path_string" select="." />
</xsl:call-template>

</filename>
</xsl:template>

<xsl:template name="getFilename">
<xsl:param name="path_string" />
<xsl:choose>
<xsl:when test="starts-with($path_string,'../')">
      <xsl:call-template name="getFilename">
      <xsl:with-param name="path_string"
select="substring-after($path_string,'../')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="starts-with($path_string,'/../')">
<xsl:call-template name="getFilename">
<xsl:with-param name="path_string"
select="substring-after($path_string,'/../')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$path_string"/>
</xsl:otherwise>
</xsl:choose>

</xsl:template>

This assuming the path is an element in path, but it should be
straightforward enough to figure out.

There's probably lots of other ways to do this.


Jon Gorman

--~------------------------------------------------------------------
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>
--~--




--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.392 / Virus Database: 268.6.0/341 - Release Date: 16/05/2006



--~------------------------------------------------------------------
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>
--~--

<Prev in Thread] Current Thread [Next in Thread>