libxml2 的解析问题:命名空间错误

Parsing issue with libxml2: Namespace error

我在解析我的 xml 文件时遇到问题,它与 libxml 不正确,但已被应用程序所有者证明有效。

我已经尝试阅读 RFC 到 XOP,但似乎与我的问题无关。而且我不明白如何在我的代码中实现其他解决方案。

int main(int argc, char **argv) {

   char         *docname;
   xmlDocPtr    doc;
   xmlNodePtr   cur;
   xmlChar      *date;

   if (argc < 2) {
     printf("Commande: %s nom_du_fichier\n", argv[0]);
     return EXIT_FAILURE;
   }


   docname = argv[1];

   doc = xmlParseFile(docname);
   cur = xmlDocGetRootElement(doc);

   cur = cur->xmlChildrenNode;
   while (cur != NULL) {
       if ((!xmlStrcmp(cur->name, (const xmlChar *)"anpr"))) {
         date = xmlGetProp(cur, "date");
         printf("date: %s\n", date);
         xmlFree(date);
       }
       cur = cur->next;
   }
   xmlFreeDoc(doc);
   return EXIT_SUCCESS;
}

代码xml:

<msg><tag date="1556896362471" session="1702"><jpeg><xop:Include href="A"/></jpeg>/msg>

file.xml:1: namespace error : Namespace prefix xop on Include is not defined

这个标签没有用,但我每次都无法手动删除。

I have a problem of parsing my xml file, it is not correct with libxml but it's certified valid by the owner of application.

如果 XML 文件与您的问题中出现的完全一样,那么应用程序的所有者是错误的。任何符合 XML 的解析器在尝试解析 XML.

时都会遇到致命错误

I have tried to read the RFC to XOP but seems don't related with my problem.

你是对的,XOP 的细节与这个 lower-level 问题无关。

可能值得学习 XML 命名空间的基础知识,这样您可以更好地理解为什么 XML 不是 well-formed(特别是 xmlns 属性)。 https://www.w3schools.com/xml/xml_namespaces.asp