This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: getting the node position in source xml in a variable
- From: Jeni Tennison <jeni at jenitennison dot com>
- To: Gurvinder Singh <GurvinderS at amdocs dot com>
- Cc: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Date: Wed, 27 Feb 2002 14:44:01 +0000
- Subject: Re: [xsl] getting the node position in source xml in a variable
- Organization: Jeni Tennison Consulting Ltd
- References: <D1713728C4550440A0E4A0816702E75C02E375C8@cypmail1srv.corp.amdocs.com>
- Reply-to: xsl-list at lists dot mulberrytech dot com
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