次のスタイルシートを試してください。xsl:for-each 要素を削除したので、この方法の方が簡単だと思います。また、XSL のような宣言型言語で foreach ループを使用するのは適切ではないように思えます。私はそれらを命令型言語に残すことを好みます。
このような結果を得るには、さまざまな方法があります。これを修正して少し実験するのに時間がかかるはずです。演習として、if ステートメントを削除し、テンプレートと述語を使用して同様の結果を達成することを試みることができます。そうする前に、おそらく XSL に関するいくつかのチュートリアルを読む必要があります。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Copy every element or attribute encountered
and find matching templates for its attributes
and child elements
-->
<xsl:template match="@*|*">
<xsl:copy>
<xsl:apply-templates select="@*|*"></xsl:apply-templates>
</xsl:copy>
</xsl:template>
<!-- For every attribute named "color" that has a value of red or blue,
follow the conditions defined in if blocks.
Notice that the specified color attributes will not be copied according
to the template above as the one selected is always the last
matching one in your XSL.
This way both the "author" attributes and "color" attributes with values
different than red and blue will be matched by the other template.
The dot "." means the currently processed node (usually element or attribute)
-->
<xsl:template match="@color[. = 'blue' or . = 'red']">
<xsl:attribute name="color">
<xsl:if test=". = 'blue'">yellow</xsl:if>
<xsl:if test=". = 'red'">pink</xsl:if>
</xsl:attribute>
</xsl:template>