Re: [xsl] Test for presence of attribute

Subject: Re: [xsl] Test for presence of attribute
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Wed, 18 Jul 2001 17:26:17 +0100
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:


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>

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>

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 Tennison

 XSL-List info and archive:

Current Thread