[xquery-talk] How can I search specific value through
XQuery
G. Ken Holman
gkholman at CraneSoftwrights.com
Tue Mar 29 07:16:25 PST 2011
At 2011-03-29 15:36 +0530, Kunal Chauhan wrote:
>So, How can I retrive exact result.
By returning only what matches ... you are explicitly returning the
entire document.
>Here is one Example which I tried.
>
>My XML file
>
><doc>
><result id="instance" href="abc">
><hi>val1</hi>
><new>val2</new>
></result>
><result id="instance1" href="abc">
><hi>val1</hi>
><new>val2</new>
></result>
><result id="instance1" href="abcds">
><hi>val1</hi>
><new>val2</new>
></result>
></doc>
>
>XQuery
>
>declare variable $docs2 external;
>let $docs2 := doc("D:/try.xml")/doc
>for $x in $docs2
>where $x/result[@href = 'abc']
>return $x
Note your variable assignment for $x is the <doc> element, so when
you return $x you are returning the entire document:
>And Result is as under
>
><doc>
><result id="instance" href="abc">
><hi>val1</hi>
><new>val2</new>
></result>
><result id="instance1" href="abc">
><hi>val1</hi>
><new>val2</new>
></result>
><result id="instance1" href="abcds">
><hi>val1</hi>
><new>val2</new>
></result>
></doc>
You want to address the items you want returned, not their parent:
for $x in $docs2/result where $x[@href = 'abc'] return $x
To make the result well-formed, you'll need to wrap the query around
a result document element.
I hope this helps.
. . . . . . . . . . . . . Ken
--
Contact us for world-wide XML consulting & instructor-led training
Crane Softwrights Ltd. http://www.CraneSoftwrights.com/q/
G. Ken Holman mailto:gkholman at CraneSoftwrights.com
Legal business disclaimers: http://www.CraneSoftwrights.com/legal
More information about the talk
mailing list