[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Understanding xsl:key

Subject: RE: Understanding xsl:key
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 22 Aug 2000 08:35:12 +0100
xsl key substring before

>> <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 Tennison

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.