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

"Hall, Kurt (Truven Health)" <[email protected]> Tue, 15 Oct 2013 18:48:03 +0000
Newsgroups gmane.text.xml.xalan.java.user
Message-ID <5382f7a88aed42998d170a118d7eadaf@BY2PR07MB089.namprd07.prod.outlook.com>
--_000_5382f7a88aed42998d170a118d7eadafBY2PR07MB089namprd07pro_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

I suspect that I have found a bug pertaining to multiple key() function cal=
ls that share a common xsl:key but occur in different templates.  Both key(=
) calls are operating on results-tree-fragments that I've turned into nodes=
ets with the xalan:nodeset() extension.

When the calls are in different templates the second occurance of key() doe=
sn't return any results.  If I combine template1 and template2 into a singl=
e 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 reusing them.

I'm 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/Tra=
nsform" 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:me=
ssage>
      <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-o=
f 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:me=
ssage>
      <xsl:variable name=3D"rtf2"><N4>4</N4><N5>5</N5><N6>6</N6></xsl:varia=
ble>
      <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:value=
-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'm 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.


--_000_5382f7a88aed42998d170a118d7eadafBY2PR07MB089namprd07pro_
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr=
osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:x=3D"urn:schemas-microsoft-com:office:excel" xmlns:m=3D"http://schema=
s.microsoft.com/office/2004/12/omml" xmlns=3D"http://www.w3.org/TR/REC-html=
40">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"=
>
<meta name=3D"Generator" content=3D"Microsoft Word 12 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
	{font-family:Calibri;
	panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:11.0pt;
	font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
	{mso-style-priority:99;
	color:blue;
	text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
	{mso-style-priority:99;
	color:purple;
	text-decoration:underline;}
span.EmailStyle17
	{mso-style-type:personal-compose;
	font-family:"Calibri","sans-serif";
	color:windowtext;}
.MsoChpDefault
	{mso-style-type:export-only;}
@page WordSection1
	{size:8.5in 11.0in;
	margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
	{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=3D"EN-US" link=3D"blue" vlink=3D"purple">
<div class=3D"WordSection1">
<p class=3D"MsoNormal">I suspect that I have found a bug pertaining to mult=
iple key() function calls that share a common xsl:key but occur in differen=
t templates.&nbsp; Both key() calls are operating on results-tree-fragments=
 that I&#8217;ve turned into nodesets with the
 xalan:nodeset() extension.<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">When the calls are in different templates the second=
 occurance of key() doesn&#8217;t return any results.&nbsp; If I combine te=
mplate1 and template2 into a single template then I get correct results for=
 both key() calls.&nbsp; I have worked around the
 issue by cloning the xsl:key (key2) and no longer reusing them.<o:p></o:p>=
</p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">I&#8217;m using Xalan 2.7.1. <o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal"><b>Results<o:p></o:p></b></p>
<p class=3D"MsoNormal">=3D=3D=3D=3D=3D=3D Template 1 =3D=3D=3D=3D=3D=3D<o:p=
></o:p></p>
<p class=3D"MsoNormal">count(tree1)=3D2<o:p></o:p></p>
<p class=3D"MsoNormal">N1=3D1<o:p></o:p></p>
<p class=3D"MsoNormal">N2=3D2<o:p></o:p></p>
<p class=3D"MsoNormal">N3=3D3<o:p></o:p></p>
<p class=3D"MsoNormal">=3D=3D=3D=3D=3D=3D Template 2 =3D=3D=3D=3D=3D=3D<o:p=
></o:p></p>
<p class=3D"MsoNormal">count(tree2)=3D3<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">Test Code:<o:p></o:p></p>
<p class=3D"MsoNormal">=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<o:p></o:p><=
/p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&lt;?xml version=3D&quot;1.0&quot;&nbsp; encoding =3D &quot;ISO-8859-1&quot=
;?&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&lt;xsl:stylesheet version=3D&quot;1.0&quot; xmlns:xsl=3D&quot;http://www.w=
3.org/1999/XSL/Transform&quot; xmlns:xalan=3D&quot;http://xml.apache.org/xa=
lan&quot; exclude-result-prefixes=3D&quot;xalan&quot;&gt;
<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
<o:p>&nbsp;</o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp; &lt;xsl:key name=3D&quot;key1&quot; match=3D&quot;*&quot; use=3D&quo=
t;text()&quot;/&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp; &lt;xsl:key name=3D&quot;key2&quot; match=3D&quot;*&quot; use=3D&quo=
t;text()&quot;/&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
<o:p>&nbsp;</o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp; &lt;xsl:template match=3D&quot;/&quot;&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:call-template name=3D&quot;template1=
&quot;/&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:call-template name=3D&quot;template2=
&quot;/&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:call-template name=3D&quot;template3=
&quot;/&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp; &lt;/xsl:template&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
<o:p>&nbsp;</o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp; &lt;xsl:template name=3D&quot;template1&quot;&gt;<o:p></o:p></span><=
/p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:message&gt;=3D=3D=3D=3D=3D=3D Templa=
te 1 =3D=3D=3D=3D=3D=3D&lt;/xsl:message&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:variable name=3D&quot;rtf1&quot;&gt;=
&lt;N1&gt;1&lt;/N1&gt;&lt;N2&gt;2&lt;/N2&gt;&lt;/xsl:variable&gt;<o:p></o:p=
></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:variable name=3D&quot;tree1&quot; se=
lect=3D&quot;xalan:nodeset( $rtf1)&quot;/&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:message&gt;count(tree1)=3D&lt;xsl:va=
lue-of select=3D&quot;count( $tree1/*)&quot;/&gt;&lt;/xsl:message&gt;<o:p><=
/o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:variable name=3D&quot;uniqNodes&quot=
; select=3D&quot;$tree1/*[generate-id() =3D generate-id( key( 'key1', text(=
)))]&quot;/&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:for-each select=3D&quot;$uniqNodes&q=
uot;&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:message&gt;&lt;xsl:value=
-of select=3D&quot;local-name()&quot;/&gt;=3D&lt;xsl:value-of select=3D&quo=
t;text()&quot;/&gt;&lt;/xsl:message&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/xsl:for-each&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp; &lt;/xsl:template&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
<o:p>&nbsp;</o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp; &lt;xsl:template name=3D&quot;template2&quot;&gt;<o:p></o:p></span><=
/p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:message&gt;=3D=3D=3D=3D=3D=3D Templa=
te 2 =3D=3D=3D=3D=3D=3D&lt;/xsl:message&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:variable name=3D&quot;rtf2&quot;&gt;=
&lt;N4&gt;4&lt;/N4&gt;&lt;N5&gt;5&lt;/N5&gt;&lt;N6&gt;6&lt;/N6&gt;&lt;/xsl:=
variable&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:variable name=3D&quot;tree2&quot; se=
lect=3D&quot;xalan:nodeset( $rtf2)&quot;/&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:message&gt;count(tree2)=3D&lt;xsl:va=
lue-of select=3D&quot;count( $tree2/*)&quot;/&gt;&lt;/xsl:message&gt;<o:p><=
/o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- ************&nbsp; CHANGE FOLLOWING =
FROM key1 to key2 ************* --&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:variable name=3D&quot;uniqNodes&quot=
; select=3D&quot;$tree2/*[generate-id() =3D generate-id( key( 'key1', text(=
)))]&quot;/&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;!-- ************************************=
************************** --&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:for-each select=3D&quot;$uniqNodes&q=
uot;&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;xsl:message&gt;&=
lt;xsl:value-of select=3D&quot;local-name()&quot;/&gt;=3D&lt;xsl:value-of s=
elect=3D&quot;text()&quot;/&gt;&lt;/xsl:message&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/xsl:for-each&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&nbsp; &lt;/xsl:template&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
<o:p>&nbsp;</o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-family:&quot;Courier New&quot;">=
&lt;/xsl:stylesheet&gt;<o:p></o:p></span></p>
<p class=3D"MsoNormal">=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<o:p></o:p><=
/p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal">Thanks for any help,<o:p></o:p></p>
<p class=3D"MsoNormal">-kurt<o:p></o:p></p>
<p class=3D"MsoNormal">(This is my first post to the user-group so forgive =
me if I&#8217;m not following proper protocol)<o:p></o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
<p class=3D"MsoNormal"><span style=3D"font-size:9.0pt;font-family:&quot;Ari=
al&quot;,&quot;sans-serif&quot;;color:#25282A"><br>
<b>Kurt Hall</b><br>
Principal Engineer<o:p></o:p></span></p>
<p class=3D"MsoNormal" style=3D"mso-margin-top-alt:9.0pt;margin-right:0in;m=
argin-bottom:9.0pt;margin-left:0in">
<b><span style=3D"font-size:9.0pt;font-family:&quot;Arial&quot;,&quot;sans-=
serif&quot;;color:#0072CE">Truven Health Analytics<o:p></o:p></span></b></p=
>
<p class=3D"MsoNormal" style=3D"mso-margin-top-alt:auto;mso-margin-bottom-a=
lt:auto"><span style=3D"font-size:9.0pt;font-family:&quot;Arial&quot;,&quot=
;sans-serif&quot;;color:#25282A">Phone:
</span><span style=3D"font-size:9.0pt;font-family:&quot;Arial&quot;,&quot;s=
ans-serif&quot;;color:#666666">303-486-9161</span><span style=3D"font-size:=
9.0pt;font-family:&quot;Arial&quot;,&quot;sans-serif&quot;;color:#25282A"><=
o:p></o:p></span></p>
<p class=3D"MsoNormal" style=3D"mso-margin-top-alt:9.0pt;margin-right:0in;m=
argin-bottom:9.0pt;margin-left:0in">
<span style=3D"font-size:9.0pt;font-family:&quot;Arial&quot;,&quot;sans-ser=
if&quot;;color:#0072CE">[email protected]<br>
truvenhealth.com<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:9.0pt;font-family:&quot;Ari=
al&quot;,&quot;sans-serif&quot;;color:#25282A">The Healthcare Business of T=
homson Reuters is now
<br>
Truven Health Analytics. <o:p></o:p></span></p>
<p class=3D"MsoNormal"><o:p>&nbsp;</o:p></p>
</div>
</body>
</html>

--_000_5382f7a88aed42998d170a118d7eadafBY2PR07MB089namprd07pro_--