Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Writting nan with capital letters in XML files
Qt 6.11 is out! See what's new in the release blog

Writting nan with capital letters in XML files

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Julieng031
    wrote on last edited by Julieng031
    #1

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        Hi @Julieng031,

        As @ChrisW67 said, the nan is 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.

        1 Reply Last reply
        2
        • J Offline
          J Offline
          Julieng031
          wrote on last edited by Julieng031
          #4

          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!

          1 Reply Last reply
          0
          • Paul ColbyP Offline
            Paul ColbyP Offline
            Paul Colby
            wrote on last edited by
            #5

            NaN seems to be defined in the XSD standard:

            Yeah, understood. Its just that QXmlStreamWriter::writeTextElement() only knows about text (not floats), while QString::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()'s number type so you could use float, double or long double, but since QString::number() only supports double (and integers), there's currently no value in adding that complexity.

            Cheers.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved