3

I have written xquery to return results in normal way.

let $results := //data:data
return 
  <result>
  {
    for $i in $results
    return
      <documentInformation>
        <id>{data($i/DATA:ID)}</id>
        <status>{data($i/@status)}</status>
        <title>{data($i/data:title)}</title>
        <displayName>{data($i/DATA:DISPLAYNAME)}</displayName>
      </documentInformation>
  }
  </result>

Now, I have to filter out the results in for loop with some condition like

(pseudo logic)
if id = 'abc' and status ="closed"  
then skip the row
else add row.

I have tried several ways. but could not run the query..

Dave Cassel
  • 8,352
  • 20
  • 38
user991255
  • 355
  • 1
  • 6
  • 13
  • 4
    Please read [How to post an SSCCE](http://www.sscce.org) and [ask]. Your question lacks example input, which will either lead to long discussions ("still does not work for me") or makes the question hardly answerable at all. Anyway, I'm very sure you don't need an if statement at all, have a look at predicates and the where clause, which are both _very_ basic and important XQuery language constructs. Don't write "I have tried several ways." Post _what_ your tried, and how it fails! – Jens Erat Mar 10 '15 at 12:48

2 Answers2

5

Try this:

<result>
{
  for $i in //data:data
  where fn:not($i/DATA:ID = 'abc' and $i/@status = "closed")
  return
    <documentInformation>
      <id>{data($i/DATA:ID)}</id>
      <status>{data($i/@status)}</status>
      <title>{data($i/data:title)}</title>
      <displayName>{data($i/DATA:DISPLAYNAME)}</displayName>
    </documentInformation>
}
</result>

Note that the XPath //data:data may have a lot of work to do, but that's a separate matter.

Dave Cassel
  • 8,352
  • 20
  • 38
  • 2
    Might be helpful to note a couple alternatives. In some situations it may be more efficient to write a predicate instead of a where clause: `for $i in //data:data[not(DATA:ID = 'abc') and @status = "closed")]`. In other situations it's convenient to nest inside the return, as `return if not($i/DATA:ID = 'abc' and $i/@status = "closed") then $a else $b` – mblakele Mar 10 '15 at 19:26
4

You Can also use if condition instead of where

<result>
{
    for $i in //data:data
    return 
        if($i/DATA:ID != 'abc' and $i/@status != "closed")
        then
        (
            <documentInformation>
            <id>{data($i/DATA:ID)}</id>
            <status>{data($i/@status)}</status>
            <title>{data($i/data:title)}</title>
            <displayName>{data($i/DATA:DISPLAYNAME)}</displayName>
            </documentInformation>
        )
        else ()
}
</result>
James
  • 32,991
  • 4
  • 47
  • 70
Nikunj Vekariya
  • 833
  • 5
  • 19