Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    XML with if's , would this work?

    I am converting some formulas from XML to another language and have run across this. I am not XML expert, but I think there is a problem in the code I was given. Could this line ever be even be checked:

    <elseif condition="[calc] &lt; 75">

    * Also, please assume these variables must always be positive numbers.
    Code:
    <element value="P">
    			<commodity value="Car">
    				<equation>
    					<input>
    						<database_variables>
    							<variable name="P"/>
    							<variable name="Q"/>
    						</database_variables>
    					</input>
    					<output>
    						<if condition="[P]&lt; 20.00">
    							<variable name="calc" formula="[Q]+([P]*.85)"/>
    							<if condition="[P] &gt;= 0 and [P]  &lt; 10">
    								<result formula="[calc]"/>
    							</if>
    							<elseif condition=" [P] &gt;= 10 and [P] &lt; 20">
    								<result formula="75"/>
    							</elseif>
    							<elseif condition="[calc] &lt; 75">
    								<result formula="75"/>
    							</elseif>
    							<else>
    								<result formula="0"/>
    							</else>
    						</if>
    						<else>
    							<result formula="0"/>
    						</else>
    					</output>
    				</equation>
    			</commodity>
    		</element>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,105
    Thanks
    23
    Thanked 594 Times in 593 Posts
    Is the xml correct: Yes.
    Is the line <elseif condition="[calc] &lt; 75"> correct: Yes.
    Would this condition ever occur: Yes.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    dcdon (07-30-2013)

  • #3
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Sunfighter,

    Thanks for the response.

    My confusion is this:

    you have the main if that says if (P<20) then inside of that you have:

    if (p<10) - do this
    elseif(p>=10 and P<20) Do this.
    (then you have the 3rd statement which is the one in question. )
    elseif(x<75) Do this

    But If P is positive and is between zero and 20 , then wouldn't it get caught in one of the first two conditions and therefore break out before it ever saw the 3rd? I can think of no condition in which it would get there. Unless I understand XML else/if's wrong, which could be the case.

    If you do understand how it works, could you please give me an example of input that would make it work. Or even better write it out in php, where I better understand.

    Thanks a ton.

  • #4
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,300
    Thanks
    28
    Thanked 275 Times in 269 Posts
    Quote Originally Posted by dcdon View Post
    Unless I understand XML else/if's wrong, which could be the case.
    There's no such thing as "XML else/if". This is just arbitrary information that someone coded into XML that has no functionality without another language (like XSLT) putting it to use.

    Quote Originally Posted by dcdon View Post
    But If P is positive and is between zero and 20 , then wouldn't it get caught in one of the first two conditions and therefore break out before it ever saw the 3rd? I can think of no condition in which it would get there.
    If I make some assumptions about the logic and translate that to JavaScript, I get this:

    Code:
    if (p < 20) {
    	if (p >= 0 && p < 10) {
    		// Do something.
    	}
    	else if (p >= 10 && p < 20) {
    		// Do something.
    	}
    	else if (p < 75) {
    		// Do something.
    	}
    	else {
    		// Do something.
    	}
    else {
    	// Do something.
    }
    This code indicates that the variable p will only be processed by the outer statement if it is less than 20 including when it is negative.

    If the variable p is zero or positive, only one of the first two nested statements will be executed.

    If the variable p is negative then the p < 75 code will be executed.

    The last, nested else statement will never be executed.

    If you wanted to simplify this code, you would get:

    Code:
    if (p < 20) {
    	if (p >= 0 && p < 10) {
    		// Do something.
    	}
    	else if (p < 20) { // p is 10 or greater.
    		// Do something.
    	}
    	else { // p is negative.
    		// Do something.
    	}
    else { // p is 20 or greater or is not a number.
    	// Do something.
    }
    It might help comprehension to be explicit about everything, however:

    Code:
    if (p < 20) {
    	if (p >= 0 && p < 10) {
    		// Do something.
    	}
    	else if (p >= 10 && p < 20) {
    		// Do something.
    	}
    	else if (p < 0) {
    		// Do something.
    	}
    else if (p >= 20 || isNaN(p)) {
    	// Do something.
    }
    Last edited by Arbitrator; 07-30-2013 at 10:27 AM. Reason: I had to fix an inaccurate JavaScript comment.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • Users who have thanked Arbitrator for this post:

    dcdon (07-30-2013)

  • #5
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Arbitrator,

    Thanks. That's exactly how I was reading it. If it is positive (and for my purposes, it always will be), it can never execute that third or fourth statement there.

  • #6
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it can never execute that third or fourth statement there.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •