QXmlQuery update of attributes
-
I'm really new with the XQuery language. I try to update an attribute value in a XML document with XQuery an the QXmlQuery class.
I found the following example on the internet.XML Source:
<root foo="bar"> <message>Hello!</message> </root>
XQuery Code:
let $doc := doc('/db/test/update-attribute/root.xml')/root return update value $doc/@foo with 'new-value'
XML Result:
<root foo="new-value"> <message>Hello!</message> </root>
Is it possible to do this with QXmlQuery class?
I tried it with the following code, but it didn't work:QString fileName("D:\\source.xml"); QFile file(fileName); if (file.open(QIODevice::ReadOnly)) { QXmlQuery xmlQuery; xmlQuery.bindVariable("fileName", &file); QString resultString; QString queryString; queryString = "declare variable $fileName external;\n"; queryString += "let $doc := doc($fileName)/root\n"; queryString += "return update value $doc/@foo with 'new-value'\n"; xmlQuery.setQuery(queryString); if (xmlQuery.evaluateTo(&resultString)) qDebug() << resultString; }
A Query like "declare variable $fileName external; doc($fileName)/root/@foo/data(.)" works, but update operation like described above not. Is update possible with QXmlQuery class or is a newer version of the XQuery language or an extenstion necessary which QXmlQuery don't support?
-
Hello
I had the same problem. It is looks like that Qt currently only supports "Minimal Conformance" and yet does not support optional XQuery features as it is described here:QXMLQuery Features and Conformance:
XQuery 1.0 Qt XML Patterns aims at being a conformant XQuery processor. It adheres to Minimal Conformance and supports the Serialization Feature and the Full Axis Feature. Qt XML Patterns currently passes 97% of the tests in the XML Query Test Suite. Areas where conformance may be questionable and where behavior may be changed in future releases include: Some corner cases involving namespaces and element constructors are incorrect. XPath is a subset of XQuery and the implementation of Qt XML Patterns uses XPath 2.0 with XQuery 1.0. The specifications discusses conformance further: XQuery 1.0: An XML Query Language. W3C's XQuery testing effort can be of interest as well, XML Query Test Suite. Currently fn:collection() does not access any data set, and there is no API for providing data through the collection. As a result, evaluating fn:collection() returns the empty sequence. We intend to provide functionality for this in a future release of Qt. Only queries encoded in UTF-8 are supported.
And here is the relation of XQuery update with "Minimal Conformance"
So I guess this feature is not included in the current version of QXMLQuery
Instead it is possible to use QDomDocument