Re: [xsl] getting the node position in source xml in a variable

Subject: Re: [xsl] getting the node position in source xml in a variable
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 27 Feb 2002 14:44:01 +0000
Hi Gurvinder,

> Yes the structure is always like this (i.e data and display nodes
> are always siblings) but the xpath can change (different names of
> tags..).. we want to keep it at one place so that if we want to
> change something we have to change only at one place..
>
> For example, are the display nodes and associated data nodes
> always siblings of each other, nested under some other node? Very Correct..

In that case, there are a couple of other models that you could try.

First, you could pass the set of nodes that are the parents of the
data and display nodes in as a parameter, with two separate
parameters, $dataName and $displayName holding the names of the
elements that you want to sort. The template would be:

<xsl:template name="lookup">
  <xsl:param name="name" />
  <xsl:param name="nodes" select="/.." />
  <xsl:param name="dataName" select="'data'" />
  <xsl:param name="displayName" select="'display'" />
  <select name="{$name}">
    <xsl:for-each select="$nodes">
      <xsl:sort select="*[name() = $displayName]" />
      <xsl:variable name="data" select="*[name() = $dataName]" />
      <option id="{$data}" value="{$data}">
        <xsl:value-of select="concat($data, '-',
                                     *[name() = $displayName]" />
      </option>
    </xsl:for-each>
  </select>
</xsl:template>

You could call this with:

  <xsl:call-template name="lookup">
    <xsl:with-param name="name" select="'test'" />
    <xsl:with-param name="nodes" select="/test/Level2" />
  </xsl:call-template>

[Here assuming that the elements holding the data are called 'data'
and the elements holding the display are called 'display'.]
  
A second option is to pass in the displayNodes as a parameter, and a
string giving the name of the element that holds the data. That's even
simpler:

<xsl:template name="lookup">
  <xsl:param name="name" />
  <xsl:param name="displayNodes" select="/.." />
  <xsl:param name="dataName" select="'data'" />
  <select name="{$name}">
    <xsl:for-each select="$displayNodes">
      <xsl:sort select="." />
      <xsl:variable name="data" select="../*[name() = $dataName]" />
      <option id="{$data}" value="{$data}">
        <xsl:value-of select="concat($data, '-', .)" />
      </option>
    </xsl:for-each>
  </select>
</xsl:template>

You could call this with:

  <xsl:call-template name="lookup">
    <xsl:with-param name="name" select="'test'" />
    <xsl:with-param name="displayNodes" select="/test/Level2/display" />
  </xsl:call-template>

[Again assuming that the element holding the data are called 'data'.]

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


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


Current Thread