Writting nan with capital letters in XML files
-
Hello,
I am using QXmlStreamWritter to produce XML files containing my technical content. To do, so, I have "writters" as follows:
/** * @brief Write double value to XML stream as element * @param stream: XML stream * @param elementName: element name * @param value: value */ static void writeDoubleToXMLElement(QXmlStreamWriter & stream, const QString & elementName, const double value) { stream.writeTextElement(elementName, QString::number(value, 'g', 10)); }It works well and write "nan" if value is not a number. However, when using QXmlSchemaValidator, it think it expects "NaN" with capital letters (it fails with "nan")... I could test values and write "NaN" by my own but I would like to know if it is something configurable to avoid having to update all my "writers" functions.
Thanks is advance.
-
QString::number() is not configurable in the way you seek: "nan" is hard-coded. It seems that if the format is specified entirely in upper-case then the output will be upper-case. Not clear if this applies to "nan".
QXmlSchemaValidator checks your XML against the schema you provide. It is this schema that is rejecting "nan" in whatever element you have written it. Are you sure the schema will accept any non-number in that element?
-
Hi @Julieng031,
As @ChrisW67 said, the
nanis hard-coded. For example, you can see it in Qt 6.4 here:} else if (qt_is_nan(d)) { if (bufSize >= 3) { buf[0] = 'n'; buf[1] = 'a'; buf[2] = 'n'; length = 3; } ...Assuming you cannot adjust the XML schema (maybe its provided by someone else?), you could simply handle NaN's manually, like:
stream.writeTextElement(elementName, qIsNan(value) ? QString("NaN") : QString::number(value, 'g', 10));Cheers.
-
Thanks for your quick answers.
@ChrisW67 - NaN seems to be defined in the XSD standard:
The ·lexical space· of double is the set of all decimal numerals with or without a decimal point, numerals in scientific (exponential) notation, and the ·literals· 'INF', '+INF', '-INF', and 'NaN'
In https://www.w3.org/TR/xmlschema11-2/.
I wanted to benefit from that.Ok, I will check this case by hand!
-
NaN seems to be defined in the XSD standard:
Yeah, understood. Its just that
QXmlStreamWriter::writeTextElement()only knows about text (not floats), whileQString::number()knows nothing about XML.What you really want, is something like this:
QString xsdNumber(const double number, const char format, const int precision) { if (qIsInf(number)) { return (number < 0) ? QStringLiteral("-INF") : QStringLiteral("INF"); } if (qIsNaN(number)) return QStringLiteral("NaN"); return QString::number(number, format, precision); } static void writeDoubleToXMLElement(QXmlStreamWriter & stream, const QString & elementName, const double value) { stream.writeTextElement(elementName, xsdNumber(value, 'g', 10)); }Note, I considered templating
xsdNumber()'snumbertype so you could usefloat,doubleorlong double, but sinceQString::number()only supportsdouble(and integers), there's currently no value in adding that complexity.Cheers.