この変換:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
<flag><xsl:value-of select=
"substring('1', 2 - not(child[compare != current()/child/compare]))"/></flag>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
次の XML ドキュメントに適用した場合:
<parent>
<child>
<compare>1</compare>
</child>
<child>
<compare>1</compare>
</child>
</parent>
必要な正しい結果が生成されます。
<parent>
<child>
<compare>1</compare>
</child>
<child>
<compare>1</compare>
</child>
<flag>1</flag>
</parent>
この XML ドキュメントに同じ変換を適用すると、次のようになります。
<parent>
<child>
<compare>1</compare>
</child>
<child>
<compare>2</compare>
</child>
</parent>
ここでも、必要な正しい結果が生成されます。
<parent>
<child>
<compare>1</compare>
</child>
<child>
<compare>2</compare>
</child>
<flag/>
</parent>
注意してください:
アイデンティティ ルールの使用とオーバーライド。
明示的な条件付き命令 (または変数) はまったく使用されません。