xsl-list
[Top] [All Lists]

Re: xpath dynamic selection

2004-07-11 07:06:35
Here is my uncutted code with the corrections from points 1-4. Altough I think 
there are still some point 3 errors it seems to do what I ask it to do for 
these parts. It is an extension for xsl-docbook

What I tried to do for the start parameter was first testing that it finds 
something with that construction. But it doesn't, so it won't work with more 
complex constructions.

My goal for the "start" parameter is giving it the value of the "step" 
parameter of matching "alt"-template.

5. The <alternative> elements in your example do not have number
attributes, and nor do the <alt> elements, so trying to select <alt>
elements whose number attributes have the same value as the number
attribute of the current <alternative> element is not going to work. I
still can't work out what you want here. If you want the value of the
<alt> element whose value is $number then that's the same as just
using $number. Are you sure that you don't just want:

  <xsl:param name="start" select="$number" />

If not, then can you try explaining how $start needs to be calculated,
and perhaps we can then help you put together the code you need to
calculate it.

Thanks,

Wim Lemkens

code:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version='1.0'>

<xsl:param name="local.l10n.xml" select="document('')"/>
<l:i18n xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0";>
  <l:l10n language="nl">
    <l:context name="title">
      <l:template name="usecase" text="Usecase %t"/>
      <l:template name="usecase/alternatecourses" text="Alternatieve Paden"/>
    </l:context>
  </l:l10n>
  <l:l10n language="en">
    <l:context name="title">
      <l:template name="usecase" text="Usecase %t"/>
      <l:template name="usecase/alternatecourses" text="Alternate Courses"/>
    </l:context>
  </l:l10n>
</l:i18n>

<xsl:template match="usecase">
  <xsl:variable name="pi-start">
    <xsl:call-template name="dbhtml-attribute">
      <xsl:with-param name="pis"
                      select="processing-instruction('dbhtml')"/>
      <xsl:with-param name="attribute" select="'start'"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="start">
    <xsl:choose>
      <xsl:when test="@continuation='continues'">
        <xsl:call-template name="usecase-starting-number"/>
      </xsl:when>
      <xsl:when test="$pi-start != ''">
        <xsl:value-of select="$pi-start"/>
      </xsl:when>
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="numeration">
    <xsl:call-template name="list.numeration"/>
  </xsl:variable>

  <xsl:variable name="type">
    <xsl:choose>
      <xsl:when test="$numeration='arabic'">1</xsl:when>
      <xsl:when test="$numeration='loweralpha'">a</xsl:when>
      <xsl:when test="$numeration='lowerroman'">i</xsl:when>
      <xsl:when test="$numeration='upperalpha'">A</xsl:when>
      <xsl:when test="$numeration='upperroman'">I</xsl:when>
      <!-- What!? This should never happen -->
      <xsl:otherwise>
        <xsl:message>
          <xsl:text>Unexpected numeration: </xsl:text>
          <xsl:value-of select="$numeration"/>
        </xsl:message>
        <xsl:value-of select="1"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <div class="{name(.)}">
    <xsl:call-template name="anchor"/>

    <xsl:if test="title">
     <xsl:call-template name="formal.object.heading"/>
    </xsl:if>
    <table class="usecase">
    <xsl:if test="description">
     <xsl:apply-templates select="description"/>
    </xsl:if>
  <tr><td>
    <ol>
      <xsl:if test="$start != '1'">
        <xsl:attribute name="start">
          <xsl:value-of select="$start"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="$numeration != ''">
        <xsl:attribute name="type">
          <xsl:value-of select="$type"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@spacing='compact'">
        <xsl:attribute name="compact">
          <xsl:value-of select="@spacing"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="*[(self::actor
                                   or self::system)]" mode="usecase"/>
    </ol>
   </td></tr>
  </table>
      <xsl:apply-templates select="alternativecourses"/>
  </div>
  
  
    <xsl:apply-templates select="*[not(self::actor
                                   or self::title
                                   or self::alternativecourses
                                   or self::description
                                   or self::system)]"/>
</xsl:template>

<xsl:template match="usecase/title">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="usecase/description">
  <tr class="description"><td>
    <xsl:apply-templates/>
  </td></tr>  
</xsl:template>

<xsl:template match="actor" mode="usecase">
  <li>
    <xsl:attribute name="number">
        <xsl:call-template name="usecase-step-number"/>
    </xsl:attribute>
    <xsl:variable name="step">
        <xsl:call-template name="usecase-step-number"/>
    </xsl:variable>

  <div class="actor">
        <xsl:apply-templates select="alt" mode="usecase">
          <xsl:with-param name="step" select="$step"/>
        </xsl:apply-templates>
        <xsl:apply-templates/>
  </div>
  </li>
</xsl:template>

<xsl:template match="system" mode="usecase">
  <li>
    <xsl:attribute name="number">
        <xsl:call-template name="usecase-step-number"/>
    </xsl:attribute>
    <xsl:variable name="step">
        <xsl:call-template name="usecase-step-number"/>
    </xsl:variable>
  <div class="system">
        <xsl:apply-templates select="alt" mode="usecase">
          <xsl:with-param name="step" select="$step"/>
        </xsl:apply-templates>
        <xsl:apply-templates/>
  </div>
  </li>
</xsl:template>

<xsl:template match="alternativecourses">
    <xsl:param name="node" select="."/>
     Alternatieve Paden
  
  <xsl:variable name="pi-start">
    <xsl:call-template name="dbhtml-attribute">
      <xsl:with-param name="pis"
                      select="processing-instruction('dbhtml')"/>
      <xsl:with-param name="attribute" select="'start'"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="start">
    <xsl:choose>
      <xsl:when test="@continuation='continues'">
        <xsl:call-template name="orderedlist-starting-number"/>
      </xsl:when>
      <xsl:when test="$pi-start != ''">
        <xsl:value-of select="$pi-start"/>
      </xsl:when>
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="numeration">
    <xsl:call-template name="list.numeration"/>
  </xsl:variable>

  <xsl:variable name="type">
    <xsl:choose>
      <xsl:when test="$numeration='arabic'">1</xsl:when>
      <xsl:when test="$numeration='loweralpha'">a</xsl:when>
      <xsl:when test="$numeration='lowerroman'">i</xsl:when>
      <xsl:when test="$numeration='upperalpha'">A</xsl:when>
      <xsl:when test="$numeration='upperroman'">I</xsl:when>
      <!-- What!? This should never happen -->
      <xsl:otherwise>
        <xsl:message>
          <xsl:text>Unexpected numeration: </xsl:text>
          <xsl:value-of select="$numeration"/>
        </xsl:message>
        <xsl:value-of select="1"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <div class="{name(.)}">
    <xsl:call-template name="anchor"/>

    <xsl:if test="title">
     <xsl:call-template name="formal.object.heading"/>
    </xsl:if>
    <table class="usecase">
    <xsl:if test="description">
     <xsl:apply-templates select="description"/>
    </xsl:if>
<tr><td>
    <ol>
      <xsl:if test="$start != '1'">
        <xsl:attribute name="start">
          <xsl:value-of select="$start"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="$numeration != ''">
        <xsl:attribute name="type">
          <xsl:value-of select="$type"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@spacing='compact'">
        <xsl:attribute name="compact">
          <xsl:value-of select="@spacing"/>
        </xsl:attribute>
      </xsl:if>
        <xsl:apply-templates select="alternative">
        </xsl:apply-templates>

    </ol>
   </td></tr>
  </table>
  </div>
  
  
</xsl:template>


<!-- alternative in the usecase itself -->
<xsl:template match="alt" mode="usecase">
    <xsl:param name="step"/>
    
    <xsl:param name="number">
      <xsl:apply-templates/>
    </xsl:param>
number : <xsl:copy-of select="$number"/>
step : <xsl:copy-of select="$step"/>

</xsl:template>


<xsl:template match="alternativecourses/alternative/actor">
  <li>
    <xsl:attribute name="number">
        <xsl:call-template name="alternative-step-number"/>
    </xsl:attribute>
  <div class="actor">
        <xsl:apply-templates/>
  </div>
  </li>
</xsl:template>

<xsl:template match="alternativecourses/alternative/system">
  <li>
    <xsl:attribute name="number">
        <xsl:call-template name="alternative-step-number"/>
    </xsl:attribute>
  <div class="system">
        <xsl:apply-templates/>
  </div>
  </li>
</xsl:template>

<xsl:template match="alternative">
    <xsl:param name="node" select="."/>
    <xsl:param name="number">
      <xsl:call-template name="alternative-number"/>
    </xsl:param>
        <!-- get the startignnmber   THIS IS WHERE THE ERROR IS!! -->
    <xsl:param name="start">
        <xsl:value-of select="ancestor::usecase/*/alt[(_at_)number = 
current()/@number]/attribute::step"/>
    </xsl:param>    
    anumber : <xsl:copy-of select="$number"/>
    step : <xsl:copy-of select="$start"/>
        <!-- generate the new list -->
  <xsl:variable name="pi-start">
    <xsl:call-template name="dbhtml-attribute">
      <xsl:with-param name="pis"
                      select="processing-instruction('dbhtml')"/>
      <xsl:with-param name="attribute" select="'start'"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="numeration">
    <xsl:call-template name="list.numeration"/>
  </xsl:variable>

  <xsl:variable name="type">
    <xsl:choose>
      <xsl:when test="$numeration='arabic'">1</xsl:when>
      <xsl:when test="$numeration='loweralpha'">a</xsl:when>
      <xsl:when test="$numeration='lowerroman'">i</xsl:when>
      <xsl:when test="$numeration='upperalpha'">A</xsl:when>
      <xsl:when test="$numeration='upperroman'">I</xsl:when>
      <!-- What!? This should never happen -->
      <xsl:otherwise>
        <xsl:message>
          <xsl:text>Unexpected numeration: </xsl:text>
          <xsl:value-of select="$numeration"/>
        </xsl:message>
        <xsl:value-of select="1"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <div class="{name(.)}">
    <xsl:call-template name="anchor"/>

  <tr><td>
    <ol>
      <xsl:if test="$start != '1'">
        <xsl:attribute name="start">
          <xsl:value-of select="$start"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="$numeration != ''">
        <xsl:attribute name="type">
          <xsl:value-of select="$type"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@spacing='compact'">
        <xsl:attribute name="compact">
          <xsl:value-of select="@spacing"/>
        </xsl:attribute>
      </xsl:if>
    <xsl:apply-templates select="*[(self::actor
                                   or self::system)]"/>
    </ol>
   </td></tr>
  </div>
      <xsl:apply-templates select="*[not(self::actor
                                   or self::title
                                   or self::description
                                   or self::system)]"/>
</xsl:template>


<xsl:template name="usecase-step-number">
  <!-- context node must be a system or actor in a usecase -->
  <xsl:param name="node" select="."/>

  <xsl:choose>
    <xsl:when test="$node/@override">
      <xsl:value-of select="$node/@override"/>
    </xsl:when>
    <xsl:when test="$node/preceding-sibling::*[(self::system or 
self::actor)]">
      <xsl:variable name="pnum">
        <xsl:call-template name="usecase-step-number">
          <xsl:with-param name="node" 
select="$node/preceding-sibling::*[(self::system or self::actor)][1]"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:value-of select="$pnum + 1"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="usecase-starting-number">
        <xsl:with-param name="list" select="parent::*"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="usecase-starting-number">
  <xsl:param name="list" select="."/>
  <xsl:choose>
    <xsl:when test="not($list/@continuation = 'continues')">1</xsl:when>
    <xsl:otherwise>
      <xsl:variable name="prevlist"
                    select="$list/preceding::usecase[1]"/>
      <xsl:choose>
        <xsl:when test="count($prevlist) = 0">2</xsl:when>
        <xsl:otherwise>
          <xsl:variable name="prevlength" select="count($prevlist/listitem)"/>
          <xsl:variable name="prevstart">
            <xsl:call-template name="usecase-starting-number">
              <xsl:with-param name="list" select="$prevlist"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:value-of select="$prevstart + $prevlength"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="alternative-number">
  <xsl:param name="node" select="."/>

  <xsl:choose>
    <xsl:when test="$node/@override">
      <xsl:value-of select="$node/@override"/>
    </xsl:when>
    <xsl:when test="$node/preceding-sibling::alternative">
      <xsl:variable name="pnum">
        <xsl:call-template name="alternative-number">
          <xsl:with-param name="node" 
select="$node/preceding-sibling::alternative[1]"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:value-of select="$pnum + 1"/>
    </xsl:when>
    <xsl:otherwise>
      1
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="alternative-step-number">
  <!-- context node must be a listitem in an orderedlist -->
  <xsl:param name="node" select="."/>

  <xsl:choose>
    <xsl:when test="$node/@override">
      <xsl:value-of select="$node/@override"/>
    </xsl:when>
    <xsl:when test="$node/preceding-sibling::*[(self::system or 
self::actor)]">
      <xsl:variable name="pnum">
        <xsl:call-template name="usecase-step-number">
          <xsl:with-param name="node" 
select="$node/preceding-sibling::*[(self::system or self::actor)][1]"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:value-of select="$pnum + 1"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="usecase-starting-number">
        <xsl:with-param name="list" select="parent::*"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>


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