例えばXMLにてリンクの設定をしたとする。大抵はhref属性は書くが、titleであったりrelは必要に応じてだけしか書かないことも多い。今までxslにて
<xsl:template match='ulink'>
<a>
<xsl:attribute name="href">
<xsl:value-of select="@url" />
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="@urltitle" />
</xsl:attribute>
<xsl:attribute name="rel">
<xsl:value-of select="@urlrel" />
</xsl:attribute>
<xsl:apply-templates/>
</a>
</xsl:template>
とこのようにあってもなくても処理するように書いていたのだが、それでは空でtitleなどくっつくのがどうにもみっともないので xsl:if で以下のようにした。
<xsl:template match='ulink'>
<a>
<xsl:attribute name="href">
<xsl:value-of select="@url" />
</xsl:attribute>
<xsl:if test="@urltitle!=''">
<xsl:attribute name="title">
<xsl:value-of select="@urltitle" />
</xsl:attribute>
</xsl:if>
<xsl:if test="@urlrel!=''">
<xsl:attribute name="rel">
<xsl:value-of select="@urlrel" />
</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</a>
</xsl:template>
属性でも使えるのかどうかわからなかったんで今まで使わなかったのだがテストしてみればちゃんとxsl:ifで回避してくれることがわかった。なんて単純なことだ...orz。