XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Steve AndersonSubject: return different result based on conditions
Author: Steve Anderson
Date: 15 Nov 2009 11:54 PM
I am having a hard time returning a value based on certain conditions.

SORRY FOR THE LAME EXAMPLE...
I have a group of departments which contain certain items.
Dept A: PS3, PSP, XBox, WII
Dept B: PS3, XBox
Dept C: WII
Dept D: PSP, WII

Each of these items have a price. I want to return the total for each department.
So, lets say
PS3 = $300
PSP = $150
Xbox = $350
WII = $250

So the total for Dept D should be $400

How can i do this using XQuery. I just found out that variables don't change in XQuery, so there goes that idea (can't use a counter of any sort). I am stuck and need your help.

Postnext
Alberto MassariSubject: return different result based on conditions
Author: Alberto Massari
Date: 16 Nov 2009 09:07 AM
Hi Steve,
in XQuery (and XSLT) you work on set of nodes, so you can compute the sum once you have written down the XPath query that highlights the prices of the items of department D. If you post the XML fragment, we could tell you such a query.

Alberto

Postnext
Steve AndersonSubject: return different result based on conditions
Author: Steve Anderson
Date: 16 Nov 2009 10:23 AM
Hi Alberto, thanks for responding.

In a nut shell this is what I have.

<dept>
<items>PS3, PSP, XBox, Wii</items>
</dept>
<dept>
<items>PS3, XBox</items>
</dept>
<dept>
<items>Wii</items>
</dept>
<dept>
<items>PSP, Wii</items>
</dept>
<item>
<name>PS3</name>
<price>300</price>
</item>
<item>
<name>PSP</name>
<price>150</price>
</item>
<item>
<name>Xbox</name>
<price>350</price>
</item>
<item>
<name>Wii</name>
<price>250</price>
</item>

the code that i have, looks like this

total = 0
for all departments
string array of <items> using tokenize with delimiter ", "
for all items found in the department
if item = "PS3"
then total = total + 300
else if item = "PSP"
then total = total + 150
and
so
on...

So, I found out the hard way that you can't change a variable in XQuery. So, this sudo code will not work.

Postnext
Alberto MassariSubject: return different result based on conditions
Author: Alberto Massari
Date: 16 Nov 2009 11:26 AM
Hi Steve,
your pseudo code should be changed into this:

for each department
tokenize the item list into a list of items
then sum all the prices associated to the item nodes that belong to that list

This is the corresponding XQuery code:

for $dept at $index in /root/dept return
<dept name="{$index}">
{
let $items := tokenize(fn:replace($dept/items, ", ", ","), ",")
return sum(//item[name = $items]/price)
}
</dept>

Hope this helps,
Alberto

Postnext
Steve AndersonSubject: return different result based on conditions
Author: Steve Anderson
Date: 16 Nov 2009 11:31 AM
Ok, I will give this a shot today and will let you know.

Thanks again.

Postnext
Steve AndersonSubject: return different result based on conditions
Author: Steve Anderson
Date: 16 Nov 2009 11:59 PM
WORKED!!

You are awesome. Thank you!

Posttop
Bob ChauvinSubject: return different result based on conditions
Author: Bob Chauvin
Date: 09 Feb 2010 08:58 AM
Alberto IS awesome!

 
Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.