Re: [xsl] multiple values for the key

Subject: Re: [xsl] multiple values for the key
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sat, 2 Aug 2008 11:18:24 +0530
David, has given one possible solution, in XSLT 1.0 (which has
advantage, that you don't need to use any extension function). Mike
gave you a XSLT 2.0 version (which I feel, is cool).

Based on Mike's idea, here is another version using node-set extension
function in XSLT 1.0,

(this runs on the XML you posted)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                       xmlns:exslt="http://exslt.org/common";
                       exclude-result-prefixes="exslt"
                       version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:key name="x" match="subroot" use="ccc"/>

<xsl:variable name="x-values">
  <v>11</v>
  <v>22</v>
</xsl:variable>

<xsl:template match="/root">
  <result>
    <xsl:for-each select="key('x', exslt:node-set($x-values)/v)">
      <value>
        <xsl:value-of select="eee" />
      </value>
    </xsl:for-each>
  </result>
</xsl:template>

</xsl:stylesheet>

I think, this could be better than David's version, because if you
want to have pretty large no. of different values to search by the
key, you just have to change this part,

<xsl:variable name="x-values">
  <v>11</v>
  <v>22</v>
  <!-- more values -->
</xsl:variable>


On Thu, Jul 31, 2008 at 9:58 PM, sudheshna iyer <sudheshnaiyer@xxxxxxxxx> wrote:
> How do I use multiple key values?
>
> Declaration:
> <xsl:key name="keyname" match="subroot" use="ccc"/>
>
> During the usage, I want to specify multiple values:
>
> <xsl:variable name="keyname" select="key('keyname', '11' or '22')"/> ==>  Here I want to use multiple values 11 and 22.
>
> For the following xml, result should be:
> aaaaa bbbbb ccccc ddddd ==> note that both values of  ccc= '22' and ccc= '11'
>
>
> <?xml version="1.0" encoding="UTF-8"?>
> <root>
>    <subroot id="11111">
>        <ccc>11</ccc> ==> needs to be picked
>        <ddd>2005-08-26</ddd>
>        <eee>aaaaa</eee>
>    </subroot>
>    <subroot id="11111">
>        <ccc>22</ccc> ==> needs to be picked
>        <ddd>2005-08-26</ddd>
>        <eee>bbbbb</eee>
>    </subroot>
>    <subroot id="11111">
>        <ccc>11</ccc>
>        <ddd>2005-08-26</ddd>
>        <eee>ccccc</eee>
>    </subroot>
>    <subroot id="11111">
>        <ccc>11</ccc>
>        <ddd>2005-08-26</ddd>
>        <eee>ddddd</eee>
>    </subroot>
>    <subroot id="11111">
>        <ccc>33</ccc>
>        <ddd>2005-08-26</ddd>
>        <eee>eeeee</eee>
>    </subroot>
> </root>
>
> Thank you for your help.


-- 
Regards,
Mukul Gandhi

Current Thread