Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using two elements for xsl:key key

Tags:

xslt

xslkey

I know that if I have an XML file like this:

 <persons>
   <class name="English">
        <person name="Tarzan" id="050676"/>
        <person name="Donald" id="070754"/>
        <person name="Dolly" id="231256"/>
   </class>
   <class name="Math">
        <person name="Winston" id="050677"/>
        <person name="Donald"  id="070754"/>
        <person name="Fred"    id="231257"/>
   </class>
 </persons>

I can define a key in an XSL file like this:

 <xsl:key name="preg" match="person" use="@id"/> 

where I'm using id as the key. However, Donald is listed twice, but is only in one place in preg.

Suppose I want him listed twice in preg. That is, I want to make the class name be part of the identifier. Basically, I want preg to have keys that are equivalent to ordered pairs: (class-name, id). How do I do that (using XSLT 1.0)?

like image 855
Paul Reiners Avatar asked Feb 27 '23 14:02

Paul Reiners


1 Answers

Concatenate the keys? How about

use="concat(../@name, @id)"

This would serve to keep them separate in the index. You'd of course have to use the same key to retrieve them. To avoid any ambiguity I'd also include a delimiter that won't occur in either subkey, as in

use="concat(../@name, '|', @id)"

This is the recommended approach in Michael Kay's XSLT2 reference.

like image 188
Jim Garrison Avatar answered Mar 01 '23 23:03

Jim Garrison