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: position()


Steve,

>I guess I don't understand the "position" function.  I am trying to see 
>if the node I'm processing is the last one, but it never works:
>
><xsl:template match="a/b">
><xsl:if test="position(..)=last(../../a)">&nbsp;&nbsp;&nbsp;&nbsp;
></xsl:if>
>xsl:apply-templates/> </xsl:template>

position() and last() never take any arguments.

position() gives you the position of the current node in the current node
list.  last() gives you the number of nodes in the current node list.
Thus, "position() = last()" returns true if the current node is the last
node in the current node list.

The current node is the one you're currently processing.  In your case,
it's a 'b' element that is a child of an 'a' element.

The current node list is the list of nodes that are being processed under
the same instruction.  The 'a/b'-matching template is being applied because
somewhere up the line there's been an xsl:apply-templates instruction that
has selected some 'b' elements to be processed.  You need to look at that
xsl:apply-templates instruction to see what the current node list is.

If there isn't an explicit xsl:apply-templates instruction that would
select the 'b' node, then it will be in a node list made up of itself and
its siblings.  This is because the built-in template applies templates to
all the children of a particular node.

The most likely case is that the 'b' element is being processed along with
all its siblings.  So, if you do:

<xsl:template match="a/b">
  <xsl:if test="position() = last()">&nbsp;&nbsp;&nbsp;&nbsp;</xsl:if>
</xsl:template>

then the non-breaking spaces will only be added when the 'b' element is the
last child of its parent (the 'a' element).

If the judgement about whether the 'b' element is "last" is more complex
than this (e.g. the last 'b' element in the document), then you might need
something more complicated.  Let me know.

I hope that helps,

Jeni

Dr Jeni Tennison
Epistemics Ltd * Strelley Hall * Nottingham * NG8 6PE
tel: 0115 906 1301 * fax: 0115 906 1304 * email: jeni.tennison@epistemics.co.uk


 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]