Re: bug(?) when reusing a xsl:key across multiple templates

Mukul Gandhi <[email protected]> Wed, 16 Oct 2013 15:05:46 +0530
Newsgroups gmane.text.xml.xalan.java.user
Message-ID <CABuuzNNSJ-+m=DMaxn4mXwX_WJmoYSpYuK+DQeSfvRswTvhiCQ@mail.gmail.com>
--089e0122893632af7f04e8d86e62
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: quoted-printable

I feel this is not a bug. My reading of XSLT 1.0 spec says, that a xsl:key
declaration (or a set of them) within a stylesheet are applicable only to
one document.

Two different nodesets (constructed from a RTF) are conceptually two
different documents, therefore as per above notion they cannot share
xsl:key declarations.

On Wed, Oct 16, 2013 at 12:18 AM, Hall, Kurt (Truven Health) <
[email protected]> wrote:

>  I suspect that I have found a bug pertaining to multiple key() function
> calls that share a common xsl:key but occur in different templates.  Both
> key() calls are operating on results-tree-fragments that I=92ve turned in=
to
> nodesets with the xalan:nodeset() extension.****
>
> ** **
>
> When the calls are in different templates the second occurance of key()
> doesn=92t return any results.  If I combine template1 and template2 into =
a
> single template then I get correct results for both key() calls.  I have
> worked around the issue by cloning the xsl:key (key2) and no longer reusi=
ng
> them.****
>
> ** **
>
> I=92m using Xalan 2.7.1. ****
>
> ** **
>
> *Results*
>
> =3D=3D=3D=3D=3D=3D Template 1 =3D=3D=3D=3D=3D=3D****
>
> count(tree1)=3D2****
>
> N1=3D1****
>
> N2=3D2****
>
> N3=3D3****
>
> =3D=3D=3D=3D=3D=3D Template 2 =3D=3D=3D=3D=3D=3D****
>
> count(tree2)=3D3****
>
> ** **
>
> ** **
>
> Test Code:****
>
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D****
>
> <?xml version=3D"1.0"  encoding =3D "ISO-8859-1"?>****
>
> <xsl:stylesheet version=3D"1.0" xmlns:xsl=3D"
> http://www.w3.org/1999/XSL/Transform" xmlns:xalan=3D"
> http://xml.apache.org/xalan" exclude-result-prefixes=3D"xalan"> ****
>
> ** **
>
>   <xsl:key name=3D"key1" match=3D"*" use=3D"text()"/>****
>
>   <xsl:key name=3D"key2" match=3D"*" use=3D"text()"/>****
>
> ** **
>
>   <xsl:template match=3D"/">****
>
>       <xsl:call-template name=3D"template1"/>****
>
>       <xsl:call-template name=3D"template2"/>****
>
>       <xsl:call-template name=3D"template3"/>****
>
>   </xsl:template>****
>
> ** **
>
>   <xsl:template name=3D"template1">****
>
>       <xsl:message>=3D=3D=3D=3D=3D=3D Template 1 =3D=3D=3D=3D=3D=3D</xsl:=
message>****
>
>       <xsl:variable name=3D"rtf1"><N1>1</N1><N2>2</N2></xsl:variable>****
>
>       <xsl:variable name=3D"tree1" select=3D"xalan:nodeset( $rtf1)"/>****
>
>       <xsl:message>count(tree1)=3D<xsl:value-of select=3D"count(
> $tree1/*)"/></xsl:message>****
>
>       <xsl:variable name=3D"uniqNodes" select=3D"$tree1/*[generate-id() =
=3D
> generate-id( key( 'key1', text()))]"/>****
>
>       <xsl:for-each select=3D"$uniqNodes">****
>
>         <xsl:message><xsl:value-of select=3D"local-name()"/>=3D<xsl:value=
-of
> select=3D"text()"/></xsl:message>****
>
>       </xsl:for-each>****
>
>   </xsl:template>****
>
> ** **
>
>   <xsl:template name=3D"template2">****
>
>       <xsl:message>=3D=3D=3D=3D=3D=3D Template 2 =3D=3D=3D=3D=3D=3D</xsl:=
message>****
>
>       <xsl:variable
> name=3D"rtf2"><N4>4</N4><N5>5</N5><N6>6</N6></xsl:variable>****
>
>       <xsl:variable name=3D"tree2" select=3D"xalan:nodeset( $rtf2)"/>****
>
>       <xsl:message>count(tree2)=3D<xsl:value-of select=3D"count(
> $tree2/*)"/></xsl:message>****
>
>       <!-- ************  CHANGE FOLLOWING FROM key1 to key2 *************
> -->****
>
>       <xsl:variable name=3D"uniqNodes" select=3D"$tree2/*[generate-id() =
=3D
> generate-id( key( 'key1', text()))]"/>****
>
>       <!-- **************************************************************
> -->****
>
>       <xsl:for-each select=3D"$uniqNodes">****
>
>           <xsl:message><xsl:value-of select=3D"local-name()"/>=3D<xsl:val=
ue-of
> select=3D"text()"/></xsl:message>****
>
>       </xsl:for-each>****
>
>   </xsl:template>****
>
> ** **
>
> </xsl:stylesheet>****
>
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D****
>
> ** **
>
> Thanks for any help,****
>
> -kurt****
>
> (This is my first post to the user-group so forgive me if I=92m not
> following proper protocol)****
>
> ** **
>
> ** **
>
>
> *Kurt Hall*
> Principal Engineer****
>
> *Truven Health Analytics*
>
> Phone: 303-486-9161****
>
> [email protected]
> truvenhealth.com****
>
> The Healthcare Business of Thomson Reuters is now
> Truven Health Analytics. ****
>
> ** **
>



--=20
Regards,
Mukul Gandhi

--089e0122893632af7f04e8d86e62
Content-Type: text/html; charset=windows-1252
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><div>I feel this is not a bug. My reading of XSLT 1.0=
 spec says, that a xsl:key declaration (or a set of them) within a styleshe=
et are applicable only to one document.<br><br></div>Two different nodesets=
 (constructed from a RTF) are conceptually two different documents, therefo=
re as per above notion they cannot share xsl:key declarations.<br>

</div><div><div><div><div><div class=3D"gmail_extra"><br><div class=3D"gmai=
l_quote">On Wed, Oct 16, 2013 at 12:18 AM, Hall, Kurt (Truven Health) <span=
 dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" target=3D"_b=
lank">[email protected]</a>&gt;</span> wrote:<br>

<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-=
left:1px solid rgb(204,204,204);padding-left:1ex">





<div link=3D"blue" vlink=3D"purple" lang=3D"EN-US">
<div>
<p class=3D"">I suspect that I have found a bug pertaining to multiple key(=
) function calls that share a common xsl:key but occur in different templat=
es.=A0 Both key() calls are operating on results-tree-fragments that I=92ve=
 turned into nodesets with the
 xalan:nodeset() extension.<u></u><u></u></p>
<p class=3D""><u></u>=A0<u></u></p>
<p class=3D"">When the calls are in different templates the second occuranc=
e of key() doesn=92t return any results.=A0 If I combine template1 and temp=
late2 into a single template then I get correct results for both key() call=
s.=A0 I have worked around the
 issue by cloning the xsl:key (key2) and no longer reusing them.<u></u><u><=
/u></p>
<p class=3D""><u></u>=A0<u></u></p>
<p class=3D"">I=92m using Xalan 2.7.1. <u></u><u></u></p>
<p class=3D""><u></u>=A0<u></u></p>
<p class=3D""><b>Results<u></u><u></u></b></p>
<p class=3D"">=3D=3D=3D=3D=3D=3D Template 1 =3D=3D=3D=3D=3D=3D<u></u><u></u=
></p>
<p class=3D"">count(tree1)=3D2<u></u><u></u></p>
<p class=3D"">N1=3D1<u></u><u></u></p>
<p class=3D"">N2=3D2<u></u><u></u></p>
<p class=3D"">N3=3D3<u></u><u></u></p>
<p class=3D"">=3D=3D=3D=3D=3D=3D Template 2 =3D=3D=3D=3D=3D=3D<u></u><u></u=
></p>
<p class=3D"">count(tree2)=3D3<u></u><u></u></p>
<p class=3D""><u></u>=A0<u></u></p>
<p class=3D""><u></u>=A0<u></u></p>
<p class=3D"">Test Code:<u></u><u></u></p>
<p class=3D"">=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<u></u><u></u></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">&lt;?xml =
version=3D&quot;1.0&quot;=A0 encoding =3D &quot;ISO-8859-1&quot;?&gt;<u></u=
><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">&lt;xsl:s=
tylesheet version=3D&quot;1.0&quot; xmlns:xsl=3D&quot;<a href=3D"http://www=
.w3.org/1999/XSL/Transform" target=3D"_blank">http://www.w3.org/1999/XSL/Tr=
ansform</a>&quot; xmlns:xalan=3D&quot;<a href=3D"http://xml.apache.org/xala=
n" target=3D"_blank">http://xml.apache.org/xalan</a>&quot; exclude-result-p=
refixes=3D&quot;xalan&quot;&gt;
<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;"><u></u>=
=A0<u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0 &lt;x=
sl:key name=3D&quot;key1&quot; match=3D&quot;*&quot; use=3D&quot;text()&quo=
t;/&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0 &lt;x=
sl:key name=3D&quot;key2&quot; match=3D&quot;*&quot; use=3D&quot;text()&quo=
t;/&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;"><u></u>=
=A0<u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0 &lt;x=
sl:template match=3D&quot;/&quot;&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:call-template name=3D&quot;template1&quot;/&gt;<u></u><u></u=
></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:call-template name=3D&quot;template2&quot;/&gt;<u></u><u></u=
></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:call-template name=3D&quot;template3&quot;/&gt;<u></u><u></u=
></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0 &lt;/=
xsl:template&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;"><u></u>=
=A0<u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0 &lt;x=
sl:template name=3D&quot;template1&quot;&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:message&gt;=3D=3D=3D=3D=3D=3D Template 1 =3D=3D=3D=3D=3D=3D&=
lt;/xsl:message&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:variable name=3D&quot;rtf1&quot;&gt;&lt;N1&gt;1&lt;/N1&gt;&l=
t;N2&gt;2&lt;/N2&gt;&lt;/xsl:variable&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:variable name=3D&quot;tree1&quot; select=3D&quot;xalan:nodes=
et( $rtf1)&quot;/&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:message&gt;count(tree1)=3D&lt;xsl:value-of select=3D&quot;co=
unt( $tree1/*)&quot;/&gt;&lt;/xsl:message&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:variable name=3D&quot;uniqNodes&quot; select=3D&quot;$tree1/=
*[generate-id() =3D generate-id( key( &#39;key1&#39;, text()))]&quot;/&gt;<=
u></u><u></u></span></p>


<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:for-each select=3D&quot;$uniqNodes&quot;&gt;<u></u><u></u></=
span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0=A0=A0 &lt;xsl:message&gt;&lt;xsl:value-of select=3D&quot;local-name(=
)&quot;/&gt;=3D&lt;xsl:value-of select=3D&quot;text()&quot;/&gt;&lt;/xsl:me=
ssage&gt;<u></u><u></u></span></p>


<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;/xsl:for-each&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0 &lt;/=
xsl:template&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;"><u></u>=
=A0<u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0 &lt;x=
sl:template name=3D&quot;template2&quot;&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:message&gt;=3D=3D=3D=3D=3D=3D Template 2 =3D=3D=3D=3D=3D=3D&=
lt;/xsl:message&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:variable name=3D&quot;rtf2&quot;&gt;&lt;N4&gt;4&lt;/N4&gt;&l=
t;N5&gt;5&lt;/N5&gt;&lt;N6&gt;6&lt;/N6&gt;&lt;/xsl:variable&gt;<u></u><u></=
u></span></p>


<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:variable name=3D&quot;tree2&quot; select=3D&quot;xalan:nodes=
et( $rtf2)&quot;/&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:message&gt;count(tree2)=3D&lt;xsl:value-of select=3D&quot;co=
unt( $tree2/*)&quot;/&gt;&lt;/xsl:message&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;!-- ************=A0 CHANGE FOLLOWING FROM key1 to key2 *********=
**** --&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:variable name=3D&quot;uniqNodes&quot; select=3D&quot;$tree2/=
*[generate-id() =3D generate-id( key( &#39;key1&#39;, text()))]&quot;/&gt;<=
u></u><u></u></span></p>


<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;!-- ************************************************************=
** --&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;xsl:for-each select=3D&quot;$uniqNodes&quot;&gt;<u></u><u></u></=
span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0=A0=A0=A0=A0 &lt;xsl:message&gt;&lt;xsl:value-of select=3D&quot;local=
-name()&quot;/&gt;=3D&lt;xsl:value-of select=3D&quot;text()&quot;/&gt;&lt;/=
xsl:message&gt;<u></u><u></u></span></p>


<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0=A0=A0=
=A0=A0 &lt;/xsl:for-each&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">=A0 &lt;/=
xsl:template&gt;<u></u><u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;"><u></u>=
=A0<u></u></span></p>
<p class=3D""><span style=3D"font-family:&quot;Courier New&quot;">&lt;/xsl:=
stylesheet&gt;<u></u><u></u></span></p>
<p class=3D"">=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<u></u><u></u></p>
<p class=3D""><u></u>=A0<u></u></p>
<p class=3D"">Thanks for any help,<u></u><u></u></p>
<p class=3D"">-kurt<u></u><u></u></p>
<p class=3D"">(This is my first post to the user-group so forgive me if I=
=92m not following proper protocol)<u></u><u></u></p>
<p class=3D""><u></u>=A0<u></u></p>
<p class=3D""><u></u>=A0<u></u></p>
<p class=3D""><span style=3D"font-size:9pt;font-family:&quot;Arial&quot;,&q=
uot;sans-serif&quot;;color:rgb(37,40,42)"><br>
<b>Kurt Hall</b><br>
Principal Engineer<u></u><u></u></span></p>
<p class=3D"" style=3D"margin-right:0in;margin-bottom:9pt;margin-left:0in">
<b><span style=3D"font-size:9pt;font-family:&quot;Arial&quot;,&quot;sans-se=
rif&quot;;color:rgb(0,114,206)">Truven Health Analytics<u></u><u></u></span=
></b></p>
<p class=3D""><span style=3D"font-size:9pt;font-family:&quot;Arial&quot;,&q=
uot;sans-serif&quot;;color:rgb(37,40,42)">Phone:
</span><span style=3D"font-size:9pt;font-family:&quot;Arial&quot;,&quot;san=
s-serif&quot;;color:rgb(102,102,102)">303-486-9161</span><span style=3D"fon=
t-size:9pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:rgb(3=
7,40,42)"><u></u><u></u></span></p>


<p class=3D"" style=3D"margin-right:0in;margin-bottom:9pt;margin-left:0in">
<span style=3D"font-size:9pt;font-family:&quot;Arial&quot;,&quot;sans-serif=
&quot;;color:rgb(0,114,206)"><a href=3D"mailto:[email protected]" =
target=3D"_blank">[email protected]</a><br>
<a href=3D"http://truvenhealth.com" target=3D"_blank">truvenhealth.com</a><=
u></u><u></u></span></p>
<p class=3D""><span style=3D"font-size:9pt;font-family:&quot;Arial&quot;,&q=
uot;sans-serif&quot;;color:rgb(37,40,42)">The Healthcare Business of Thomso=
n Reuters is now
<br>
Truven Health Analytics. <u></u><u></u></span></p>
<p class=3D""><u></u>=A0<u></u></p>
</div>
</div>

</blockquote></div><br><br clear=3D"all"><br>-- <br>Regards,<br>Mukul Gandh=
i
</div></div></div></div></div></div>

--089e0122893632af7f04e8d86e62--