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]

RE: Understanding xsl:key


Heiner,

>> <xsl:key name="test" match="title" use="substring-before(name, ' ')"/>
>> will create 
>> node		key value
>> title[1]	'Design'
>> title[2]	'Pattern'
>> title[3]	'Building'
>> 
>> I assume though I haven't tried it.
>
>I have done something similar, which was working fine. But I wanted 
>to know how to do it with the key() function, and that's what I don't 
>get working.

The key function is just a simple look up - you give it a key value, it
tells you the nodes that are associated with that value.  There isn't any
functionality in keys that allow you to look at all the key values and find
those that fulfill a certain pattern.

In your 'Pattern' example, say, to get the functionality that you want, the
processor would have to go through every single key value in the key, check
whether it contained the string 'Pattern', and then return those nodes that
have a key value that fulfill that test.  This would probably be a lot more
complicated for the processor, certainly take a longer time.

Which isn't to say that it wouldn't be useful.  In particular, we have to
jump through quite a lot of hoops to find out what the unique key values
are when doing grouping - a function that gave a list of all the key values
would be really helpful in doing this quickly.

If you know that the only titles that you're going to be interested in are
those containing the word 'Pattern', then you can hard code this into your
stylesheet with:

  <xsl:key name="pattern-titles" match="title" use="contains(., 'Pattern')" />

This will give you a key that has two possible key values, 'true' and
'false'.  If you then do key('pattern-titles', 'true'), then you will get a
list of those titles that contain 'Pattern'.

Obviously this technique is useless if the search string you're interested
in changes each time you run the stylesheet.

The other thing that you can do (my thanks to Mike Kay's book for pointing
this possibility out) is assign each title multiple key values within the
same key space.  So you can do:

  <xsl:key name="test" match="title" use="." />
  <xsl:key name="test" match="title" use="substring-before(., ' ')" />
  <xsl:key name="test" match="title" use="substring-after(., ' ')" />

If all your titles are less than two words long, then this will allow you
to do:

  key('test', 'Pattern')

and get all the 'title' elements whose title is 'Pattern' or whose first
word is 'Pattern' or whose last word (in a two-word title) is 'Pattern'.

You can add extra keys to your heart's content in order to break down the
title further:

  <xsl:key name="test" match="title"
           use="substring-before(substring-after(., ' '), ' '))" />
  <xsl:key name="test" match="title"
           use="substring-before(substring-after(substring-after(., ' '), '
'), ' ')" />
  ...

but it is a bit laborious! :)

I hope that this helps, anyway,

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]