266 11 The XML Transformation Language
...
<!-- Change all occurrences of Protein to P -->
<xsl:template match="Protein">
<xsl:sort select="@id"/>
<P>
<xsl:apply-templates select="@*|node()"/>
</P>
</xsl:template>
<!-- Change all occurrences of Substrate to S -->
<xsl:template match="Substrate">
<xsl:sort select="@id"/>
<S>
<xsl:apply-templates select="@*|node()"/>
</S>
</xsl:template>
...
Transform 11.2 A modification of the program in transform 11.1 in which the pro-
teins and substrates have been sorted by their ids
read a novel. This order is called thedocument order. However, the order in
which elements are selected during the transformation can be changed by
using axsl:sortelement. In transform 11.2 a transformation is performed
that not only changes some element names but also changes the order of
those elements.
Theapply-templatescommand serves to change the context of the
transformation from one element or attribute to another one. Thefor-each
is another command that accomplishes the same effect. The only difference
between them is thatapply-templatescauses another template to be-
come active in a new context while thefor-eachcommand stays inside
the same template. This is illustrated in transform 11.3 which changes the
tag ofinteractionelements withinProteinelements toI.
While bothapply-templatesandfor-eachhave the same effect, there
are some differences. Thefor-eachcommand is a traditional technique for
controlling the actions performed by a computer program, and those who
have programming experience will find it a familiar command. By contrast,
apply-templatesis a rule-based command that uses a matching or “lock-
and-key” mechanism which is much more flexible and powerful.
The power of theapply-templatesrule-based command is illustrated
by transform 11.2. In this program, child elements of aProteinother than