Skip to Main Content

DevOps, CI/CD and Automation

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Xquery to return distinct values in xml

user12679330Jul 12 2011 — edited Jul 13 2011
Hi,
I have a requirement, where in i have to get distinct values from an xml file.

EX: Input

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Main xmlns="http://www.abc.org.com/FormDetails">
<abcd first="1" second="2"/>
<Fruits>
<fruit name="banana" color="green">
<fruit name="apple" color="red">
<fruit name="grape" color="green">
<fruit name="apple" color="red">
<fruit name="banana" color="green">
<fruit name="apple" color="red">
<fruit name="grape" color="green">
<fruit name="orange" color="orange">
<fruit name ="lemon" color="yellow">
</Fruits>
<Fruits>
<fruit name="banana" color="green">
<fruit name="apple" color="red">
<fruit name="grape" color="green">
<fruit name="apple" color="red">
<fruit name="banana" color="green">
<fruit name="apple" color="red">
<fruit name="grape" color="green">
<fruit name="orange" color="orange">
<fruit name ="lemon" color="yellow">
</Fruits>
<Fruits>
<fruit name="banana" color="green">
<fruit name="apple" color="red">
<fruit name="grape" color="green">
<fruit name="apple" color="red">
<fruit name="banana" color="green">
<fruit name="apple" color="red">
<fruit name="grape" color="green">
<fruit name="orange" color="orange">
<fruit name ="lemon" color="yellow">
</Fruits>
</Main>

Required output: String

banana = green|apple = red| grape=green|orange=orange|lemon=yellow

I have used this xquery:

declare namespace xf = "http://tempuri.org/abcorg/Transformations/Distinct_Fruits/";
declare namespace ns = "http://www.abc.org.com/FormDetails";


declare function xf:Distinct_Fruits($fruits as element(ns:Main))
as xs:string{


for $d in distinct-values($fruits//ns:fruit/@name)
return "{$d} ="

for $e in distinct-values($fruits//ns:fruit/@color)
return "{$e}|"

};


declare variable $fruitsas element(ns:Main) external;

xf:Distinct_Member($fruits)



I am getting error like:

Error executing the XQuery transformation: line 11, column 1: {err}FORG0005: expected exactly one item, got 2+ items


Please let me know where It went wrong.


Thanks..

Edited by: user12679330 on Jul 12, 2011 4:47 AM
This post has been answered by odie_63 on Jul 13 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 10 2011
Added on Jul 12 2011
9 comments
3,803 views