Question

This is as much an XML question as it is a Qt one: Why does the following test for a namespace-uri of the attr attribute of the foo element fail?

{
    const QString test("<foo xmlns='http://example.org/ns' attr='value'><empty/></foo>");

    QXmlStreamReader r(test);
    QVERIFY(r.namespaceProcessing());
    QVERIFY(r.readNextStartElement());
    QCOMPARE(r.name().toString(), QLatin1String("foo"));
    QCOMPARE(r.namespaceUri().toString(),
             QLatin1String("http://example.org/ns"));
    QVERIFY(!r.attributes().isEmpty());
    QCOMPARE(r.attributes().front().name().toString(),
             QLatin1String("attr"));

    // FAIL, namespaceUri() is empty:
    QCOMPARE(r.attributes().front().namespaceUri().toString(),
             QLatin1String("http://example.org/ns"));
}

Is this a QXmlStreamReader bug, or are XML attributes in general not in the namespace declared with xmlns?

Était-ce utile?

La solution

XML attributes are not in the namespace declared with xmlns.

Had the same question and found the answer in the specification of namespaces in XML:

A default namespace declaration applies to all unprefixed element names within its scope. Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top