tags:

views:

37

answers:

2

I want to check if the node <Type> is either "Debit" or "Credit"

so that I can transform the information from just credit card information into Debit or Credit transactions.

any suggestion????

+2  A: 

The element xsl:if is for "if A do B else do nothing". Use xsl:choose (with xsl:when and xsl:otherwise) for "if A do B else do C". Otherwise we do need a more specific example of what you mean.

Kathy Van Stone
Thank you <xsl:choose><xsl: when test="contains(Type,'Debit')></xsl:when><xsl:otherwise></xsl:otherwise></xsl:choose> It worked perfectly thanks for all the comments and answers even though my qustion wasnt that descriptive (sorry).
gary A.K.A. G4
+1  A: 

I particularly like using xsl:choose in most situations. It provides the most flexibility. I also would use a variable outside the template for type.

Variable code (belongs outside templates):

<xsl:variable name="$type">
    <xsl:value-of select="//type" />
</xsl:variable>

xsl:choose code (belongs in a template):

<xsl:choose>
    <xsl:when test="$type='credit'">
        <xsl:text>Type is credit card</xsl:text>
    </xsl:when>
    <xsl:when text="$type='debit'">
        <xsl:text>Type is debit card</xsl:text>
    </xsl:when>
    <xsl:otherwise>
        <xsl:text>Type is neither debit or credit card</xsl:text>
    </xsl:otherwise>
</xsl:choose>

Hope this helped :)

developer
Couldn't you shorten that code to <xsl:when test="attribute::Type[text()='Credit']"> or somesuch? In my experience, XSL interprets an empty node-set as false and a node-set of 1+ as true. This may not be reliable however.
Tom W
@Tom W: I like to use variables.. this is just my opinion: I see it as better coding practices. This is because if you need to use this text a thousand times, but all of a sudden a part of the path needs to change, it only needs to be changed once in the variable, instead of a thousand times in all the places the test was used. No? Interested in what you think.
developer
That's a fair counterargument. It would depend on the context it was used; I would naturally assume that for very many repeated operations, using variables would slow down the XSL processing engine; but your argument for reusability is a good one. If it was used in a lot of different places then a variable is probably a better choice. I tend to prefer brevity where it's appropriate.
Tom W
@Tom W: How much do you suppose it would slow down the processing engine? Just curious for future coding of xsl :)
developer