xsl-list
[Top] [All Lists]

Re: [xsl] node depth

2007-09-13 10:16:19
Hey Bernie, I'm a little late to the response on this one - thought I'd
put in my two cents.

Remember that a variable can capture any output data that would usually
be sent to the result document... including output from internal XSLT
elements. You can get a good variety of count and depth combinations
using the <xsl:number/> element; if you put <xsl:number/> in a
<xsl:variable>, the string value of that number will be available for
you to use in the rest of your template.

~ Scott



Date: Fri, 7 Sep 2007 14:04:00 +0200
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
From: Bernie <berniecc(_at_)gmail(_dot_)com>
Subject: Re: [xsl] node depth
Message-ID:
<271da0ec0709070504n2ecbcfadmd73ac9c7314aef7e(_at_)mail(_dot_)gmail(_dot_)com>

Thanx Abel, I'm a XSL newbie learning.

On 9/7/07, Abel Braaksma <abel(_dot_)online(_at_)xs4all(_dot_)nl> wrote:
Bernie wrote:
Is there any way to find out the depth of the current element in a
recursive template? The XML is the following:


The depth of any node in the current tree can be requested with:
count(ancestor-or-self::*)

Bonus: your stylesheet can be written a bit easier as follows:

<xsl:template match="section">
        <li>
            <xsl:apply-templates select="label" />
        </li>
        <ul>
            <xsl:apply-templates select="section" />
        </ul>
  </xsl:template>

  <xsl:template match="label">
     <xsl:apply-templates select="@href[(_at_)href != '']" />
     <xsl:value-of select=".[not(@href) | normalize-space(@href) =
'']" />
  </xsl:template>

   <xsl:template match="label/@href">
      <a href="{.}"><xsl:value-of select="label" /></a>
   </xsl:template>



As a general hint, whenever you are writing an xsl:choose or an xsl:if
with a node test, consider rewriting it as a matching
template/applying
templates pair. This will undoubtedly make your code more adept to
change, efficient and easier to understand in the long run. A test for
a
label being empty and then outputting its value is unnecessary. It may
happen that an empty <a></a> occurs but that still renders the same,
if
you don't like that you can remove it by changing the matching
template to:

   <xsl:template match="label[.!='']/@href">
      <a href="{.}"><xsl:value-of select="label" /></a>
   </xsl:template>

Cheers,

Abel Braaksma



<xsl:template match="section">
    <xsl:if test="label!=''">
      <li>
        <xsl:choose>
          <xsl:when test="label/@href!=''">
            <a>
                              <xsl:attribute name="href">
                                      <xsl:value-of
select="label/@href" />
                              </xsl:attribute>
                                <xsl:value-of select="label" />
                        </a>
          </xsl:when>
          <xsl:otherwise>
                        <xsl:value-of select="label" />
          </xsl:otherwise>
        </xsl:choose>
      </li>
      <ul>
              <xsl:apply-templates select="section" />
      </ul>
    </xsl:if>
  </xsl:template>
-----------------------------

Bernie.


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






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



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