我有以下有效 XML:xml-tag可以使用两个命名空间,或者这是libxml2错误?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<aaa xmlns:de="http://www.dolby.com/dcinema/ws/smi/v11/SPL" atr="abc" xmlns:fe="http://somewhere">
some text
<de:bbb atr1="abb" atr2="baa" >aaa</de:bbb>
<de:ccc>aaa</de:ccc>
<fe:ddd>bbb</fe:ddd>
some more text
</aaa>
而下面的C代码:
#include <stdio.h>
#include <libxml/xmlreader.h>
#include <libxml/tree.h>
char xml_data[] = {
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
" <aaa xmlns:de=\"http://www.dolby.com/dcinema/ws/smi/v11/SPL\" "
" atr=\"abc\""
" xmlns:fe=\"http://somewhere\">\n"
" some text\n"
" <de:bbb atr1=\"abb\" atr2=\"baa\" >aaa</de:bbb>\n"
" <de:ccc>aaa</de:ccc>\n"
" <fe:ddd>bbb</fe:ddd>\n"
" some more text\n"
" </aaa>"
};
void printns(xmlNsPtr ns, int deep, char * marker)
{
while (ns)
{
printf("%*c%s+%s\n", deep * 5 + 1, ' ', marker, ns->prefix);
ns = ns->next;
}
}
void printelem(xmlNodePtr ptr, int deep)
{
printf("%*c%s\n", deep * 5, ' ', ptr->name);
if (ptr->type == XML_ELEMENT_NODE)
{
printns(ptr->nsDef, deep, "d");
printns(ptr->ns, deep, "u");
}
if (ptr->xmlChildrenNode) printelem(ptr->xmlChildrenNode, deep+1);
if (ptr->next) printelem(ptr->next, deep);
}
int main(void)
{
LIBXML_TEST_VERSION
xmlInitParser();
xmlDocPtr doc;
doc = xmlReadDoc(BAD_CAST xml_data, NULL, NULL, XML_PARSE_NOBLANKS);
printelem(doc->xmlChildrenNode, 1);
xmlFreeDoc(doc);
}
这将产生以下的输出:
aaa
d+de
d+fe
text
bbb
u+de
u+fe
text
text
ccc
u+de
u+fe
text
text
ddd
u+fe
text
text
正如你看到的,libxml2的说如预期的那样,当ddd有一个命名空间时,bbb和ccc同时拥有两个命名空间。这是一些xml-standart规则,这对我来说是未知的,或者是这个libxml2错误?
我不认为XML是有效的。它的结构良好。 –
您能否提供带代码的[MCVE]?以及您正在使用的确切版本号。 –
提供的xml是绝对有效的,通过标准和任何验证器,如http://www.xmlvalidation.com/。 将尝试提供MCV ASAP。 –