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

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

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

<top>
 <nums/>
 <nums>
   <num>01</num>
   <num>02</num>
   <num>03</num>
   <num>04</num>
   <num>05</num>
   <num>06</num>
   <num>07</num>
   <num>08</num>
   <num>09</num>
   <num>10</num>
 </nums>
</top>

the result is:

<top>
   <nums/>
   <nums>
      <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>
   </nums>
</top>


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
node-list).

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


-- 
Cheers,
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
Keywords
xml