xml conditional if statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joe2k1
    New Member
    • Nov 2008
    • 3

    xml conditional if statement

    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="Ca nada"
    if fld_City="Toron to"
    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"Can ada"
    if fld_City="Toron to"
    if fld_Member="2"
    display the following text, "blah blah."

    if fld_country"Can ada"
    if fld_City="Ottaw a"
    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:fl d_COUNTRY='Cana da'?>?fld_COUNT Y_DESCR?><?end if?>
    </code>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.
    Code:
    <xsl:template match="element[fld_Country='Canada'][fld_City='Toronto'][fld_Member='1']">
    // note that the expression depends on your input xml
    <xsl:text>bla bla blubb</xsl:text>
    </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

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      Is the code actually formed like so?
      <code>
      <?if@inlines:fl d_COUNTRY='Cana da'?>?fld_COUNT Y_DESCR?><?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.

      Comment

      • joe2k1
        New Member
        • Nov 2008
        • 3

        #4
        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.

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          You have a nested when. If you want to use a nested when you need to have to have a nested choose with it.
          Code:
          <choose>
            <when>
                <choose>
                    <when>

          Comment

          • joe2k1
            New Member
            • Nov 2008
            • 3

            #6
            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?

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              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.

              Comment

              Working...