Re: [xsl] multiple values for the key

Subject: Re: [xsl] multiple values for the key
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sat, 02 Aug 2008 05:43:40 -0400
At 2008-08-02 11:18 +0530, Mukul Gandhi wrote:
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,

The node-set extension can be avoided to achieve what you want.


<xsl:for-each select="key('x', exslt:node-set($x-values)/v)">

The above can be replaced with standard XSLT 1.0 to read the stylesheet file as a source node tree ...


    <xsl:for-each
      select="key('x',document('')/*/xsl:variable[@name='x-values']/v)">

... to get what you want without using extensions. I think it is important not to rely on extensions when not absolutely necessary. The "ken.xsl" below shows this.

I grant, though, that if your stylesheet is large then putting this into a small included or imported fragment would keep any overhead of building the tree small. If, Mukul, you were looking to create a maintainable resource that could be shared across different stylesheets, then "ken2.xsl" below would achieve that where the only tree being built is for the "ken2values.xsl" fragment with the data. This produces a simple variable that XSLT 1.0 stylesheet writers could use.

But moving to XSLT 2.0 would certainly be preferred.

I hope this helps.

. . . . . . . . Ken


t:\ftemp>type iyer.xml <?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>

t:\ftemp>type mukul.xsl
<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>

t:\ftemp>call xslt iyer.xml mukul.xsl mukul.xml

t:\ftemp>type mukul.xml
<?xml version="1.0" encoding="utf-8"?>
<result>
   <value>aaaaa</value>
   <value>bbbbb</value>
   <value>ccccc</value>
   <value>ddddd</value>
</result>
t:\ftemp>type ken.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                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',document('')/*/xsl:variable[@name='x-values']/v)">
      <value>
        <xsl:value-of select="eee" />
      </value>
    </xsl:for-each>
  </result>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>call xslt iyer.xml ken.xsl ken.xml

t:\ftemp>type ken.xml
<?xml version="1.0" encoding="utf-8"?>
<result>
   <value>aaaaa</value>
   <value>bbbbb</value>
   <value>ccccc</value>
   <value>ddddd</value>
</result>
t:\ftemp>type ken2.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

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

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

<xsl:include href="ken2values.xsl"/>

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

</xsl:stylesheet>

t:\ftemp>type ken2values.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

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

<xsl:variable name="x-values"
              select="document('')/*/xsl:variable[@name='x-values-data']/v"/>

</xsl:stylesheet>

t:\ftemp>call xslt iyer.xml ken2.xsl ken2.xml

t:\ftemp>type ken2.xml
<?xml version="1.0" encoding="utf-8"?>
<result>
   <value>aaaaa</value>
   <value>bbbbb</value>
   <value>ccccc</value>
   <value>ddddd</value>
</result>
t:\ftemp>rem Done!



--
Upcoming XSLT/XSL-FO hands-on courses:      Wellington, NZ 2009-01
World-wide corporate, govt. & user group XML, XSL and UBL training
RSS feeds:     publicly-available developer resources and training
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread