user2998504
user2998504

Reputation: 75

Required item type of first operand of '/' is node(); supplied value has item type xs:anyAtmonicType

Hi can someone please help me I am not sure why I am getting

"Required item type of first operand of '/' is node(); supplied value has item type xs:anyAtmonicType".

I am trying to group websites by year.

 <publications>
    {
    for $x in distinct-values(//www) 
    return 
    <year-Pub>{for $y in //www where $x/year = $y/year
    return <year>{$y/*}</year>}</year-Pub>
    }
    </publications>

Upvotes: 1

Views: 3501

Answers (2)

Michael Kay
Michael Kay

Reputation: 163352

I suspect that you want

<publications>
    {
    for $x in distinct-values(//www/year) 
    return 
    <year-Pub>{for $y in //www[year = $x]
               return <year>{$y/*}</year>}</year-Pub>
    }
</publications>

Alternatively look at "group by" in XQuery 3.0.

Upvotes: 2

Jens Erat
Jens Erat

Reputation: 38682

distinct-values(...) returns a sequence of xs:anyAtomicType values, eg. strings or numbers, but never XML nodes (node()). When running on a sequence of XML nodes (which //www returns), fn:data(...) is implicitly called for each node to convert it to an atomic value.

In the line for $y in //www where $x/year = $y/year, you want to perform an axis step starting at $x, which subsequently is not possible.

As you didn't give any input or description of what exactly you want to achieve, I cannot help you with a working and tested solution, but as a brief sketch to deal with distinct nodes:

  • Fetch the distinct values as before (and make sure they contain what you're aiming at). I guess you need distinct-values(//www/year instead).
  • Now, loop over all nodes, and during comparison remember the variable is already holding the atomic value, not an element.

Upvotes: 1

Related Questions