Connecting Tech Pros Worldwide Help | Site Map

xml conditional if statement

Newbie
 
Join Date: Nov 2008
Posts: 3
#1: Nov 11 '08
Hello everyone,

I have a question about if statements with xml. I need to ensure certain fields have a particular value, if this is true I want to display a large amount of text. Let me give an example below:

if fld_Country="Canada"
if fld_City="Toronto"
if fld_Member="1"
display the following text, "blah blah."

if fld_country"USA"
if fld_city="New York"
if fld_member="2"
display the following text, "blah blah."

if fld_country"Canada"
if fld_City="Toronto"
if fld_Member="2"
display the following text, "blah blah."

if fld_country"Canada"
if fld_City="Ottawa"
display the following text, "blah blah."

end if statements

I am not certain if this is possible to nest so many if statements within a single if statement. Should I use choices? Any advice would be greatly appreicated. Thanks.
Joe.

<code>
<?if@inlines:fld_COUNTRY='Canada'?>?fld_COUNTY_DES CR?><?end if?>
</code>
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#2: Nov 11 '08

re: xml conditional if statement


what you want to do seems a bit strange. xml (with some exceptions) is used to store data (statically store, like in a DB).

any condition would need someone/something (that someone is usually called "parser") that actually decides if the condition is fulfilled or not. an xml parser doesn't do that.

in your case I recommend using a script that processes an input giving you the desired xml file.
- xslt (needs xml, returns xml or text)
- any programming language

for the case of xslt your “weapon” of choice is a conditional XPath expression, e.g.
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="element[fld_Country='Canada'][fld_City='Toronto'][fld_Member='1']">
  2. // note that the expression depends on your input xml
  3. <xsl:text>bla bla blubb</xsl:text>
  4. </xsl:template>
in any case I recommend reading a tutorial first, if you're not accustomed to the language you want to use.

XSLT tutorial

if you encounter any problems post back so we can help you getting it right.

regards
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#3: Nov 12 '08

re: xml conditional if statement


Is the code actually formed like so?
<code>
<?if@inlines:fld_COUNTRY='Canada'?>?fld_COUNTY_DES CR?><?end if?>
</code>

Is the fact that the last query has no Member signficant?

It really depends on how you're going to use your code.
Newbie
 
Join Date: Nov 2008
Posts: 3
#4: Feb 6 '09

re: xml conditional if statement


Here is a quick follow up.

It would appear I am using XSLT processor.

I need to make certain two conditionals are true for a statement to print.

Unfortunately, I am having one the first statement found to be true and the message is printed!

<?choose:?>
<?when:GENDER='MALE'?>
<?when:AGE='30'?>
Please note that you have been selected to participate in a survery for men who are age 30 this year.
<?end when?>
<?end when?>
<?end choose?>

Here is the error message when I tried to process this syntax


<Line 103, Column 63>: XML-22047: (Error) Invalid instantiation of 'xsl:when' in 'xsl:when' context.


I am unable to determine the exact line but I am 99% certain it relates to the above coding.

Any help or advice would be greatly appreciated!

Thank you.
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#5: Feb 6 '09

re: xml conditional if statement


You have a nested when. If you want to use a nested when you need to have to have a nested choose with it.
Expand|Select|Wrap|Line Numbers
  1. <choose>
  2.   <when>
  3.       <choose>
  4.           <when>
  5.  
Newbie
 
Join Date: Nov 2008
Posts: 3
#6: Feb 6 '09

re: xml conditional if statement


I tried the nesting, but unfortunately it didn't work. Here is what I tried below:

<choose>
<when>
<choose>
<when>
if both when conditions are true please show this message.
end when
end choose
end when
end choose

is that logic correct?
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#7: Feb 12 '09

re: xml conditional if statement


Does it work if you use nested ifs instead? Or nested ifs in the when?

I really have no idea as to what processes you're using to manufacture the code which manufactures the code.
Reply