I have this XML code
<data>
<proteins>
<protein>
<accession>111</accession>
</protein>
</proteins>
<peptides>
<peptide>
<accession>111</accession>
<sequence>AAA</sequence>
</peptide>
<peptide>
<accession>111</accession>
<sequence>AAA</sequence>
</peptide>
<peptide>
<accession>111</accession>
<sequence>AAA</sequence>
</peptide>
<peptide>
<accession>111</accession>
<sequence>BBB</sequence>
</peptide>
<peptide>
<accession>111</accession>
<sequence>BBB</sequence>
</peptide>
<peptide>
<accession>111</accession>
<sequence>BBB</sequence>
</peptide>
<peptide>
<accession>111</accession>
<sequence>BBB</sequence>
</peptide>
</peptides>
</data>
and this XSLT code
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="byAccSeq" match="peptide"
use="concat(accession, '|', sequence)"/>
<xsl:template match="/">
<root><xsl:apply-templates select="/*/proteins/protein"/></root>
</xsl:template>
<xsl:template match="protein">
<xsl:apply-templates
select="../../peptides/peptide[accession=current()/accession]"/>
</xsl:template>
<xsl:template match="peptide[generate-id()=
generate-id(key('byAccSeq', concat(accession, '|', sequence))[1])]">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="peptide"/>
</xsl:stylesheet>
The output is this one
<root>
<peptide>
<accession>111</accession>
<sequence>AAA</sequence>
</peptide>
<peptide>
<accession>111</accession>
<sequence>BBB</sequence>
</peptide>
</root>
Now, the same XSLT code but with almost all paths changed to absolute
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="byAccSeq" match="/data/peptides/peptide"
use="concat(accession, '|', sequence)"/>
<xsl:template match="/">
<root><xsl:apply-templates select="/data/proteins/protein"/></root>
</xsl:template>
<xsl:template match="/data/proteins/protein">
<xsl:apply-templates
select="/data/peptides/peptide[accession=current()/accession]"/>
</xsl:template>
<xsl:template match="/data/peptides/peptide[generate-id()=
generate-id(key('byAccSeq', concat(accession, '|', sequence))[1])]">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="peptide"/>
</xsl:stylesheet>
does not change anything. However, if the last path is also expressed as absolute
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="byAccSeq" match="/data/peptides/peptide"
use="concat(accession, '|', sequence)"/>
<xsl:template match="/">
<root><xsl:apply-templates select="/data/proteins/protein"/></root>
</xsl:template>
<xsl:template match="/data/proteins/protein">
<xsl:apply-templates
select="/data/peptides/peptide[accession=current()/accession]"/>
</xsl:template>
<xsl:template match="/data/peptides/peptide[generate-id()=
generate-id(key('byAccSeq', concat(accession, '|', sequence))[1])]">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="/data/peptides/peptide"/>
</xsl:stylesheet>
then the ouput is simply
<root></root>
I did not expect this.
And, also unexpectedly (for me) if I write the template match before, the output is again the wished one
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="byAccSeq" match="/data/peptides/peptide"
use="concat(accession, '|', sequence)"/>
<xsl:template match="/">
<root><xsl:apply-templates select="/data/proteins/protein"/></root>
</xsl:template>
<xsl:template match="/data/proteins/protein">
<xsl:apply-templates
select="/data/peptides/peptide[accession=current()/accession]"/>
</xsl:template>
<xsl:template match="/data/peptides/peptide"/>
<xsl:template match="/data/peptides/peptide[generate-id()=
generate-id(key('byAccSeq', concat(accession, '|', sequence))[1])]">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
The last files: http://www.xsltcake.com/slices/sgWUFu
I would like to understand the logic behind this.
As @Michael alluded, the issue is one of template conflict resolution. Changing the match pattern from peptide
to /data/peptides/peptide
raises the priority of the template to the same level as the template for handling /data/peptides/peptide[generate-id()=
generate-id(key('byAccSeq', concat(accession, '|', sequence))[1])]
. There are nodes in the document that could be selected by either/both of those match patterns, which results in a template conflict.
It is technically an error to have more than one matching template rule, but processors generally recover by applying the last matching template in the document, which is why changing the order of the templates produced different results.
As Michael would well know, Saxon produces the following warning:
Recoverable error
XTRE0540: Ambiguous rule match for /data/peptides[1]/peptide[1]
Matches both "/data/peptides/peptide" on line 16 of file:///C:/sandbox/so.xsl
and "/data/peptides/peptide[generate-id()= generate-id(key('byAccSeq', concat(accession,
'|', sequence))[1])]" on line 13 of file:///C:/sandbox/so.xsl
Recoverable error
XTRE0540: Ambiguous rule match for /data/peptides[1]/peptide[4]
Matches both "/data/peptides/peptide" on line 16 of file:///C:/sandbox/so.xsl
and "/data/peptides/peptide[generate-id()= generate-id(key('byAccSeq', concat(accession,
'|', sequence))[1])]" on line 13 of file:///C:/sandbox/so.xsl