QXmlStreamReader和属性上的空namespaceUri()

时间:2012-06-21 12:20:28

标签: xml qt xml-namespaces qxmlstreamreader

这是一个XML问题,因为它是Qt问:为什么attr元素的foo属性的名称空间-uri的以下测试失败?

{
    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"));
}

这是QXmlStreamReader错误,还是XML属性通常不在使用xmlns声明的命名空间中?

1 个答案:

答案 0 :(得分:1)

XML属性不在使用xmlns声明的命名空间中。

有同样的问题,并在specification of namespaces in XML中找到答案:

默认名称空间声明适用于其范围内的所有未加前缀的元素名称。默认名称空间声明不直接应用于属性名称;对无前缀属性的解释由它们出现的元素决定。
相关问题