Re: [xsl] Unexpected result from <a href="...">...</a> in some cases

Subject: Re: [xsl] Unexpected result from <a href="...">...</a> in some cases
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 19 Aug 2008 20:50:20 -0400
At 2008-08-19 15:05 -0600, Quinn Taylor wrote:
Allow me to clarify...

Thanks for doing so.

What result to you expect for your test string "/path/to/resource"?

<a href="../../../">[root]</a> / <a href="../../">path</a> / <a href="../">to</a> / resource

What result to you expect for "/to/resource"?

<a href="../../">[root]</a> / <a href="../">to</a> / resource


What result to you expect for "/resource"?

<a href="../">[root]</a> / resource

Got it.


If you run my code with the commented code un-commented, you can
actually see that $parentPath has the value I want, it just isn't
printing out what I need.

But I couldn't understand the output that it gives:


<a href="../../../">[root]</a> / path&lt;../../path&gt; / to&lt;../to&gt; / resource

... which is certainly different than your clarification, so I appreciate you took the time to clarify.

I also got lost in your code with the definition of the variable "recursiveResult" and pulling it apart again ... I couldn't see why the variable would be necessary and not just generate each step as required.

Below is what I think is a complete result, without the need for such a variable. I've tried to document it to explain the approach.

I hope this helps.

. . . . . . . . . . . Ken


t:\ftemp>type quinn.xsl <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0"> <xsl:output method="html" indent="yes"/>

  <xsl:template match="*">
    <xsl:call-template name="printRecursivePath">
     <xsl:with-param name="text">/path/to/resource</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="printRecursivePath">
     <xsl:with-param name="text">/to/resource</xsl:with-param>
    </xsl:call-template>
    <xsl:call-template name="printRecursivePath">
     <xsl:with-param name="text">/resource</xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <!-- Split path components at '/', add parent directory links -->
  <xsl:template name="printRecursivePath">
    <!--$path rebuilds the input $text one step at a time-->
    <xsl:param name="path" select="''"/>
    <!--$parent builds "../" for each parent in the $text-->
    <xsl:param name="parent" select="'../'"/>
    <!--this starts as original and pares down each step-->
    <xsl:param name="text"/>
    <!--this is the next branch of the text-->
    <xsl:variable name="head" select="substring-before($text,'/')"/>
    <!--this is what is left after the next branch of the text-->
    <xsl:variable name="tail" select="substring-after($text,'/')"/>
    <xsl:choose>
      <xsl:when test="not(contains($tail,'/'))">
        <!--at the last portion of the text, put out link-->
        <a href="{$parent}">
          <xsl:choose>
            <xsl:when test="$parent='../' and $head">
              <!--must be at penultimate step-->
              <xsl:value-of select="$head"/>
            </xsl:when>
            <xsl:when test="not(substring-before($path,'/'))">
              <!--must be at the very start-->
              <xsl:text>[root]</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <!--otherwise must be in the middle-->
              <xsl:value-of select="substring-before($path,'/')"/>
            </xsl:otherwise>
          </xsl:choose>
        </a>
        <!--with link done, redo logic ignoring first step in path-->
        <xsl:text> / </xsl:text>
        <xsl:choose>
          <xsl:when test="$path">
            <!--next step still has path components, so repeat all
                this logic as if the first step isn't there-->
            <xsl:call-template name="printRecursivePath">
              <xsl:with-param name="text"
                select="concat(substring-after($path,'/'),$head,'/',$tail)"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <!--the last step does not have path components-->
            <b>
              <xsl:value-of select="$tail"/>
            </b>
            <!--some white space to separate results-->
            <xsl:text>

</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <!--still building the parent string for the current $text-->
        <xsl:call-template name="printRecursivePath">
          <xsl:with-param name="parent" select="concat($parent,'../')"/>
          <xsl:with-param name="path" select="concat($path,$head,'/')"/>
          <xsl:with-param name="text" select="$tail"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
t:\ftemp>call xslt quinn.xsl quinn.xsl quinn.html

t:\ftemp>type quinn.html
<a href="../../../">[root]</a> / <a href="../../">path</a> / <a href="../">to</a> / <b>resource</b>


<a href="../../">[root]</a> / <a href="../">to</a> / <b>resource</b>

<a href="../">[root]</a> / <b>resource</b>


t:\ftemp>rem Done!




--
Upcoming XSLT/XSL-FO hands-on courses:      Wellington, NZ 2009-01
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread