Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

xsl:value-of

Status
Not open for further replies.

toolkit

Programmer
Aug 5, 2001
771
0
0
GB
I was wondering if anyone could explain how to achieve the following. In my XSL, I have a child element whose parent may or may not have specified a width attribute. I believe the following element will grab the value:
Code:
<xsl:value-of select=&quot;../@width&quot;/>
However, how do I assign a default value if the width attribute is missing?
Thanks for the replies, Neil :)
 
Something like this:

<xsl:choose>
<xsl:when test=&quot;../@width=''&quot;>
<xsl:variable name=&quot;width&quot;>
200
</xsl:variable>
</xsl:when>
<xsl:when test=&quot;../@width!=''&quot;>
<xsl:variable name=&quot;width&quot;>
<xsl:value-of select=&quot;../@width&quot;>
</xsl:when>
</xsl:choose>

Sets the variable 'width' to either 200 (if no width is specified) or the value of @width (if it is specified).

Hope this helps! Nick (Software Developer)


nick@retrographics.fsnet.co.uk
nick.price@myenable.com
 
another way of doing this is with the string-length function. i have found that sometimes comparing an attribute to an empty string returns false if it is null and true if it is an empty string. the string-length returns false if the attribute is missing or if the attribute is &quot;&quot;. so my personal preference has been to do this

<xsl:choose>
<xsl:when test=&quot;string-length(../@width)>0&quot;>
<xsl:variable name=&quot;width&quot; select=&quot;../@width&quot; />
</xsl:when>
<xsl:eek:therwise>
<xsl:variable name=&quot;width&quot;>
<xsl:variable name=&quot;width&quot;>200</xsl:variable>
</xsl:eek:therwise>
</xsl:choose> mike griffith
----------------------------
mgriffith@lauren.com
mdg12@po.cwru.edu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top