i have a problem with this statement :
<xsl:choose>
<xsl:when test="cars[#id='1']">
<xsl:choose>
<xsl:when test="cars[#id='1']='1'">true</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
true
</xsl:otherwise>
</xsl:choose>
"True" is allways displayed even if my "cars[#id='1'])='0' (Not exists).
Thanks for Help
Could it be that your input isn't matching at all?
<xsl:choose>
<xsl:when test="cars[#id='1']">
<xsl:choose>
<xsl:when test="cars[#id='1']='1'">true</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
true <!-- change this and see what it returns -->
</xsl:otherwise>
</xsl:choose>
From the information given, there could be any number of reasons. Common beginner's mistakes would suggest the following possibilities:
(a) the 'cars' element is actually in a namespace
(b) your context node for executing this code is not the parent of the 'cars' element
Related
I'm trying to built some rules around a calculated variable value, but am struggling to get the rules to work with an output of false being returned if either or both criteria are used. I need to use both qualifying criteria, but in the process of bug testing tried each argument individually.
I have a variable called "calculation". However when a discount amount is applied to it, I do not want the value of the calculation to be below 301.12. This however is only the case when a discount amount has been applied. If the value of the calculation is below 301.12 without a discount then this is OK, and the lower value is acceptable.
What is happening however is the the lower value after the discount has been applied is still being returned when the process is run.
All help is greatly appreciated.
Here is my code:
<xsl:variable name="discountAmount">
<xsl:choose>
<xsl:when test="#affiliateID='12345'">50</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="feeAmount">
<xsl:choose>
<xsl:when test="#affiliateID='12345'">25</xsl:when>
<xsl:otherwise>50</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="grossAmount" select="format-number(db1:grossPremium, '#0.00')" />
<xsl:variable name="discountGiven" select="($grossAmount - $feeAmount) div 100 * $discountAmount" />
<xsl:variable name="calculation" select="($grossAmount - $discountGiven)" />
<xsl:choose>
<xsl:when test="$discountAmount > 0">
<xsl:choose>
<xsl:when test="$calculation < 301.12">
<xsl:value-of select="$calculation=301.12" />
</xsl:when>
</xsl:choose>
</xsl:when>
</xsl:choose>
You cannot change values of variables in XSLT. Try it like this:
<xsl:variable name="calc0" select="($grossAmount - $discountGiven)" />
<xsl:variable name="calculation">
<xsl:choose>
<xsl:when test="$discountAmount > 0 and $calc0 < 301.12">
301.12
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$calc0" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
i am trying to do equal comparison of negative number, but i dont see any output. below is the code.
<xsl:variable name="OwnershipStatus" select="Veh_Ownsp[Veh_Ownsp_ID=$OwnerData1/Veh_Ownsp_ID]/Ownsp_Cntl_Type_ID"/>
Here OwnershipStatus is returninenter code hereg -1
xsl:choose>
<xsl:when test="$OwnershipStatus = -1" >
<xsl:value-of select="Or"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="And"/>
</xsl:otherwise>
</xsl:choose>
I guess you are trying to hardcode 'Or' and 'And' in case of condition getting evaluated to true and false, respectively. Since the values are literal they should be enclosed with single quotes as shown below:
<xsl:choose>
<xsl:when test="$OwnershipStatus = -1" >
<xsl:value-of select="'Or'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'And'"/>
</xsl:otherwise>
My code involves two choose statements, to quickly execute, which is the right tag, where I am having nearly 50 conditions of the same format.
<xsl:choose><!--Individual whens-->
<xsl:when test="starts-with(., 'Kishan')">
<b><xsl:apply-templates/></b>
</xsl:when>
<xsl:when test="starts-with(., 'Gagan')">
<b><xsl:apply-templates/></b>
</xsl:when>
<xsl:when test="starts-with(., 'Likhith')">
<b><xsl:apply-templates/></b>
</xsl:when>
<xsl:when test="starts-with(., 'Kowshik')">
<b><xsl:apply-templates/></b>
</xsl:when>
</xsl:choose>
<xsl:choose><!--grouped when-->
<xsl:when test="starts-with(., 'Kishan') or starts-with(., 'Gagan') or starts-with(., 'Likhith') or starts-with(., 'Kowshik')">
<b><xsl:apply-templates/></b>
</xsl:when>
</xsl:choose>
At least in theory, both procedures should exit at the first true result - so if you want to optimize the performance, test the most common values first.
Is it possible to check the type of a node I matched with a template inside the same template? In case it is, how can I do it? For example I would like to do something like this:
<xsl:template match="#*|node()">
<xsl:choose>
<xsl:when test="current() is an attribute">
<!-- ... -->
</xsl:when>
<xsl:when test="current() is an element">
<!-- ... -->
</xsl:when>
<xsl:otherwise>
<!-- ... -->
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Take a look at this answer here, as this should give you the information you need:
Difference between: child::node() and child::*
This gives the following xsl:choose to test all the nodes, including the document node.
<xsl:choose>
<xsl:when test="count(.|/)=1">
<xsl:text>Root</xsl:text>
</xsl:when>
<xsl:when test="self::*">
<xsl:text>Element </xsl:text>
<xsl:value-of select="name()"/>
</xsl:when>
<xsl:when test="self::text()">
<xsl:text>Text</xsl:text>
</xsl:when>
<xsl:when test="self::comment()">
<xsl:text>Comment</xsl:text>
</xsl:when>
<xsl:when test="self::processing-instruction()">
<xsl:text>PI</xsl:text>
</xsl:when>
<xsl:when test="count(.|../#*)=count(../#*)">
<xsl:text>Attribute</xsl:text>
</xsl:when>
</xsl:choose>
I highly recommend you to use the expressions on sequence types introduced in XPath 2.0. For example:
. instance of document-node()
. instance of element()
A more precise way to determine if the node $node is a root node:
not(count($node/ancestor::node()))
The expression in TimC's answer tests the type of the current node:
count(.|/)=1
but isn't applicable in the case when we want to determine the type of a node in a variable -- which may belong to another document -- not to the current document.
Also, a test for a namespace node:
count($node | $node/../namespace::*) = count($node/../namespace::*)
I have another simple xsl variable question. I'm trying to evaluate an expression and toggle an 'AM' or 'PM' suffix. The variable never evaluates to anything. I've even changed my test to with no luck.
<xsl:variable name="DisplayAMPM">
<xsl:choose>
<xsl:when test="number(substring($LastBootUpTime, 9,2))>11">
<xsl:value-of select="PM"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="AM"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:copy-of select="DisplayAMPM"/>
If you use value-of, put the "AM" and "PM" in quotes so that the processor sees it as a string.
Also, if you reference the variable, like you're trying to do in the copy-of, don't forget the $.
<xsl:variable name="DisplayAMPM">
<xsl:choose>
<xsl:when test="number(substring($LastBootUpTime, 9,2))>11">
<xsl:value-of select="'PM'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'AM'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:copy-of select="$DisplayAMPM"/>
You have a runaway > character in your test attribute which should of course be >.
Secondly, you don't copy your variable ($DisplayAMPM), instead you copy the (nonexistent?) DisplayAMPM element child node set.