This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: problem with using xsl:key


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]