Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 1st, 2007, 05:08 AM
Authorized User
 
Join Date: Jun 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default using 3 XPaths to index using xsl:key

I am trying to create an index based on 3 elements but not sure if it is possible.

the following is sample lookup xml document and i need to retrieve <target_value> given <stream>, <codetype> and <source_sourcevalue>.

<lookups>
      <lookup>
            <stream>client</stream>
            <codetype>entity.title_id</codetype>
            <source_sourcesystem>XXX</source_sourcesystem>
            <source_sourcetable>entity</source_sourcetable>
            <source_sourcefielddescription/>
            <source_sourcevalue>92</source_sourcevalue>
            <target_description>-</target_description>
            <target_codetype>Title</target_codetype>
            <target_value>20092<target_value</>
            <target_table>_client</target_table>
      </lookup>
      <lookup>
            <stream>client</stream>
            <codetype>entity.title_id</codetype>
            <source_sourcesystem>XXX</source_sourcesystem>
            <source_sourcetable>entity</source_sourcetable>
            <source_sourcefielddescription>Abbot</source_sourcefielddescription>
            <source_sourcevalue>62</source_sourcevalue>
            <target_description>-</target_description>
            <target_codetype>Title</target_codetype>
            <target_value>10062<target_value</>
            <target_table>_client</target_table>
      </lookup>
</lookups>

and the following is snapshot of my XSLT that does the lookup


<xsl:variable name="lookupDoc" select="document('lookup.xml')"/>

<xsl:key use="stream|codetype|source_sourceValue" name="getLookUp" match="lookup"/>

<xsl:template name="lookup">
      <xsl:param name="stream"/>
      <xsl:param name="codeType"/>
      <xsl:param name="sourceValue"/>



      <xsl:value-of select="string(key('getLookUp', concat(stream, codeType, sourceValue))/target_value)"/>
</xsl:template>


thanks


 
Old March 1st, 2007, 06:02 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There's no built-in capability for multi-part keys in XSLT but you can do-it-yourself using string concatenation. Just make sure you use the same expression in the "use" attribute of xsl:key and in the second argument of the key function:

use = "concat(stream, '#', codeType, '#', sourceValue)"

key('k', concat(stream, '#', codeType, '#', sourceValue))

In 2.0 you can use a user-defined function to hide the sordid detail:

use="f:compound-key(stream, codeType, sourceValue)"

key('k', f:compound-key(stream, codeType, sourceValue))

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 2nd, 2007, 01:02 AM
Authorized User
 
Join Date: Jun 2006
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for the quick reply.

I realised that my above example doesnt work still. I read in one of your very old posting in biglist.com that if your lookup file is a separate XML document and it is declared as <xsl:variable name="colorLookupDoc" select="document('lookup.xml')"/> in the XSLT, then the XSLT must use <xsl:for-each select="$colorLookupDoc"> in order to lookup the value.

But will this slow down performance? I was hoping that a simple statement like <xsl:value-of select="key('kk', $index)"/> would do the job. I am essentially doing a lookup on about 50% of the nodes in the primary XML document and if the <xsl:for-value> is used then wouldnt it be better to use a direct XPath expression?

For example, the following is the lookup xml

<color>
    <cid>c1</cid>
    <value>yellow</value>
</color>
<color>
    <cid>c2</cid>
    <value>black</value>
</color>

then wouldnt it be better that i use
<xsl:value-of select="$lookupDoc/color[cid = $index]"/>

or would xsl:key still add value in performance?

thanks. I hope my explanantion makes sense.
 
Old March 2nd, 2007, 04:09 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If it didn't work with the second form, then you were doing something wrong.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
<xsl:key elayaraja.s XSLT 1 July 23rd, 2008 05:00 AM
allow duplicate values in index key pawatz70 Access 3 November 12th, 2007 07:41 PM
Multiple-key index umeshtheone Pro VB 6 3 October 30th, 2007 07:50 AM
Using key() and <xsl:key> freddy XSLT 2 January 18th, 2007 08:55 PM
selection within xsl:key Kabe XSLT 4 February 25th, 2005 05:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.