RE: [xsl] Using the key function

Subject: RE: [xsl] Using the key function
From: "Fanghanel, Karl" <Karl.Fanghanel@xxxxxxxxx>
Date: Wed, 23 May 2007 08:53:20 -0400
My apologies.  I didn't verify my xml format before sending.  I made the
necessary changes (see below) and it should make sense now.  I added a
bit more information as well.

My xsl code does work giving all the values under the house element as
well as some others, but I'm not able to get the length value from the
corresponding trail element.  The trail element contains information
belonging to the house element and is associated by matching the ref and
id values.  The only way I know how to do this is by using the key
function, but it's not working.

I can't easily change the group-by function because that is what is used
in production and I can't change the "property[@name='number']/@val"
because all the other elements (not shown) are grouped by this unique
property except the trail element.

I realize that the match=//* can be an expensive operation but I'm only
doing this in my development code.

Karl


--- XSL ---

<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; >

<xsl:output method="html"/>

<xsl:key name="numkey" match="//house" use="member/@ref"/>

<html>
	<body>
		<table ... (table format)
			<xsl:for-each-group select="//project" group-
by="property[@name='number']/@val">
			<xsl:sort select="@number"/>
				<tr>
					<td>
						<xsl:value-of
select="current-grouping-
key()"/>
					</td>
					<td>
						<xsl:value-of
select="@name"/>
					</td>
					<td>
						<xsl:value-of
select="key('numkey',@id)/@length"/>
					</td>
				</tr>
			</xsl:for-each-group>
		</table>
	</body>
</html>
</stylesheet>



--- XML ---

<?xml version="1.0" encoding="UTF-8" ?>
...
<project>
	<trail id="1234" name="W$100" length="50.5">
		<property name="route" val="84"/>
	</trail>
	<trail id="9876" name="W$200" length="40.0">
		<property name="route" val="64"/>
	</trail>
	<house id="1122" name="1_CAD">
		<member ref="1234"/>
		<property name="number" val="123L0001"/>
	</house>
	<house id="3344" name="2_CAB">
		<member ref="9876"/>
		<property name="number" val="123L0002"/>
	</house>
</project>

----------------------------------------------------------------


-----Original Message-----
From: David Carlisle [mailto:davidc@xxxxxxxxx]
Sent: Tuesday, May 22, 2007 4:39 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Using the key function


  <xsl:key name="numkey" match="//house" use="member/@ref"/>

starting a match with // does nothing (in a template it just changes the
default template in some cases, but in a key it does nothing)


select="//*"

that's an expensive operation! //* means all elements at all depths in
your entire document I would guess you want to select house elements
here, so that would be
select="house"
(assuming the current element at that point is the project element, I
can't tell as you omitted the releveant bit of the stylesheet)

but if you do mean to select house elements, then the xsl:sort
select=@number won't sort anything as house elements don't have a
number, perhaps you want xsl:sort select="property[@name='number']/@val"
but I really can't tell as you give no indication of what output you
expect or which elements you wish to sort/group

David

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs.
________________________________________________________________________

Current Thread