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. QDomElement::setAttributeNS() sets empty namespace URI
QtWS25 Last Chance

QDomElement::setAttributeNS() sets empty namespace URI

Scheduled Pinned Locked Moved Solved General and Desktop
4.8qdomdocumentqdomelementxmlnsnamespace
3 Posts 2 Posters 1.5k Views
  • 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.
  • D Offline
    D Offline
    drB33R
    wrote on last edited by
    #1

    Hi guys,

    I wanted to use namespace in my QDomDocument, but namespace URI is not appearing in the output.
    Using Qt 4.8.6

    I have no idea what's wrong here, so any idea would be useful.

    The following line should do the trick:

    m_root.setAttributeNS("foo", "bar",  1.0);
    

    and the result should look like this:

    <fooelement foo:bar="1.0"/>
    

    but, i got this:

    <fooelement :bar="1.0"/>
    

    Here is the full source:

    #include <QCoreApplication>
    #include <QFile>
    #include <QDomDocument>
    #include <QTextStream>
    
    int main(int argc, char *argv[])
    {
        QFile file("attributeNStest.xml");
        QDomDocument *m_doc = new QDomDocument("foodoc");
        QDomElement m_root;
    
        file.open(QIODevice::WriteOnly);
    
        m_root = m_doc->createElement("fooelement");
    
        // version attribute
        m_root.setAttributeNS("foo", "bar",  1.0);
        m_doc->appendChild(m_root);
    
        QTextStream out(&file);
        out << m_doc->toString();
    
        return 0;
    }
    
    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @drB33R, welcome :)

      I'm no XML expert, but I think I can see the issue...

      In your desired output:

      <fooelement foo:bar="1.0"/>
      

      foo is not a namespace URI, but rather a namespace prefix.

      Note, the prefix is supposed to alias to a namespace URI via an xmlns attribute, but you have not provided the URI in your desired output, so I'll use http://example.com/mynamespace for example.

      Now, where you have:

      m_root.setAttributeNS("foo", "bar",  1.0);
      
      • the first parameter (foo) is supposed to be the namespace URI (remember, foo in your desired output is the namespace prefix, not the URI).
      • the second parameter (bar) is supposed to be "qualified name" - that is, both the namespace prefix and the attribute name together - ie foo:bar in your example.

      So, putting it all together, you would change:

      m_root.setAttributeNS("foo", "bar",  1.0);
      

      to

      m_root.setAttributeNS("http://example.com/mynamespace", "foo:bar",  1.0);
      

      That gives the output:

      <fooelement foo:bar="1" xmlns:foo="http://example.com/mynamespace"/>
      

      Hopefully that gets you a bit closer to whatever you're trying to achieve :)

      Cheers.

      D 1 Reply Last reply
      2
      • Paul ColbyP Paul Colby

        Hi @drB33R, welcome :)

        I'm no XML expert, but I think I can see the issue...

        In your desired output:

        <fooelement foo:bar="1.0"/>
        

        foo is not a namespace URI, but rather a namespace prefix.

        Note, the prefix is supposed to alias to a namespace URI via an xmlns attribute, but you have not provided the URI in your desired output, so I'll use http://example.com/mynamespace for example.

        Now, where you have:

        m_root.setAttributeNS("foo", "bar",  1.0);
        
        • the first parameter (foo) is supposed to be the namespace URI (remember, foo in your desired output is the namespace prefix, not the URI).
        • the second parameter (bar) is supposed to be "qualified name" - that is, both the namespace prefix and the attribute name together - ie foo:bar in your example.

        So, putting it all together, you would change:

        m_root.setAttributeNS("foo", "bar",  1.0);
        

        to

        m_root.setAttributeNS("http://example.com/mynamespace", "foo:bar",  1.0);
        

        That gives the output:

        <fooelement foo:bar="1" xmlns:foo="http://example.com/mynamespace"/>
        

        Hopefully that gets you a bit closer to whatever you're trying to achieve :)

        Cheers.

        D Offline
        D Offline
        drB33R
        wrote on last edited by drB33R
        #3

        Hi @Paul-Colby,

        Awesome!
        Now, I understand better what the Qt document wanted to say about this function.
        (Probably, an example in the doc would be useful.)

        It's a different question, but is there any way to move the xmlns attribute outside from this tag?
        (Example, If I would use more <fooelement> tags, it would be redundant to see this definition again.)

        Thanks a lot!

        1 Reply Last reply
        0

        • Login

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