RE: [xsl] Trouble getting the self node attributes within for-each loop

Subject: RE: [xsl] Trouble getting the self node attributes within for-each loop
From: "Chaudhary, Harsh" <HCHAUDHA@xxxxxxxxx>
Date: Fri, 7 Dec 2007 10:57:47 -0600
    <zipcode @zip="11111" @index="984" @population="4765">
    <zipcode @zip="22222" @index="74" @population="437">
    <zipcode @zip="33333" @index="643" @population="432876">
    <zipcode @zip="33333" @index="643" @population="4978">

I am using XSL 1.0

Taking this XML, I need to group the element based on ZipCode and sort
based on zip code also. Then select the element with the highest index
within a group, and in case 2 or more elements have the same, select the
one with the highest population out of the elements with the highest

I have a variable called nodesWithMaxIndex which is supposed to store
all the elements belonging to a zip code which have the maximum index.

My code looked like:

<xsl:variable name="nodesWithMaxIndex" select="selectTheNodes">
	<xsl:for-each <elements with max index>
			<xsl:value-of <whichever node matches>

This variable will then be passed to create another variable which was
name nodeWithMaxPopOutOfNodesWithMaxIndex. But, this variable had just
the element values and not the node itself. So, in order to get the
nodes to be able to pass to generate the second variable, instead of
<xsl:value-of>, I did <xsl:copy-of> which game me a copy of the
appropriate node along with attributes and child elements.

I haven't tried out your solution yet. But then again, I am not using
XSL 2.0. But I will try it out just for fun and see how XSL 2.0 makes my
life easier.


-----Original Message-----
From: Michael Kay [mailto:mike@xxxxxxxxxxxx]
Sent: Friday, December 07, 2007 3:41 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] Trouble getting the self node attributes within
for-each loop

> I apologize for the incomplete question but it is about 8:00
> here and it was getting pretty late. I figured out what I was
> doing wrong. The result of a tired mind I guess. Basically, I
> was passing the value-of the zipcode element as value of the
> variable whereas I should have been doing a copy-of.

8:00 sounds early to me...

You haven't given any explanation as to why you need to copy the
This seems to be a case where your variable should contain references to
selected elements, not copies of them. In XSLT 1.0 that's only possible
you can select the nodes in a single XPath expression, but I would
hazard a
guess that this is achievable in your case.

In 2.0 you can build a node-set (that is, a variable whose value is a
sequence of references to existing nodes) without being constrained to a
single XPath expression, by virtue of the xsl:sequence instruction.

Michael Kay

Current Thread