Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ned help on XSLT sequence in the loop

Posted on 2009-07-06
18
Medium Priority
?
270 Views
Last Modified: 2013-11-18
If i send the input is
1.  Lunch and 'BreakFast'  the out put should be =
BreakFast
 lunch
2.  'BreakFast' and Lunch  the out put should be
 BreakFast
lunch
3.  Lunch and Dinner the out put should be
Lunch
Dinner
4.  Dinner and Lunch  the out put should be
Lunch
Dinner

I alway have 2 field in the input. I dont get a combination Breakfast and Dinner.

Thank you

<xsl:for-each select="meal">
                   	<xsl:if test="TYPE = 'BreakFast'">
		  <BeginDate>
			<xsl:value-of select="concat('_ ',TYPE)"/>
		   </BeginDate>
		</xsl:if>
		<xsl:if test="DTM01 = 'Dinner'">
		    <EndDate>
			<xsl:value-of select="concat('_',TYPE)"/>
		    </EndDate>
		</xsl:if>
		<xsl:if test="DTM01 = 'Lunch'">
		     <EndDate>
			<xsl:value-of select="concat('_',TYPE)"/>
		      </EndDate>
		</xsl:if>
 
	<xsl:text>&#xA;</xsl:text>
</xsl:for-each>

Open in new window

0
Comment
Question by:developer777
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
18 Comments
 

Author Comment

by:developer777
ID: 24789401
updated points
0
 

Author Comment

by:developer777
ID: 24789402
updated points
0
 

Author Comment

by:developer777
ID: 24789404
help please
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24789477
you can put contains() in the tests, so you need two tests, preferably in when clauses
<xsl
        <xsl:choose>
            <xsl:when test="contains(translate(., 'LUNCH', 'lunch'), 'lunch') and contains(translate(., 'DINER', 'diner'),'dinner')">
                <xsl:text>Lunch and Dinner</xsl:text>
            </xsl:when>
            <xsl:when test="contains(translate(., 'LUNCH', 'lunch'), 'lunch') and contains(translate(., 'BREAKFST', 'breakfst'),'breakFast ')">
                <xsl:text>BreakFast and lunch</xsl:text>
            </xsl:when>
        </xsl:choose>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24789491
The translate() function I used for making the serach for you keywords, case-insensitive

Note: sending 4 follow ups in a minute will not make you get a quicker answer
0
 

Author Comment

by:developer777
ID: 24789646
I think I did not ask the question properly.
meal can occurs maximun of 2 times. This will run the loop 2 times., If i need to write in c, or java
isLunch = false;
isBreakFast = false;
isDinner = false;
string bf= null;
string lunch = null;
string dinner= null;
for() // runs 2
{
   If (meal.equals("BreakFast")
   {
      isBreakFast = true;
     bf = meal;
   }
If (meal.equals("lunch")
   {
      IsLunch = true;
      lunch = meal;
   }
If (meal.equals("Dinner")
   {
     isDinner = true;
     dinner = meal;
   }


}/ end loop.
If(isDinner)
{
   println(lunch); //new line
  print(dinner); //new line
   
}
If(isBf)
{
 print(isBreakFast ); //new line
 println(lunch); //new line
   
}
I am loking some thing similar to this in XSLT.

Note: I am in hurry, Today is a ruch day. sorry for the updates
Thank you


0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24789671
You are in a hurry, OK, please ask the right question from the start then
Please explain what you want, do I really have to go out and interprete java code to get what you need?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24789695
Listen, this forum is about XSLT
- XSLT requires a completely different approach then procedural Javacode, so you procedural java code means nothing in this context
- input to XSLT is XML, I haven't( seen any XML so far

So please show the XML you start from or some examples of XML
based on that, show what you want as an output.
If you are really stressed and in a hurry, better learn to ask questions the right way
0
 

Author Comment

by:developer777
ID: 24789726
I am sorry.

I have one xml element, that can repeat 2 or more times. Possible values are Breakfast, lunch, or dinner.
if i loop through these by for each, the out put would be in the same order as input. But my requirement need a sequence.
input 1:

<ROOT>
   <Meal>lunch</Meal>
    <Meal>Breakfast</Meal>
</ROOT>
Out put should be always breakfast first if it is with lunch. ===>
breakfast
luncn
======
input 1:

<ROOT>
   <Meal>Dinner</Meal>
    <Meal>Lunch</Meal>
</ROOT>
Out put should be always Lunch first if it is with dinner. ===>

luncn
dinner

This would help

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24789756
Thank you, this really helps.
You made a mistake by posting java code.
because you don't solve this in XSLT, using loops and variables
You can XPath directly in the XML structure


<xsl:template match="ROOT">
        <xsl:choose>
            <xsl:when test="contains(translate(Meal, 'LUNCH', 'lunch'), 'lunch') and contains(translate(Meal, 'DINER', 'diner'),'dinner')">
                <xsl:text>Lunch and Dinner</xsl:text>
            </xsl:when>
            <xsl:when test="contains(translate(Meal, 'LUNCH', 'lunch'), 'lunch') and contains(translate(Meal, 'BREAKFST', 'breakfst'),'breakFast ')">
                <xsl:text>BreakFast and lunch</xsl:text>
            </xsl:when>
        </xsl:choose>
</xsl:template>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24789761
If you need them on two lines, you could do something like this
<xsl:text>Lunch&#10;Dinner</xsl:text>
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24789765
and there is a bug, sorry
<xsl:when test="contains(translate(Meal, 'LUNCH', 'lunch'), 'lunch') and contains(translate(Meal, 'BREAKFST', 'breakfst'),'breakFast ')">
should be
<xsl:when test="contains(translate(Meal, 'LUNCH', 'lunch'), 'lunch') and contains(translate(Meal, 'BREAKFST', 'breakfst'),'breakfast ')">
0
 

Author Comment

by:developer777
ID: 24789934
I am geting this error
A sequence of more than one item is not allowed as the first argument of translate()
Thank you
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24790006
yep, sorry, that is a mistake, try this
    <xsl:template match="ROOT">
        <xsl:choose>
            <xsl:when test="Meal[contains(translate(., 'LUNCH', 'lunch'), 'lunch')]  and Meal[contains(translate(., 'DINER', 'diner'),'dinner')]">
                <xsl:text>Lunch and Dinner</xsl:text>
            </xsl:when>
            <xsl:when test="Meal[contains(translate(., 'LUNCH', 'lunch'), 'lunch')] and Meal[contains(translate(., 'BREAKFST', 'breakfst'),'breakfast')]">
                <xsl:text>BreakFast and lunch</xsl:text>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

Open in new window

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 24790025
From your error message, I understand you are using XSLT2,
so you can use lower-case()
    <xsl:template match="ROOT">
        <xsl:choose>
            <xsl:when test="Meal[contains(lower-case(.), 'lunch')]  and Meal[contains(lower-case(.),'dinner')]">
                <xsl:text>Lunch and Dinner</xsl:text>
            </xsl:when>
            <xsl:when test="Meal[contains(lower-case(.), 'lunch')] and Meal[contains(lower-case(.),'breakfast')]">
                <xsl:text>BreakFast and lunch</xsl:text>
            </xsl:when>
        </xsl:choose>
    </xsl:template>

Open in new window

0
 

Author Comment

by:developer777
ID: 24790160
Than you. It works.  I am accepting solution. Can you explain me the what the does?
test="Meal[contains(lower-case(.), 'lunch')]  and Meal[contains(lower-case(.),'dinner')]">
Is it comparing every element in the NodeList

 Is there anyway we can do with out hard coding the values in the output.

Thank you
0
 

Author Closing Comment

by:developer777
ID: 31600369
Thank you for your time.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24791607
welcome

Meal[contains(lower-case(.), 'lunch')]
is an XPath expression that selects the Meal child element that has a content that contains 'lunch' when lower-cased
In an test expression this will reult in false() if there isn't such a Meal child, or true() when there is

test="Meal[contains(lower-case(.), 'lunch')]  and Meal[contains(lower-case(.),'dinner')]">
checks whether there is a Meal child with 'lunch' AND one with 'dinner'. I made order not important that way

Which values would you like to not 'hardcode'?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question