RE: [xsl] xsl:key use attribute using string() causes missing all matches except the first one

Subject: RE: [xsl] xsl:key use attribute using string() causes missing all matches except the first one
From: Xiaocun Xu <xiaocunxu@xxxxxxxxx>
Date: Wed, 24 Mar 2004 11:26:14 -0800 (PST)
Thanks.  I found this reference in the XSLT
Programmer's Reference, should have paid more
attention when reading :(

Xiaocun

--- Michael Kay <mhk@xxxxxxxxx> wrote:
> When you apply string() to a node-set containing
> more than one node it
> returns the string value of the first node in the
> node-set.
> 
> Michael Kay 
> 
> # -----Original Message-----
> # From: Xiaocun Xu [mailto:xiaocunxu@xxxxxxxxx] 
> # Sent: 24 March 2004 16:42
> # To: xsl-list
> # Subject: [xsl] xsl:key use attribute using
> string() causes 
> # missing all matches except the first one
> # 
> # Hi,
> # 
> # I recently had an interesting discovery with
> xsl:key use 
> # attribute using string().
> # I been using string() in xsl:key use attribute
> extensively 
> # since it allows me to retrieve records using the
> key value 
> # "".  But in the following example, using string()
> caused me 
> # to miss all matches except the first one.  I am
> not sure what 
> # is the reason for this, would appreciate if anyone
> could shed 
> # light on this behavior.  Example input XML and
> XSLT below.
> # 
> # thanks,
> # Xiaocun
> # 	
> # input XML:
> # <range>
> # 	<row row="16">
> # 		<cell column="1">supplier</cell>
> # 		<cell column="2">s3</cell>
> # 		<cell column="3">item</cell>
> # 		<cell column="4">
> # 			<subcell>item1</subcell>
> # 			<subcell>item2</subcell>
> # 			<subcell>item3</subcell>
> # 		</cell>
> # 		<cell column="5">s3_AM1</cell>
> # 		<cell column="6">accepted</cell>
> # 	</row>
> # 	<row row="17">
> # 		<cell column="1">supplier</cell>
> # 		<cell column="2">s4</cell>
> # 		<cell column="3">item</cell>
> # 		<cell column="4">
> # 			<subcell>item1</subcell>
> # 			<subcell>item2</subcell>
> # 			<subcell>item4</subcell>
> # 		</cell>
> # 		<cell column="5">s4_AM1</cell>
> # 		<cell column="6">accepted</cell>
> # 	</row>
> # 	<row row="18">
> # 		<cell column="1">supplier</cell>
> # 		<cell column="2">s5</cell>
> # 		<cell column="3">item</cell>
> # 		<cell column="4">item1</cell>
> # 		<cell column="5">s5_AM1</cell>
> # 		<cell column="6">accepted</cell>
> # 	</row>
> # </range>
> # 
> # complete XSLT without using string():
> # <xsl:stylesheet version="1.0"
> # xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> # xmlns:fo="http://www.w3.org/1999/XSL/Format";>
> # <xsl:key name="itemInvitesKey"
> # match="//row[cell[@column=3] = 'item']"
> # use="cell[@column=4]"/>
> # <xsl:key name="itemInvitesKey"
> # match="//row[cell[@column=3] = 'item']"
> # use="(cell[@column=4]/subcell)"/>
> # <xsl:template match="/">
> # 	<xsl:apply-templates select="range"/>
> # </xsl:template>
> # <xsl:template match="range">
> # 	<xsl:text>item1
</xsl:text>
> # 	<xsl:for-each select="key('itemInvitesKey',
> 'item1')">
> # 		<xsl:value-of select="cell[@column=2]"/>
> # 		<xsl:text>
</xsl:text>
> # 	</xsl:for-each>
> # 	<xsl:text>
</xsl:text>
> # 	<xsl:text>item2
</xsl:text>
> # 	<xsl:for-each select="key('itemInvitesKey',
> 'item2')">
> # 		<xsl:value-of select="cell[@column=2]"/>
> # 		<xsl:text>
</xsl:text>
> # 	</xsl:for-each>
> # </xsl:template>
> # </xsl:stylesheet>
> # 
> # correct output:
> # item1
> # s5
> # s3
> # s4
> # 
> # item2
> # s3
> # s4
> # 
> # Exact same XSLT as above, except xsl:key use
> attribute using string():
> # <xsl:key name="itemInvitesKey"
> # match="//row[cell[@column=3] = 'item']"
> # use="string(cell[@column=4]/subcell)"/>
> # 
> # Output: same as above, except no matches were
> returned except 
> # for the first key (item1)
> # item1
> # s5
> # s3
> # s4
> # 
> # item2
> # 
> # __________________________________
> # Do you Yahoo!?
> # Yahoo! Finance Tax Center - File online. File on
> time.
> # http://taxes.yahoo.com/filing.html
> # 
> #
> # 
> # 
> 


__________________________________
Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.
http://taxes.yahoo.com/filing.html

Current Thread