XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (191 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
12.26Mb size Format: txt, pdf, ePub

Multivalued Keys

A key can be multivalued, in that a single node can have several values, each of which can be used to find the node independently. For example, a book may have several authors, and each author's name can be used as a key value. This could be defined as follows:


The
use
expression,
author/name
, selects more than one node, so the typed value of each of its nodes (that is, the name of each author of the book) is used as one of the values in the set of node-value pairs that makes up the key.

In this particular example, as well as one book having several authors, each author may have written several books, so when you use an XPath expression such as:


you will be selecting all the books in which Agatha Christie was one of the authors. What if you want to find all the books in which Alex Homer and David Sussman are joint authors? You can do this using the
intersect
operator provided in XPath 2.0:

     select=“key(‘book-author’, ‘Alex Homer’)”/>

     select=“key(‘book-author’, ‘David Sussman’)”/>

     select=“$set1 intersect $set2”/>

You can also supply a sequence of several values as the second argument to the
key()
function. For example, you might write:



The result of the
select
expression in the

instruction is the set of all books in which one of the authors is either Agatha Christie or a co-author of Agatha Christie. This is because
$ac
is the set of all books in which Agatha Christie is an author, so
$ac/author/name
is the set of all authors of these books, and using this set of named authors as the value of the key produces the set of books in which
any
of them is an author.

Example: Multivalued Nonunique Keys

This example shows how a node can have several values for one key, and a given key value can identify more than one node. It uses author name as a key to locate

elements.

Source

The source file is
booklist.xml
:



    Design Patterns

    Erich Gamma

    Richard Helm

    Ralph Johnson

    John Vlissides



    Pattern Hatching

    John Vlissides



    Building Applications Frameworks

    Mohamed Fayad

    Douglas C. Schmidt

    Ralph Johnson



    Implementing Applications Frameworks

    Mohamed Fayad

    Douglas C. Schmidt

    Ralph Johnson



Stylesheet

The stylesheet is
author-key.xsl
.

It declares the key and then simply copies the

elements that match the author name supplied as a parameter. You can run this stylesheet in Saxon with a call such as (all on one line):

java -jar c:\saxon\saxon9.jar -s:booklist.xml -xsl:author-key.xsl

      author=“Ralph Johnson”

or in Altova (note the nested quotes):

AltovaXML -in booklist.xml -xslt2 author-key.xsl

      -param author=“‘John Vlissides’”

or in Gestalt:

gestalt author-key.xsl booklist.xml author=“John Vlissides”

Note that parameters containing spaces have to be written in quotes on the command line. In Altova, two pairs of quotes are needed, because the value supplied for the parameter is an XPath expression rather than a string.

 xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”

 version=“2.0”

>




  



Output

With the parameter set to the value
John Vlissides
, the output is as follows:



    Design Patterns

    Erich Gamma

    Richard Helm

    Ralph Johnson

    John Vlissides



    Pattern Hatching

    John Vlissides


(In Altova, the result will not be indented, because the Altova processor discards whitespace text nodes from the input.)

Multiple Named Keys

There is nothing to stop you from defining several keys for the same nodes. For example:



Other books

Nine White Horses by Judith Tarr
The Pacific Giants by Jean Flitcroft
Smoke and Mirrors by Elly Griffiths
Awakening The Warriors by S E Gilchrist
The Forbidden Lord by Sabrina Jeffries
A Safe Place for Joey by Mary MacCracken
Leland's Baby by Michelle Hart
Where Old Ghosts Meet by Kate Evans