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: Gurvinder Singh <GurvinderS@xxxxxxxxxx>
Date: Wed, 27 Feb 2002 14:18:52 +0200
hi
It didnt work..
I dont think that when i convert using msxml:node-set i get a single root
node...
because i was able to loop thru all the nodes.

when i use the following template it works fine....

<xsl:template name="lookup">
	<xsl:param name="name"/>
	<xsl:param name="datanodes"/>
	<xsl:param name="displaynodes"/>
	<select>
		<xsl:attribute name="name"><xsl:value-of
select="$name"/></xsl:attribute>
		<xsl:for-each select="msxsl:node-set($displaynodes)">
			<xsl:variable name="num" select="position()"/>
			<xsl:variable name="data"><xsl:value-of
select="msxsl:node-set($datanodes)[$num]"/></xsl:variable>
			<option id="{$data}" value="{$data}">
				<xsl:value-of select="$data"/>-<xsl:value-of
select="."/>
			</option>
		</xsl:for-each>
	</select>
</xsl:template>

BUT if i put a sort into the for-each then also it gives out all the values
but only the displaynodes are sorted and the datanodes are not sorted
so..they make wrong combinations...

for example 

if i pass this as datanodes
<data>3</data>
<data>2</data>
<data>1</data>

and
<display>C</display>
<display>B</display>
<display>A</display>


with out sort i get
3-C
2-B
1-A
with sort i get
3-A
2-B
1-C
Thanks & Regards

Gurvinder
Amdocs Limited , Cyprus


-----Original Message-----
From: Jeni Tennison [mailto:jeni@xxxxxxxxxxxxxxxx]
Sent: Wednesday, February 27, 2002 1:37
To: Gurvinder Singh
Cc: 'xsl-list@xxxxxxxxxxxxxxxxxxxxxx'
Subject: Re: [xsl] getting the node position in source xml in a variable


Hi Gurvinder,

> I have a template which takes two node-sets as parameters. I loop
> thru one of these nodeset using for-each and output the node value
> along with the corresponding node in the second node-list. Now the
> problem is that it works fine if i dont have a sort but with sort
> only the first node list is sorted. So it takes the wrong
> corresponding values from the second node-list. because the position
> gives the position in the context. So i should use <xsl:number> but
> when i put the xsl:number in the variable it always gives 1. even
> the position() if inside <xsl:variable> gives always 1, but if used
> in select attribute of <xsl:variable> it works fine

I think that the problem is that when you turn a result tree fragment
into a node set (as you're doing with the msxsl:node-set() extension
function), you get back a node set containing a single root node. In
your template, you seem to be imagining that you're getting back the
children of this root node (the elements or whatever).

Try this slightly amended version:

<xsl:template name="lookup">
  <xsl:param name="name"/>
  <xsl:param name="datanodes"/>
  <xsl:param name="displaynodes"/>
  <select name="{$name}">
    <xsl:for-each select="msxsl:node-set($displaynodes)/node()">
      <xsl:sort select="." />
      <xsl:variable name="num"
                    select="count(preceding-sibling::node()) + 1"/>
      <xsl:variable name="data"
        select="msxsl:node-set($datanodes)/node()[$num]" />
      <option id="{$data}" value="{$data}">
        <xsl:value-of select="$data"/>-<xsl:value-of select="."/>
      </option>
    </xsl:for-each>
  </select>
</xsl:template>

I'd normally question the wisdom of passing in result tree fragments
as the values of the $datanodes and $displaynodes parameters, but
since you need to have control over their ordering, and the nodes need
to be siblings to get the numbering to work, it's probably a good
idea.

Cheers,

Jeni

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

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


Current Thread