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


Dirk,

>But now I need to know the number of the actual file I
>read in the template I call. I tried something like this:
>
><xsl:apply-templates select="document(/page/folder)/news[@show = 'true']">
>  <xsl:with-param name="index"><xsl:value-of select="position()"
>/></xsl:with-param>
></xsl:apply-templates>
>
>But the index parameter is allways '1'. How can I get
>the position? Or is there any other way to get the
>number of the actual xml file?

Within your xsl:apply-templates above, you set the value of the parameter
$index to the value of the position() of the current node within the
current node list.  The xsl:apply-templates doesn't do anything to change
the value of the current node, so if this is in a template like:

<xsl:template match="/">
  <xsl:apply-templates select="document(/page/folder)/news[@show = 'true']">
    <xsl:with-param name="index">
      <xsl:value-of select="position()" />
    </xsl:with-param>
  </xsl:apply-templates>
</xsl:template>

then the current node when setting the parameter value is still the root
node of the input document.  As there is only one of these, the value is
always 1.

I'm not exactly sure which position you are interested in: the index of the
file within the files that you're actually retrieving (i.e. only those for
whom @show = 'true') or the index of the file within all the files that you
could have retrieved.

If you want to find the position of the 'news' element amongst the other
'news' elements that you're actually retrieving, you want to use the
position() function when the current node is the 'news' element you're
interested in and the current node list is the list of the 'news' elements
that you've retrieved.  This is the situation within the 'news'-matching
templates that are applied using the xsl:apply-templates you've specified
above, so try something like:

<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>

If you want to find the position of the 'news' element amongst the 'news'
elements that you *could* have retrieved, you want to use the position()
function when the current node is the 'news' element you're interested in
and the current node list is the list of all the 'news' elements, whether
@show='true' or not.  In that case, try something like:

<xsl:template match="/">
  <xsl:apply-templates select="document(/page/folder)/news" />
</xsl:template>

<xsl:template match="news">
  <xsl:variable name="index" select="position()" />
  <xsl:if test="@show = 'true'">
    News item <xsl:value-of select="$index" />
    ...
  </xsl:if>
</xsl:template>

I hope that this 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]