Re: Stylesheet assistance

Subject: Re: Stylesheet assistance
From: "Nikolai Grigoriev" <grig@xxxxxxxxxxx>
Date: Wed, 26 Jul 2000 01:11:03 +0400
Daniel,

> Any suggestions on how do do the following?

I would suggest passing indentation as a parameter:

<xsl:template match="a1|a2|a3|a4|a5|a6">
  <xsl:param name="indentation" select="0"/>
  <p>

    <!-- If an indentation is greater than 0, create a style attribute -->
    <xsl:if test="$indentation &gt; 0">
      <xsl:attribute name="style">
        <xsl:text>margin-left: </xsl:text>
        <xsl:value-of select="$indentation"/>
        <xsl:text>px</xsl:text>
      <xsl:attribute>
    </xsl:if>

    <!-- If an indentation is greater than 0 or <b/> is present, -->
    <!-- increase the indentation of children by 12-->
    <xsl:choose>
      <xsl:when test="b or $indentation &gt; 0">
        <xsl:apply-templates>
          <xsl:with-param name="indentation" select="$indentation+12"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise><xsl:apply-templates/></xsl:otherwise>
    </xsl:choose>

  </p>
</xsl:template>

You can also solve this without parameters, but then you will need to calculate
the offset individually for every <a#> element, climbing up the document tree by
a dedicated recursive template:

<xsl:template match="a1|a2|a3|a4|a5|a6">
  <p>

    <!-- If an indentation trigger is set, create a style attribute -->
    <xsl:if test="ancestor::*/b">
      <xsl:attribute name="style">
        <xsl:text>margin-left: </xsl:text>
        <xsl:apply-templates select="." mode="count-indentation"/>
        <xsl:text>px</xsl:text>
      <xsl:attribute>
    </xsl:if>

    <xsl:apply-templates/>
  </p>
</xsl:template>


<!-- Calculate the offset: on each step up the doctree, -->
<!-- add 12 to indentation; stop when <b/> is found. -->
<xsl:template match="*" mode="count-indentation"/>
  <xsl:param name="indentation" select="0"/>
  <xsl:choose>
    <xsl:when test="b"><xsl:value-of select="$indentation"</xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select=".." mode="count-indentation">
        <xsl:with-param name="indentation" select="$indentation+12"/>
      </xsl:apply-templates>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Seemingly, the first solution would generate less overhead than the second
one. Note also that they behave differently when several <b/>'s are
present on ancestors of an element (situation not excluded by your DTD):

- the first thing will indent from the topmost <b/> ignoring lower <b/>'s;
- the second one will reset indents to 0 on every <b/>.

Regards,
Nikolai Grigoriev
RenderX





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


Current Thread