XSL choose or if??

Larry Brister
Larry Brister used Ask the Experts™
on
I have the following tag which is fine when there's data

This:
<xsl:value-of select="LicenseExpirationMonth1"/>/<xsl:value-of select="LicenseExpirationYear1"/>

Produced (Example) this:
01/2014

However...when there's no data I get the / mark...which I don;t want.

How do I wrap that to only show the / when there's data?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
<xsl:if test="normalize-space(LicenseExpirationMonth1)"><xsl:text>/</xsl:text></xsl:if>
Something like this should work

<xsl:if test="LicenseExpirationMonth1 and LicenseExpirationYear1">
<xsl:value-of select="LicenseExpirationMonth1"/>/<xsl:value-of select="LicenseExpirationYear1"/>
</xsl:if>

Open in new window

Larry Bristersr. Developer

Author

Commented:
Perfect!

Remind me to buy you a sody-pop sometime!
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
some notes
- normalize-space() I do that to cast the element to string and remove trailing whitespace (basically I do this to make sure that a single space would still return fale)
- why not test="not(normalize-space(LicenseExpirationMonth1) = '')"> ? Well, a string expression used as a boolean expression returns false if the string expression resolves to an empty string
- why xsl:if and not xsl:choose? You would use xsl:choose when there are multiple conditional branches or if you would have an "else" clause (which would be xsl:otherwise in the choose)
<xsl:if test="..."></xsl:if> is short for
<xsl:choose><xsl:when test="..."></xsl:when></xsl:choose>
(or xsl:if is short for an xsl:choose with only one xsl:when and no xsl:otherwise)
Larry Bristersr. Developer

Author

Commented:
Gertone...
Sorry.  Didn;t see your reply for some reason.  My apologies.
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
Ah, I did not spot the second value-of, good catch zc2
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
@Irbrister,
well, no problem,
though mine would have the desired effect (you likely don't want the '/' if the Month is not there and the year is) I consider zc2's solution more correct.
Most important is that you have an answer to your question :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial