Recent Changes - Search:

Accueil

OpenSSL

SyncML

Apache Portable Runtime

Libxml2

Net-snmp

CUrl

Boost

Perl

ZLib

Samba

VPN

Serveurs de messagerie

edit

Libxml2/Libxml2-schema

Libxml2.Libxml2-schema History

Hide minor edits - Show changes to output

January 17, 2008, at 09:00 AM by Arnaud Grandville -
Changed lines 6-7 from:
Dans l'exemple ci-dessous, le subject a été remplacé par erreur un item heading, ce qui provoque la détection suivante :\\
Schemas validity error : Element 'heading': This element is not expected. Expected is ( subject ).
to:
Dans l'exemple ci-dessous, le subject a été remplacé par erreur par un item heading, ce qui provoque la détection suivante :\\
Schemas validity error : Element 'heading': This element is not expected. Expected is ( subject ).\\
Added lines 62-72:
\\
\\
Quelques liens:\\
Pour l'apprentissage de la syntaxe XSD\\
http://www.w3schools.com/schema/default.asp
\\
Pour l'édition sous Windows de tels fichiers\\
[[http://www.liquid-technologies.com/|Liquid XML Studio 2008 (Freeware)]]


January 17, 2008, at 08:55 AM by Arnaud Grandville -
Added lines 1-61:
La validation de la grammaire d'un fichier XML est nécessaire lorsqu'il s'agit d'échanges des données, car la validation de la grammaire assurera que la totalité des données attendues a été fournit ainsi que la nature de ces données.\\

J'utilise la librairie [[http://xmlsoft.org/html/libxml-xmlschemas.html|Schema]] de libxml2 \\

Imaginons que vous souhaitiez collecter des messages du style d'email au format XML. Hors la problématique des caractères incompatibles (&,',<,> devant être remplacés respectivement par &amp;,&quot;,&lt;,&gt;) vous pourriez vous attendre à recevoir une structure  contenant quatre éléments, from, to, subject et body.\\
Dans l'exemple ci-dessous, le subject a été remplacé par erreur un item heading, ce qui provoque la détection suivante :\\
Schemas validity error : Element 'heading': This element is not expected. Expected is ( subject ).
nous avons ainsi détecté rapidement et simplement un échange de données incorrect.\\


Maintenant un exemple de cette implémentation:\\

(:source lang=C :)

char szXSD[]="<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>"
"<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
"<xs:element name=\"message\">"
"<xs:complexType>"
"<xs:sequence>"
"<xs:element name=\"to\" type=\"xs:string\"/>"
"<xs:element name=\"from\" type=\"xs:string\"/>"
"<xs:element name=\"subject\" type=\"xs:string\"/>"
"<xs:element name=\"body\" type=\"xs:string\"/>"
"</xs:sequence>"
"</xs:complexType>"
"</xs:element>"
"</xs:schema>";

char szXml[]="<?xml version=\"1.0\"?>"
"<message>"
"<to>Tove</to>"
"<from>Jani</from>"
"<heading>Reminder</heading>"
"<body>Don't forget me this weekend!</body>"
"</message>";



xmlSchemaParserCtxtPtr XSDCtxPtr = xmlSchemaNewMemParserCtxt(szXSD,strlen(szXSD));
if (XSDCtxPtr == NULL) {
printf("erreur");
return NOK;
}
xmlSchemaSetParserErrors(XSDCtxPtr, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr);

xmlSchemaPtr schema = xmlSchemaParse(XSDCtxPtr);
xmlSchemaValidCtxtPtr schemaCtxt;
schemaCtxt = xmlSchemaNewValidCtxt(schema);
xmlSchemaSetValidErrors(schemaCtxt, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr);

xmlParserInputBufferPtr input = xmlParserInputBufferCreateMem ((const char*)szXml, strlen(szXml), XML_CHAR_ENCODING_NONE);

int ret;
ret= xmlSchemaValidateStream(schemaCtxt, input, XML_CHAR_ENCODING_NONE,NULL, NULL);
printf("%i---\n",ret);

xmlSchemaFreeValidCtxt(schemaCtxt);
xmlSchemaFreeParserCtxt(XSDCtxPtr);
xmlSchemaFree(schema);

(:sourcend:)
Edit - History - Print - Recent Changes - Search
Page last modified on January 17, 2008, at 09:00 AM