Comparing Equality of Node Using Deep-Equal() Not Working For Numeric Data.

Wednesday, 4 February 2015 · 5 messages
AK
ankit kumar4 Feb 2015 08:15 #
Hi, I am trying to compare to compare two nodes containing same double values with different scientific notation, matching the two nodes return false. But it should return true. Please help me out.
==============================================
let $a := <a>123.4</a>
let $b := <a>12.34E1</a>
return deep-equal($a,$b)
Expected : true Output : false Thanks Ankit
FG
Florent Georges4 Feb 2015 08:44 #
Quoted text · ankit kumar · 1 line
On 4 February 2015 at 08:15, ankit kumar wrote:
Hi,
Quoted text · ankit kumar · 5 lines
> let $a := <a>123.4</a> > let $b := <a>12.34E1</a> > return deep-equal($a,$b) > Expected : true > Output : false
I expect it to return false. The elements are not typed, so their respective string values are compared to each other. Which indeed are different (they are not the same character sequences). But if you validate the elements so they are typed as xs:double say, then the expression returns true. Given the following schema in ankit.xsd:
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               elementFormDefault="qualified">
       <xs:element name="a" type="xs:double"/>
    </xs:schema>
the following query returns qhat you expect: xquery version "3.0"; import schema default element namespace "" at "ankit.xsd";
    let $a := validate { <a>123.4</a> }
    let $b := validate { <a>12.34E1</a> }
    return
       deep-equal($a, $b)
Regards,
CG
Christian Grünin reply to ankit kumar, 4 Feb 2015, 08:154 Feb 2015 08:46 #
Hi Ankit,
Quoted text · ankit kumar · 3 lines
> I am trying to compare to compare two nodes containing same double values > with different scientific notation, matching the two nodes return false. But > it should return true. Please help me out.
The reason is that the values are compared as nodes, not as doubles. You'll have to convert your input to numeric values. Here are two solutions that work:
  let $a := number(<a>123.4</a>)
  let $b := number(<a>12.34E1</a>)
  return deep-equal($a,$b)

  let $a := <a>123.4</a>
  let $b := <a>12.34E1</a>
  return xs:double($a) eq xs:double($b)
Best, Christian
Quoted text · ankit kumar · 12 lines
> > ============================================== > let $a := <a>123.4</a> > let $b := <a>12.34E1</a> > return deep-equal($a,$b) > > Expected : true > Output : false > > > Thanks > Ankit
AK
ankit kumar4 Feb 2015 09:19 #
Hi, Thanks for your prompt reply. My scenario is that i have a node of complex type. I don't have any knowledge that what may be the value inside the node of any type, I have to compare node whether they are equal or not, also taking care of different scientific notation for same value.is this possible in BaseX. Thanks Ankit
Quoted text · Christian Grün · 38 lines
On 4 February 2015 at 13:16, Christian Grün <christian.gruen at gmail.com> wrote: > Hi Ankit, > > > I am trying to compare to compare two nodes containing same double values > > with different scientific notation, matching the two nodes return false. > But > > it should return true. Please help me out. > > The reason is that the values are compared as nodes, not as doubles. > You'll have to convert your input to numeric values. Here are two > solutions that work: > > let $a := number(<a>123.4</a>) > let $b := number(<a>12.34E1</a>) > return deep-equal($a,$b) > > let $a := <a>123.4</a> > let $b := <a>12.34E1</a> > return xs:double($a) eq xs:double($b) > > Best, > Christian > > > > > > ============================================== > > let $a := <a>123.4</a> > > let $b := <a>12.34E1</a> > > return deep-equal($a,$b) > > > > Expected : true > > Output : false > > > > > > Thanks > > Ankit >
CG
Christian Grün4 Feb 2015 09:22 #
Hi Ankit, Currently, BaseX has no schema support; this is why you'll need to make type comparison explicit in your XQuery code. Hope this helps, Christian
Quoted text · ankit kumar · 57 lines
On Wed, Feb 4, 2015 at 9:19 AM, ankit kumar <anky4bugs at gmail.com> wrote: > Hi, > > Thanks for your prompt reply. My scenario is that i have a node of complex > type. I don't have any knowledge that what may be the value inside the node > of any type, I have to compare node whether they are equal or not, also > taking care of different scientific notation for same value.is this possible > in BaseX. > > > Thanks > Ankit > > > > > On 4 February 2015 at 13:16, Christian Grün <christian.gruen at gmail.com> > wrote: >> >> Hi Ankit, >> >> > I am trying to compare to compare two nodes containing same double >> > values >> > with different scientific notation, matching the two nodes return false. >> > But >> > it should return true. Please help me out. >> >> The reason is that the values are compared as nodes, not as doubles. >> You'll have to convert your input to numeric values. Here are two >> solutions that work: >> >> let $a := number(<a>123.4</a>) >> let $b := number(<a>12.34E1</a>) >> return deep-equal($a,$b) >> >> let $a := <a>123.4</a> >> let $b := <a>12.34E1</a> >> return xs:double($a) eq xs:double($b) >> >> Best, >> Christian >> >> >> > >> > ============================================== >> > let $a := <a>123.4</a> >> > let $b := <a>12.34E1</a> >> > return deep-equal($a,$b) >> > >> > Expected : true >> > Output : false >> > >> > >> > Thanks >> > Ankit > >

⚡Generated with XQuery