-2

This is the XML input:

<bookstore>
  <book category="COOKING">
  <title lang="en">Everyday Italian</title>  
  <author>Giada De Laurentiis</author>
    <year>2005</year>
   <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
</bookstore>

I am using BaseX 7.9. When I request attributes,

for $book in collection()/bookstore/book
return $book/@*

an error occurs:

[SENR0001] Attributes cannot be serialized: attribute category {"COOKING"}.

How can this be fixed? Thanks for helping me!

Christian Grün
  • 6,012
  • 18
  • 34
Luke Le
  • 728
  • 1
  • 9
  • 24
  • Well, we honestly can't tell you more than the error message already does: Attributes can not be serialized. This is what your query is doing. If you want to get the attribute values, you could for example use `@*/data()`. If you need more help, you have to specify what you want to get back as result – dirkk Nov 25 '14 at 09:50
  • thanks dirkk i want to get exactly attribute @category. it is xsi:type attribute. i use @*/string() then get back result is "COOKING CHILDREN", it is a string. I do not want it – Luke Le Nov 25 '14 at 10:03
  • 1
    Cooking children sounds delicious, why don't you want it? – dirkk Nov 25 '14 at 10:07
  • Kidding aside, you do get the result back for attribute category. It simply is a concatenation of both results (you iterate over both books and you get back the attributes of each of them). So, what do you want to get back? What is the expected result? – dirkk Nov 25 '14 at 10:09
  • i use Basex 6.7.1 then get attributes by @* is well. Why basex 7.9 is not working. – Luke Le Nov 25 '14 at 10:10
  • :)) i want to use function. declare function local:getAttribute($attributes as attribute()*) as attribute()* {{ for $attribute in $attributes where matches($attribute,"(^|\W)COOKING(\W|$)","i") and name($attribute) != "xsi:type" and name($attribute) != 'id' return $attribute }}; then, return local:getAttribute($book/@*) – Luke Le Nov 25 '14 at 10:28
  • It is not "not working" in BaseX 7.9, it is defined behaviour. BaseX is just strict about serialization. As I said before, use `data()` (or `string()`) to return the attribute value, which is what you are most likely interested in. – dirkk Nov 25 '14 at 10:39
  • possible duplicate of [BaseX attributes cannot be serialized](http://stackoverflow.com/questions/24663393/basex-attributes-cannot-be-serialized) – dirkk Nov 25 '14 at 10:39

1 Answers1

1

The XQuery 3.1 Serialization specification provides the new "adaptive" serialization mode, which allows the serialization of attribute and namespace nodes. Since Version 8.0 of BaseX, this mode is used as new default.

This was different in earlier versions of the specification, which did not allow attributes to be output on their own (see the error code SENR0001 for more information).

Christian Grün
  • 6,012
  • 18
  • 34