RE: [xsl] preceding-sibling after sort gives unexpected result

Subject: RE: [xsl] preceding-sibling after sort gives unexpected result
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Thu, 11 Sep 2003 19:01:13 +0100
> I am trying to process a document similar to the one below, 
> using the stylesheet below. My problem is that after I have 
> performed the sort, I appear to get the preceding sibling in 
> the original document order rather than in the newly sorted order.

preceding-sibling finds a node's neighbours in the tree that it belongs
to, not its neighbours in the list of nodes that you happen to be
processing at the time.

You can construct a new tree containing copies of the node and (using
xx:node-set()) find the preceding-sibling on this new tree.

Michael Kay

> 
> Looking at the results, you can see that for result number 4, 
> I was hoping to get (a) as my preceding sibling, and not (c), 
> which was the original document order.
> 
> This is an unexpected result, and not at all what I was 
> hoping for. Is this supposed to happen, or is this just a 
> quirk of MSXML?
> 
> 
> Kaine
> 
> 
> Source XML:
> -----------
> <root>
> 	<parent>
> 		<child name="a">40</child>
> 		<child name="b">60</child>
> 		<child name="c">50</child>
> 		<child name="d">30</child>
> 		<child name="e">20</child>
> 	</parent>
> </root>
> 
> 
> Stylesheet:
> -----------
> <xsl:template match="root">
> 	<results>
> 		<xsl:apply-templates select="parent"/>
> 	</results>
> </xsl:template>
> 
> 
> <xsl:template match="parent">
> 	<xsl:apply-templates select="child">
> 		<xsl:sort select="." data-type="number" 
> order="descending"/>
> 	</xsl:apply-templates>
> </xsl:template>
> 
> 
> <xsl:template match="child">
> 	<result number="{position()}">
> 		<current-child-name>
> 			<xsl:value-of select="@name"/>
> 		</current-child-name>
> 		<current-child-value>
> 			<xsl:value-of select="."/>
> 		</current-child-value>
> 		<preceding-sibling-name>
> 			<xsl:value-of
> select="preceding-sibling::child[1]/@name"/>
> 		</preceding-sibling-name>
> 	</result>
> </xsl:template>
> 
> 
> Result:
> -------
> <results>
> 	<result number="1">
> 		<current-child-name>b</current-child-name>
> 		<current-child-value>60</current-child-value>
> 		<preceding-sibling-name>a</preceding-sibling-name>
> 	</result>
> 	<result number="2">
> 		<current-child-name>c</current-child-name>
> 		<current-child-value>50</current-child-value>
> 		<preceding-sibling-name>b</preceding-sibling-name>
> 	</result>
> 	<result number="3">
> 		<current-child-name>a</current-child-name>
> 		<current-child-value>40</current-child-value>
> 		<preceding-sibling-name/>
> 	</result>
> 	<result number="4">
> 		<current-child-name>d</current-child-name>
> 		<current-child-value>30</current-child-value>
> 		<preceding-sibling-name>c</preceding-sibling-name>
> 	</result>
> 	<result number="5">
> 		<current-child-name>e</current-child-name>
> 		<current-child-value>20</current-child-value>
> 		<preceding-sibling-name>d</preceding-sibling-name>
> 	</result>
> </results>
> 
> 
> Post sort:
> ----------
> <root>
> 	<parent>
> 		<child name="b">60</child>
> 		<child name="c">50</child>
> 		<child name="a">40</child>
> 		<child name="d">30</child>
> 		<child name="e">20</child>
> 	</parent>
> </root>
> 
> 
> My system:
> ----------
> XP Professional
> IE6 SP2
> MSXML4 SP2
> 
> 
> SNIP
> 
> 
> 
> PROACTIS Group Limited
> Holtby Manor, Stamford Bridge Road, York, YO19 5LL
> Tel +44 (0)1904 481999 Fax +44 (0)1904 481666
> Visit us at www.proactis.com <http://www.proactis.com/> 
> 
> PROACTIS (r) - Control spend.  Streamline purchasing.
> 
> ************************************************************
> The information contained in this email is intended only for 
> the individual to whom it is addresses.  It may contain 
> privileged and confidential information.  If you have 
> received this message in error or there are any problems, 
> please notify the sender immediately and delete the message 
> from your computer.  The unauthorised use, disclosure, 
> copying or alteration of this message is forbidden.  PROACTIS 
> Group Limited will not be liable for direct, special, 
> indirect or consequential damage as a result of any virus 
> being passed on, or arising from alteration of the content of 
> this message by a third party.
> ************************************************************
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


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


Current Thread