Re: AW: document() and position()

Subject: Re: AW: document() and position()
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Sat, 22 Jul 2000 15:10:43 +0100
Dirk,

>If I do anything like this:
>
><xsl:template match="/">
>   <xsl:apply-templates select="document(/page/folder)/news[@show = 'true']"
>/>
></xsl:template>
>
><xsl:template match="news">
>   <xsl:variable name="index" select="position()" />
>   News item <xsl:value-of select="$index" />
>   ...
></xsl:template>
>
>I will allways get '1'. Because the 'news' tag is allways the number one
>position in the current xml file.

I'm very surprised that this is always generating '1'.  What XSLT processor
are you using?  When you use the position() function, it should look at the
position of the current node in the *current node list*, not in the
'current XML file'.  

Within the template, the current node is the node that's been matched - in
this case the 'news' element.  The current node list is set by the
xsl:apply-templates that was used to apply the template.  In this case, the
current node list consists of the nodes specified by the XPath:

  document(/page/folder)/news[@show = 'true']

In your example, this list has two nodes in it: the 'news' element from
2.xml and the 'news' element from 4.xml.  So the position() of the first
should be 1 and the position() of the second should be 2.

I have tested the following files in SAXON and it gives the result that I
think you are after.  Please let me know what the resulting output should
look like if I've misinterpreted you.

---- test.xml ----
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl" ?>
<page>
  <folder>1.xml</folder>
  <folder>2.xml</folder>
  <folder>3.xml</folder>
  <folder>4.xml</folder>
</page>
----
---- 1.xml ----
<news show="false">
  1.xml
</news>
----
---- 2.xml ----
<news show="true">
  2.xml
</news>
----
---- 3.xml ----
<news show="false">
  3.xml
</news>
----
---- 4.xml ----
<news show="true">
  4.xml
</news>
----
---- test.xsl ----
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:template match="/">
   <xsl:apply-templates select="document(/page/folder)/news[@show='true']" />
</xsl:template>

<xsl:template match="news">
  News item <xsl:value-of select="position()" />[<xsl:value-of
  select="normalize-space(.)" />]
</xsl:template>

</xsl:stylesheet>
----

Command line:  saxon -a -t -o out.xml test.xml

---- out.xml ----
<?xml version="1.0" encoding="utf-8" ?>
  News item 1 [2.xml]

  News item 2 [4.xml]
----

I hope this helps,

Jeni

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


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


Current Thread