この短くて完全に「プッシュ スタイル」の変換:
<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="cd/node()"/>
<xsl:template match="cd[position() >= 3 and 4 >= position()]/title">
<xsl:copy><xsl:apply-templates/></xsl:copy>
</xsl:template>
</xsl:stylesheet>
提供された XML ドキュメントに適用した場合:
<catalog>
<cd>
<title>Empire Burlesque</title>
</cd>
<cd>
<title>Hide your heart</title>
</cd>
<cd>
<title>Greatest Hits</title>
</cd>
<cd>
<title>Still got the blues</title>
</cd>
</catalog>
必要な正しい結果が生成されます。
<title>Greatest Hits</title>
<title>Still got the blues</title>
説明:
空のテンプレート<xsl:template match="cd/node()"/>
は、 の子の処理 (「削除」) を防ぎcd
ます。
2 番目のテンプレートは、3 以上 4 以下の a の子に対してのみ、最初のテンプレートをオーバーライドします。これは、一致した要素を効果的にコピーtitle
します。cd
position()
title
この<xsl:strip-space elements="*"/>
命令は、XML ドキュメントからすべての空白のみのテキスト ノードを削除することで、これらすべてを可能にします。このようにcd
して、命令によって形成されたノードリスト<xsl:apply-templates>
(要素の組み込み XSLT テンプレート) 内の要素の位置は、2、4、6、8 ではなく、1、2、3、4 になります。