hello first question on stackoverflow :-)
i've experienced strange behaviour xsl:key function in xslt, wich bit hard explain, easy demonstrate.
when i'm indexing 2 different identical nodesets xsl:key element, fails index them properly.
in test example want know how many cells have been indexed each table. here first input 2 identical tables (exept @id):
<?xml version="1.0" encoding="utf-8"?> <test> <table id="table1"> <cell>cell 1</cell> <cell>cell 2</cell> <cell>cell 3</cell> </table> <table id="table2"> <cell>cell 1</cell> <cell>cell 2</cell> <cell>cell 3</cell> </table> </test> then second input different content second table (first cell contains "cell_1" underscore):
<?xml version="1.0" encoding="utf-8"?> <test> <table id="table1"> <cell>cell 1</cell> <cell>cell 2</cell> <cell>cell 3</cell> </table> <table id="table2"> <cell>cell_1</cell> <cell>cell 2</cell> <cell>cell 3</cell> </table> </test> here xslt. i'm counting each table element, number of cells sharing same current parent::table.
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:key name="cell" match="//cell" use="parent::table"/> <xsl:template match="//test"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> <xsl:template match="table"> <numcells id_table="{@id}"> <xsl:value-of select="count(key('cell', .))"/> </numcells> </xsl:template> </xsl:stylesheet> and here first output 2 identical tables. shows 6 cells in each table instead of 3.
<?xml version="1.0" encoding="utf-8"?> <test> <numcells id_table="table1">6</numcells> <numcells id_table="table2">6</numcells> </test> and second output 2 different tables. shows right number of 3 cells each table.
<?xml version="1.0" encoding="utf-8"?> <test> <numcells id_table="table1">3</numcells> <numcells id_table="table2">3</numcells> </test> it may related xslt processor, i've tested saxon, xalan , xsltproc same result.
i've seen around problem using @id:
<xsl:key name="cell" match="//cell" use="parent::table/@id"/> and then:
<xsl:value-of select="count(key('cell', @id))"/> but i'm still wondering causing behaviour. explanations!
a key value primitive value string or number, not node itself. if want key on node identity use use="generate-id(parent::table)".
your current key string value of table element , concatenation of text descendant nodes first sample key values like
cell 1 cell 2 cell 3 you want group or key based on identity of node, not based on string contents. use generate-id or use id attribute present on table elements, have found out.
Comments
Post a Comment