This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Understanding difficulties with call-template


Robert,

>OK got it working here this works.
>
><xsl:template name="comma-block">
>        <xsl:param name="nodes"/>
>        <xsl:for-each select="$nodes">
>                <xsl:value-of select="."/>
>                <xsl:if test="position() != last()">, </xsl:if>
>        </xsl:for-each>
></xsl:template>
>
><xsl:template match="preferred-locations">
><xsl:element name="p">
>Preferred Locations:
><xsl:call-template name="comma-block">
>        <xsl:with-param name="nodes" select="preferred-location"/>
></xsl:call-template>
></xsl:element>
></xsl:template>

Just an observation: when I find myself passing nodes or node lists into
named templates, I usually change them into moded templates because then I
don't have to fiddle around with passing and accepting parameters all the
time.  So the above can be changed to:

<xsl:template match="preferred-location" mode="comma-block">
  <xsl:value-of select="." />
  <xsl:if test="position() != last()">, </xsl:if>
</xsl:template>

<xsl:template match="preferred-locations">
  <p>
    Preferred Locations:
    <xsl:apply-templates select="preferred-location" mode="comma-block" />
  </p>
</xsl:template>

The current node list (which, as Mike Brown explained, you need to know
about to do tests on a node's position within it) is defined within the
xsl:apply-templates in the second template.  Templates are being applied to
all preferred-location children of preferred-locations, and the list of
those children becomes the node list within the first template.

There is another alternative, namely to test whether the preferred-location
has any following-sibling preferred-location (in other words, if it's the
last in the list).  This means you don't have to worry about what the
current node list is at all, because you are using information straight
from the DOM.  This has the advantage that you can use the same named
template for different elements that you want to list in a similar way:

<xsl:template name="comma-block">
  <xsl:for-each select="*">
    <xsl:value-of select="." />
    <xsl:if test="not(following-sibling::*)">, </xsl:if>
  </xsl:for-each>
</xsl:template>

<xsl:template match="preferred-locations">
  <p>
    Preferred Locations:
    <xsl:call-template name="comma-block" />
  </p>
</xsl:template>

<xsl:template match="disliked-locations">
  <p>
    Disliked Locations:
    <xsl:call-template name="comma-block" />
  </p>
</xsl:template>

I probably favour the first alternative because I don't like the difficulty
of checking what the current node is within the named template above: with
moded templates, it's easy to tell what the current node is.

Cheers,

Jeni

Jeni Tennison
http://www.jenitennison.com/


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]