Hi Andreas,
let $asElement := <html><head><meta name="foo" content="bar"/></head></html> return serialize($asElement,map{"method":"text"})
- why is the result to the very last function empty?
It is empty because the text output method will only output the string value of your element, which is an empty string (i.e., the same you get when wrapping your element with fn:string; see [1] for more details).
- how can I convert an XML node to an entity escaped string, _without_
passing it in as string, but as element() or node()?
In Saxon and BaseX, the "xml" output method is used as default for XQuery functions (fn:serialize, file:write, etc). In Saxon, it’s also used as default for serializing the final result of a query. Early versions of BaseX used the same default. When the "adaptive" method was introduced, we wanted to switch over to this method (because it seemed to be more appropriate for the use cases of most BaseX users). As this method turned out to be mostly suitable for debugging data, we eventually introduced our own serialization method, called "basex", which serializes XML as XML, strings as strings, binary data as byte stream, and so on [2].
You can enforce an identical serialization behavior by defining the xml output method in the query prolog…
declare namespace output = 'http://www.w3.org/2010/xslt-xquery-serialization'; declare option output:method 'xml'; serialize(<html/>)
…or (if you want it globally) by setting the BaseX SERIALIZER option to "method=xml". In both cases,
1. The fn:serialize function call will convert your element to a string. 2. The output option will ensure that the result of your query will be output according to the rules of the "xml" output method.
what Saxon does (though I still wonder, how the Saxon people then get an unescaped string of XML, as BaseX does).
A serialize($element) function call combined with the "text" output method will be the appropriate choice.
Hope this helps, Christian
[1] https://www.w3.org/TR/xslt-xquery-serialization-31/#text-output [2] http://docs.basex.org/wiki/XQuery_Extensions#Serialization