Getting distinct Values from XML in java, OSB, XQuery

1.7k views Asked by At
<data>  
  <info>  
    <name>A</name>  
  </info>  
  <info>  
    <name>B</name>  
  </info>  
  <info>  
    <name>A</name>  
  </info>  
<info>  
    <name>C</name>  
  </info>  
<info>  
    <name>B</name>  
  </info>  
</info>  

Is there any way to get result like in java or OSB or in Xquery distinct-values is not working in Oracle service bus so i want made java method to get unique names

<data>  
<info>  
<name>A</name>  
</info>  
<info>  
<name>B</name>  
</info>   
<info>  
<name>C</name>  
</info>  
</data>  
2

There are 2 answers

0
Ahmed Junaid On BEST ANSWER

I have solved like this

<CUSTOMER_NO_1>{ 
    (for $test in fn:distinct-values($data_ENV1/ns0:data/ns0:CUST/ns0:CUSTNO)
        return $test)[1]
}</CUSTOMER_NO_1>
<CUSTOMER_NO_2>{    
    (for $test in fn:distinct-values($data_ENV1/ns0:data/ns0:CUST/ns0:CUSTNO)
        return $test)[2]
}</CUSTOMER_NO_2>
<CUSTOMER_NO_3>{    
    (for $test in fn:distinct-values($data_ENV1/ns0:data/ns0:CUST/ns0:CUSTNO)
        return $test)[3]
}</CUSTOMER_NO_3>
<CUSTOMER_NO_4>{    
    (for $test in fn:distinct-values($data_ENV1/ns0:data/ns0:CUST/ns0:CUSTNO)
        return $test)[4]
}</CUSTOMER_NO_4>
<CUSTOMER_NO_5>{    
    (for $test in fn:distinct-values($data_ENV1/ns0:data/ns0:CUST/ns0:CUSTNO)
        return $test)[5]
}</CUSTOMER_NO_5>
<CUSTOMER_NO_6>{    
    (for $test in fn:distinct-values($data_ENV1/ns0:data/ns0:CUST/ns0:CUSTNO)
        return $test)[6]
}</CUSTOMER_NO_6>
<CUSTOMER_NO_7>{    
    (for $test in fn:distinct-values($data_ENV1/ns0:data/ns0:CUST/ns0:CUSTNO)
        return $test)[7]
}</CUSTOMER_NO_7>
6
Josh On

I assume you want to group all the info elements based on the name value and output only one info from each group?

let $data :=
<data>  
  <info>  
    <name>A</name>  
  </info>  
  <info>  
    <name>B</name>  
  </info>  
  <info>  
    <name>A</name>  
  </info>  
  <info>  
    <name>C</name>  
  </info>  
  <info>  
    <name>B</name>  
  </info>  
</data>  
return
  <data>{
    let $distinct-names := distinct-values($data/info/name)
    for $name in $distinct-names
    let $infos := $data/info[name eq $name]
    return
      $infos[1]
  }</data>

I think you can also use the "group by" extension:

let $data :=
<data>  
  <info>  
    <name>A</name>  
  </info>  
  <info>  
    <name>B</name>  
  </info>  
  <info>  
    <name>A</name>  
  </info>  
  <info>  
    <name>C</name>  
  </info>  
  <info>  
    <name>B</name>  
  </info>  
</data>  
return
  <data>{
    for $info in $data/info
    group $info as $infos by $info/name as $name
    return 
      $infos[1]
  }</data>

Both of the above queries return:

<data>
  <info><name>A</name></info>
  <info><name>B</name></info>
  <info><name>C</name></info>
</data>