[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Re: Position() in apply-templates

Subject: Re: [xsl] Re: Position() in apply-templates
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Tue, 1 Apr 2014 19:02:26 -0700

On Tue, Apr 1, 2014 at 2:47 PM, David Rudel <fwqhgads@xxxxxxxxx> wrote:
> A. No
> B. No
> C. Yes

This is correct.

Here is an example:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
      <xsl:apply-templates select="node()|@*"/>
  <xsl:template match="nums">
      <xsl:apply-templates select="num">
        <xsl:sort select="10 -position()"/>
        <xsl:with-param name="pPos" select="position()"/>

  <xsl:template match="num">
    <xsl:param name="pPos"/>
    <num paramPos="{$pPos}"><xsl:value-of select="."/></num>

When this transformation is applied on the following source XML document:


the result is:

      <num paramPos="2">10</num>
      <num paramPos="2">09</num>
      <num paramPos="2">08</num>
      <num paramPos="2">07</num>
      <num paramPos="2">06</num>
      <num paramPos="2">05</num>
      <num paramPos="2">04</num>
      <num paramPos="2">03</num>
      <num paramPos="2">02</num>
      <num paramPos="2">01</num>

That is, for B), position() is the position of the context node (in
this case the 2-nd node in the previous node-list).

Do note that if I remove the declaration:

<xsl:strip-space elements="*"/>

all "paramPos" attributes in the result will have value 4, because the
two intermediate text nodes will also be in the node-list of the
<xsl:apply-templates> instruction (the one from the identity rule) and
the cotext item (the 2nd "nums" element is the 4th in a 5-node

So, the answer about B) is that its position() is "the number assigned
to that node" but from the outer <xsl:apply-templates>  :)

Dimitre Novatchev
Truly great madness cannot be achieved without significant intelligence.
To invent, you need a good imagination and a pile of junk
Never fight an inanimate object
To avoid situations in which you might make mistakes may be the
biggest mistake of all
Quality means doing it right when no one is looking.
You've achieved success in your field when you don't know whether what
you're doing is work or play
To achieve the impossible dream, try going to sleep.
Facts do not cease to exist because they are ignored.
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
I finally figured out the only reason to be alive is to enjoy it.

Current Thread