DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2011
    Posts
    4

    Xquery distinct values from XML with looping.

    Hi,
    I have a requirement, where in i have to get distinct values from an xml file.

    EX: Input
    Code:
    <?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:
    Code:
    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..

  2. #2
    Join Date
    Jul 2011
    Posts
    4
    Any luck in this? please advice on my requirement.

Similar Threads

  1. Distinct Values in XML with .Net
    By vinu in forum XML
    Replies: 1
    Last Post: 11-16-2010, 03:02 AM
  2. SQL Help
    By carbon_13 in forum Database
    Replies: 14
    Last Post: 03-14-2008, 04:03 PM
  3. Try XML Junction
    By Tim in forum xml.announcements
    Replies: 0
    Last Post: 10-11-2001, 04:00 PM
  4. Data Junction Announces XML Junction 7.51
    By Tim Frost in forum xml.announcements
    Replies: 0
    Last Post: 04-02-2001, 10:53 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links