On Tuesday 12 March 2002 02:29, Hugo Ferreira wrote:
> Hi,
>
> This may be a little off topic, but can anyone tell me how I may do the
> following:
> I would like to obtain a node's text value. If the value is "" the I would
> like to output
> NULL otherwise I would like to use the node's value.
>
> So far I have:
>
> <xsl:variable name="sa">
>   <xsl:if test="not(string(sales_agent))">NULL</xsl:if>
>   <xsl:if test="string(sales_agent)"><xsl:value-of
> select="sales_agent"/></xsl:if>
>  </xsl:variable>
>

First of all, you should use <xsl:choose> instead of two <xsl:if>'s here (not 
really a problem, but it looks prettier and is more efficient):

<xsl:variable name="sa">
  <xsl:choose>
    <xsl:when test="not(string(sales_agent))">NULL</xsl:when>
    <xsl:otherwise><xsl:value-of select="sales_agent"/></xsl:otherwise>
  </xsl:choose>
</xsl:variable>

> But how may I output the variable's value? It seems I can only do this as
> an element attribute.

I'm not sure what the problem is.  You just do:

<xsl:value-of select="$sa"/>

Or, if you want to use it as the value of an element's attribute, do 
something like

<theElement someAttribute="{$sa}">...</theElement>

If you are outputting as an attribute, if you want you can also skip the 
variable and output the attribute like this:

<theElement>
  <xsl:attribute name="someAttribute">
    <xsl:choose>
      <xsl:when test="not(string(sales_agent))">NULL</xsl:when>
      <xsl:otherwise><xsl:value-of select="sales_agent"/></xsl:otherwise>
    </xsl:choose>
  </xsl:attribute>
  ... other stuff inside theELement ...
</theElement>

You can use xsl:attribute to dynamically create an attribute in any element, 
so long as xsl:attribute is the first thing in that element other than 
whitespace.  You can even dynamically change the name of the attribute using 
<xsl:attribute name="{$theName}">.

Hope that helps, let me know if I was confused by your question.

>
> BTW, can anyone point me to a mailing list were such questions may be
> answered?

xsl-list seems to be the most popular, and you can usually get your questions 
answered within an hour or less.  Lots of smart people hang out there, 
including a lot of the people who are actually responsible for XSLT.  Go to 
http://www.mulberrytech.com/xsl/xsl-list/subscribe-unsubscribe.html

>
> TIA.
> Hugo.

-- 
Peter Davis
"I DO want your money, because god wants your money!"
-- The Reverend Jimmy, from _Repo_Man_

Reply via email to