Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saxon XPath error - "cannot serialize attribute"

when I run an XQuery command-line, it works as long as the result is an element.

When I extend that XQuery to get an attribute-value only, it fails with this error:

SENR0001: Cannot serialize a free-standing attribute node (net.sf.saxon.om.NameOfNode)

So, this works

java -cp ...Saxon-HE-9.9.0-1.jar net.sf.saxon.Query 
   -s:AnyXMLFileAvailable.xml -qs:/

this fails:

java -cp ...Saxon-HE-9.9.0-1.jar net.sf.saxon.Query 
   s:AnyXMLFileAvailable.xml -qs://@*

As long as there is at least one attribute anywhere in the XML, this command will fail with the above error

I believe this can be fixed with licensed Saxon which enables use of the

 -outval:recover 

switch. Is there another way?

like image 641
user192127 Avatar asked Sep 09 '26 10:09

user192127


1 Answers

The default serialization method when you run Query from the command line is "xml"; I have been reluctant to change that for compatibility reasons. The "xml" serialization method fails when you try to serialize certain results, including maps, arrays, and attribute nodes.

If you set !method=adaptive on the command line (escaping the "!" with "\" if using bash) then serialization should never fail, it will produce some kind of representation of the query result. If the result is an attribute, it will represent it as name="value" which may or not be what you want. If you actually want just the string value of the attribute, then you need to extract this within the query, e.g. by using the string() or data() functions.

The -outval option is quite irrelevant, it's concerned with schema-validation of the query result.

like image 152
Michael Kay Avatar answered Sep 11 '26 11:09

Michael Kay