문제

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?

도움이 되었습니까?

해결책

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.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top