Subject: Re: [xsl] problem with using xsl:key From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx> Date: Mon, 11 Feb 2002 19:17:46 +0000 |
Hi Kit, > i would like to define a key to such as group them by name.... (e.g. > <xsl:key name="NameID" match="name" use="<xsl:value-of select="name">) > > and then find the person with a person name Mike M Jones (first=mike follow > by middle=M and last=Jones) writing such key... > > <xsl:apply-templates select="key('NameID', '<xsl:value-of > select="deviation">')"/> > > i know this is not correct but that's probably the best i can > explain the situation... can someone tell me what is the best > approach to achieve the desired results? Sure. When you create a key, you have three attributes that you have to manage: name, match and use. You can make up whatever name you want for the key (you've used 'NameID' and that's fine): <xsl:key name="NameID" match="..." use="..." /> The match attribute determines what kind of nodes get returned by the key. Usually you want the match attribute to match the kinds of elements that you want to group. So in your case, if you want to group person elements by their name, you need the match attribute to match person elements: <xsl:key name="NameID" match="person" use="..." /> The use attribute determines the value by which the nodes that you're matching get grouped. So to group person elements by name, you need the use attribute to hold an expression that, given a person element, returns a string of the form you want. You want the string to have the first name, a space, then the middle name, then a space, then the last name. So the use attribute should be as in the following: <xsl:key name="NameID" match="person" use="concat(name/first, ' ', name/middle, ' ', name/last)" /> Then, to apply templates to all the person elements whose name is 'Mike M Jones', you just do: <xsl:apply-templates select="key('NameID', 'Mike M Jones')" /> Of course the second argument to the key() function doesn't have to be a literal string - you could get it from an element or attribute in your XML structure. So if you had an element like: <derivation>Mike M Jones</derivation> that was a child of the current node, you could do: <xsl:apply-templates select="key('NameID', derivation)" /> Remember that you cannot use xsl:value-of elements within attribute values - the stylesheet is not well formed for a start! If you find yourself wanting to use xsl:value-of within a select or use attribute, then just put whatever you would put in the select attribute of the xsl:value-of directly in the select or use attribute itself. I hope that helps, Jeni --- Jeni Tennison http://www.jenitennison.com/ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] problem with using xsl:key, 林 子芯 | Thread | [xsl] regd: XML schema, laksh vr |
[xsl] newbie question: nested tags, Joe Cheng | Date | [xsl] RE: newbie question: nested t, Joe Cheng |
Month |