This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Test for presence of attribute
- To: Nathan Shaw <n8_shaw at yahoo dot com>
- Subject: Re: [xsl] Test for presence of attribute
- From: Jeni Tennison <mail at jenitennison dot com>
- Date: Wed, 18 Jul 2001 17:26:17 +0100
- CC: XSL-List at lists dot mulberrytech dot com
- Organization: Jeni Tennison Consulting Ltd
- References: <20010718161342.71554.qmail@web9801.mail.yahoo.com>
- Reply-To: xsl-list at lists dot mulberrytech dot com
Hi Nathan,
> BUT, URL is not required and if it is not present, then the IF
> statement fails. I need something like IsDefined() or
> AttributeExists()...
You can test for the presence of any node by trying to select it and
converting the result to a boolean. So to test for the attribute @URL
being present, you can use:
boolean(@URL)
If you're doing this in a context where the expression will be
converted to a boolean anyway, then you don't even have to worry about
the call to boolean(). Just use:
<xsl:if test="@URL">
<a href="{@URL}"><xsl:value-of select="@Title" /></a>
</xsl:if>
Alternatively, apply templates to the URL attribute:
<xsl:apply-templates select="@URL" />
and have a template that creates the result that you want from it:
<xsl:template match="@URL">
<a href="{.}"><xsl:value-of select="../@Title" /></a>
</xsl:template>
If the XSLT processor doesn't find a node to apply templates to, then
no template gets applied.
> Also, is there a len() function? I would rather use something like
> <xsl:if test="len(@URL)">...
Try string-length().
I hope that helps,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list