Do czego konkretnie odnosi się opcja "pedantic parser"

0

Czytam sobie kod źródłowy libxml2: https://gitlab.gnome.org/search?search=XML_PARSE_PEDANTIC&nav_source=navbar&project_id=1665&group_id=8&search_code=true&repository_ref=master

I próbuję ustalić co to konkretnie znaczy "pedantic parser". Tzn jaki kod XML/HTML byłby sparsowany inaczej zależnie od tej opcji - co ona zmienia dokładnie.

Albo może gdzie szukać takich informacji?

Znalazłem jeszcze to issue: https://gitlab.gnome.org/GNOME/libxml2/-/issues/418 ale nic tam nie jest wyjaśnione.

0

Zakładam (nie chce mi się teraz testować), że najprostszy przykład to będzie dokument w stylu:

<br>

Tutaj brakuje znacznika zamykającego <br>.

0
hauleth napisał(a):

Zakładam (nie chce mi się teraz testować), że najprostszy przykład to będzie dokument w stylu:

<br>

Tutaj brakuje znacznika zamykającego <br>.

Nope.

Działa tak samo niezależnie od tego czy przekażę XML_PARSE_PEDANTIC czy nie.

2

Nie wiem, czy to prawda, ale brzmi sensownie:

The XML_PARSE_PEDANTIC flag in libxml2 is used when parsing an XML document. When this flag is set, the parser will enforce strict conformance to the XML specification and will return an error if it encounters any deviations from the specification. This flag is useful when parsing XML documents that are meant to be used in a production environment and need to adhere to strict standards.

With the XML_PARSE_PEDANTIC flag set, the parser will check for things like proper use of encoding, well-formedness of the document structure, proper use of namespaces, and proper use of attribute values. If any of these checks fail, the parser will return an error and stop parsing the document.

This flag is in contrast to the XML_PARSE_RECOVER flag, which allows the parser to recover from errors and continue parsing the document, even if it deviates from the XML specification. The XML_PARSE_RECOVER flag is useful when parsing XML documents that may have been created by hand or by an application that is not strict about adhering to the XML specification.

Źródło oczywiste

0
slsy napisał(a):

Nie wiem, czy to prawda, ale brzmi sensownie:

The XML_PARSE_PEDANTIC flag in libxml2 is used when parsing an XML document. When this flag is set, the parser will enforce strict conformance to the XML specification and will return an error if it encounters any deviations from the specification. This flag is useful when parsing XML documents that are meant to be used in a production environment and need to adhere to strict standards.

With the XML_PARSE_PEDANTIC flag set, the parser will check for things like proper use of encoding, well-formedness of the document structure, proper use of namespaces, and proper use of attribute values. If any of these checks fail, the parser will return an error and stop parsing the document.

This flag is in contrast to the XML_PARSE_RECOVER flag, which allows the parser to recover from errors and continue parsing the document, even if it deviates from the XML specification. The XML_PARSE_RECOVER flag is useful when parsing XML documents that may have been created by hand or by an application that is not strict about adhering to the XML specification.

Z tą różnicą, że nawet jak wrzucę niepoprawne tagi lub namespace'y i atrybuty, to cały output i errory są takie same niezależnie od tego czy przekażę tą flagę czy nie.

Zaczynam podejrzewać że parsowanie z defaulta jest "pedantic", i przekazanie tej flagi nic nie zmienia.

1

Grzebiąc po kodzie widać, że ctxt->pedantic jest sprawdzany tutaj w związku z z czymś co się nazywa entity declarations.

Dublując taki entity:

<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE author [
  <!ELEMENT author (#PCDATA)>
  <!ENTITY js "Jo Smith">
  <!ENTITY js "Jo Smith">
]>
<author>&js;</author>

Kod

#include <stdlib.h>
#include <stdio.h>

#include <libxml/parser.h>
#include <libxml/tree.h>

int main(int argc, char **argv) {
    xmlDoc *doc = NULL;
    xmlNode *root_element = NULL;

    if (argc != 2)
        return(1);

    /*
     * this initialize the library and check potential ABI mismatches
     * between the version it was compiled for and the actual shared
     * library used.
     */
    LIBXML_TEST_VERSION

    /*parse the file and get the DOM */
    doc = xmlReadFile(argv[1], NULL,  XML_PARSE_PEDANTIC);

    if (doc == NULL) {
        printf("error: could not parse file %s\n", argv[1]);
    }

    /*free the document */
    xmlFreeDoc(doc);

    /*
     *Free the global variables that may
     *have been allocated by the parser.
     */
    xmlCleanupParser();

    return 0;
}

Zwraca mi

$ g++ main.cpp -o main -I/usr/include/libxml2/ -lxml2
$ ./main /tmp/test.xml 
/tmp/test.xml:5: parser warning : Entity(js) already defined in the internal subset
  <!ENTITY js "Jo Smith">

A bez flagi nie.

1
Spearhead napisał(a):

Grzebiąc po kodzie widać, że ctxt->pedantic jest sprawdzany tutaj w związku z z czymś co się nazywa entity declarations.

Dublując taki entity:

<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE author [
  <!ELEMENT author (#PCDATA)>
  <!ENTITY js "Jo Smith">
  <!ENTITY js "Jo Smith">
]>
<author>&js;</author>

Ja dla tego inputu dostaje

DOCTYPE improperly terminated\n
htmlParseStartTag: invalid element name\n
htmlParseStartTag: invalid element name\n
htmlParseStartTag: invalid element name\n
Tag author invalid
2

https://gitlab.gnome.org/GNOME/libxml2/-/blob/master/SAX2.c#L643
wyszukaj sobie ->pedantic i spróbuj rozkminić
Szkoda, że nie ma na to testów, bo wtedy było by to jasne.

0
MarekR22 napisał(a):

https://gitlab.gnome.org/GNOME/libxml2/-/blob/master/SAX2.c#L643
wyszukaj sobie ->pedantic i spróbuj rozkminić
Szkoda, że nie ma na to testów, bo wtedy było by to jasne.

No sam podlinkowałem to repo, i szukałem tam info; i bardzo mało tam znalazłem. Atrybut jest użyty właściwie 5 razy w całym repo.

Głównie chciałbym znaleźć jakieś wystąpienie XML które działa z flagą, a nie działa bez. To co podesłał @Spearhead zwraca mi ten sam output dla obu.

2
Riddle napisał(a):
Spearhead napisał(a):

Grzebiąc po kodzie widać, że ctxt->pedantic jest sprawdzany tutaj w związku z z czymś co się nazywa entity declarations.

Dublując taki entity:

<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE author [
  <!ELEMENT author (#PCDATA)>
  <!ENTITY js "Jo Smith">
  <!ENTITY js "Jo Smith">
]>
<author>&js;</author>

Ja dla tego inputu dostaje

DOCTYPE improperly terminated\n
htmlParseStartTag: invalid element name\n
htmlParseStartTag: invalid element name\n
htmlParseStartTag: invalid element name\n
Tag author invalid

Bo używasz parsowania html zamiast xml. Jak zamienię w swoim kodzie xmlReadFile na htmlReadFile (wymaga podlinkowania #include <libxml/HTMLparser.h>) to mój output to również:

$ ./main /tmp/test.xml 
/tmp/test.xml:2: HTML parser error : DOCTYPE improperly terminated
<!DOCTYPE author [
                 ^
/tmp/test.xml:3: HTML parser error : htmlParseStartTag: invalid element name
  <!ELEMENT author (#PCDATA)>
   ^
/tmp/test.xml:4: HTML parser error : htmlParseStartTag: invalid element name
  <!ENTITY js "Jo Smith">
   ^
/tmp/test.xml:5: HTML parser error : htmlParseStartTag: invalid element name
  <!ENTITY js "Jo Smith">
   ^
/tmp/test.xml:7: HTML parser error : Tag author invalid
<author>&js;</author>

Z flagą czy bez.

0

Może przykłady z różna definicją przestrzeni nazw? Pusty, niepoprawne URI, coś co wygląda na poprawne URI.

<?xml version='1.0' encoding="UTF-8"?>
<Document xmlns:foobar="">
	<foobar:a>1</foobar:a>
</Document> 

vs

<?xml version='1.0' encoding="UTF-8"?>
<Document xmlns:foobar="111">
	<foobar:a>1</foobar:a>
</Document> 

vs

<?xml version='1.0' encoding="UTF-8"?>
<Document xmlns:foobar="http://foo.bar/foobar.xsd">
	<foobar:a>1</foobar:a>
</Document> 
0
Spearhead napisał(a):

Bo używasz parsowania html zamiast xml. Jak zamienię w swoim kodzie xmlReadFile na htmlReadFile (wymaga podlinkowania #include <libxml/HTMLparser.h>) to mój output to również:

$ ./main /tmp/test.xml 
/tmp/test.xml:2: HTML parser error : DOCTYPE improperly terminated
<!DOCTYPE author [
                 ^
/tmp/test.xml:3: HTML parser error : htmlParseStartTag: invalid element name
  <!ELEMENT author (#PCDATA)>
   ^
/tmp/test.xml:4: HTML parser error : htmlParseStartTag: invalid element name
  <!ENTITY js "Jo Smith">
   ^
/tmp/test.xml:5: HTML parser error : htmlParseStartTag: invalid element name
  <!ENTITY js "Jo Smith">
   ^
/tmp/test.xml:7: HTML parser error : Tag author invalid
<author>&js;</author>

Z flagą czy bez.

No właśnie, a mi chodzi o HTML, nie o XML.

Spearhead napisał(a):

Z flagą czy bez.

No własnie, więc pytanie czy ta flaga ma jakikolwiek wpływ jak się parsuje HTML?

1
MarekR22 napisał(a):

Szkoda, że nie ma na to testów, bo wtedy było by to jasne.

A jednak jest na to jakiś test, tylko trudno go znaleźć i zrozumieć https://gitlab.gnome.org/GNOME/libxml2/-/blob/master/testapi.c#L13828-13857

1

No własnie, więc pytanie czy ta flaga ma jakikolwiek wpływ jak się parsuje HTML?

Próbuję i nie bardzo się udaje nic znaleźć. Gołe HTML nie umożliwia definiowania własnych entities.

FWIW, to również zwraca błąd gdy się parsuje z flagą przez parser XML:

<a xml:lang="?" href="xyz" hreflang="de">Click for German</a>

Parser html nie widzi różnicy.

1 użytkowników online, w tym zalogowanych: 0, gości: 1