This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Key function using key strings from msxsl:node-set generated node. Doesn't work?
- From: "Macaulay,Malcolm (US)" <Malcolm dot Macaulay2 at cnare dot com>
- To: <XSL-List at lists dot mulberrytech dot com>
- Date: Thu, 14 Mar 2002 23:33:45 -0600
- Subject: [xsl] Key function using key strings from msxsl:node-set generated node. Doesn't work?
- Reply-to: xsl-list at lists dot mulberrytech dot com
I have a baffling problem. I'm trying to call the key function using strings extracted from nodes coming from the msxsl:node-set function. No matter what I try (i.e. normalize-space etc) I can't get the same string from msxsl:node-set as I do from the raw XML.
There's a good reason why I don't want to use the Muenchian method (which works fine). But I won't bore you with that. I've created a simple example which illustrates the point.
Any help would be greatly appreciated.
cheers
Malcolm
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Table from keys.xsl"?>
<People>
<Person>
<Name>Malcolm</Name>
<Address>
<No>10</No>
<Street>Lime Close</Street>
</Address>
</Person>
<Person>
<Name>Peter</Name>
<Address>
<No>20</No>
<Street>Milner Rd</Street>
</Address>
</Person>
<Person>
<Name>Mary</Name>
<Address>
<No>20</No>
<Street>Milner Rd</Street>
</Address>
</Person>
<Person>
<Name>John</Name>
<Address>
<No>20</No>
<Street>Milner Rd</Street>
</Address>
</Person>
</People>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:preserve-space elements="*"/>
<!--
key giving Person by the raw address string-->
<xsl:key name="PersonByAddress" match="/People/Person" use="Address"/>
<!--
Address string key so unqiue addresses can a extracted by Muenchian method-->
<xsl:key name="Address" match="/People/Person/Address" use="."/>
<!--
Alternative to Muenchian method - a ready-made list of the unique addresses-->
<xsl:variable name="UniqueAddressStrings">
<Address>
<No> 10</No>
<Street>Lime Close</Street>
</Address>
<Address>
<No> 20</No>
<Street>Milner Rd</Street>
</Address>
</xsl:variable>
<!--
-->
<xsl:template match="/">
<!--
-->
<p>Works: Table contents keyed by unique address strings, determined on-the-fly by Muenchian method</p>
<!--
-->
<table border="1" cellspacing="0">
<tr>
<td>Address</td>
<td>No. of people</td>
</tr>
<xsl:for-each select="/People/Person/Address[generate-id(.)=generate-id(key('Address',.)[1])]">
<xsl:sort select="."/>
<tr>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:value-of select="count(key('PersonByAddress',.))"/>
</td>
</tr>
</xsl:for-each>
</table>
<!--
-->
<p>Doesn't work: Table contents keyed by unique address strings, extracted from a unique list of address included in the XSLT. Requires msxsl:node-set, but cannot get the same string nodes created by node-set function. Normalize-space does not seem to help.</p>
<!--
-->
<table border="1" cellspacing="0">
<tr>
<td>Address</td>
<td>No. of people</td>
</tr>
<xsl:for-each select="msxsl:node-set($UniqueAddressStrings)/Address">
<xsl:sort select="."/>
<tr>
<td>
<xsl:value-of select="normalize-space(.)"/>
</td>
<td>
<xsl:value-of select="count(key('PersonByAddress',normalize-space(.)))"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list