This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Test for presence of attribute


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]