-
Notifications
You must be signed in to change notification settings - Fork 0
/
test4.c
68 lines (58 loc) · 2.14 KB
/
test4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/xmlschemas.h>
int main() {
xmlDocPtr doc;
xmlSchemaPtr schema;
xmlSchemaParserCtxtPtr schemaParserCtxt;
xmlSchemaValidCtxtPtr schemaValidCtxt;
// User-supplied XML input (replace this with actual input)
const char* xmlData = "<root>Hello, world!</root>";
const char* xsdData = "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"><xs:element name=\"root\" type=\"xs:string\"/></xs:schema>";
// Parse XML document
doc = xmlReadMemory(xmlData, strlen(xmlData), NULL, NULL, 0);
if (doc == NULL) {
fprintf(stderr, "Error: Failed to parse XML document.\n");
return 1;
}
// Create XML schema parser context
schemaParserCtxt = xmlSchemaNewMemParserCtxt(xsdData, strlen(xsdData));
if (schemaParserCtxt == NULL) {
fprintf(stderr, "Error: Failed to create XML schema parser context.\n");
xmlFreeDoc(doc);
return 1;
}
// Parse XML schema
schema = xmlSchemaParse(schemaParserCtxt);
if (schema == NULL) {
fprintf(stderr, "Error: Failed to parse XML schema.\n");
xmlSchemaFreeParserCtxt(schemaParserCtxt);
xmlFreeDoc(doc);
return 1;
}
// Create XML schema validation context
schemaValidCtxt = xmlSchemaNewValidCtxt(schema);
if (schemaValidCtxt == NULL) {
fprintf(stderr, "Error: Failed to create XML schema validation context.\n");
xmlSchemaFree(schema);
xmlSchemaFreeParserCtxt(schemaParserCtxt);
xmlFreeDoc(doc);
return 1;
}
// Validate XML document against schema
if (xmlSchemaValidateDoc(schemaValidCtxt, doc) != 0) {
fprintf(stderr, "Error: XML document failed schema validation.\n");
xmlSchemaFreeValidCtxt(schemaValidCtxt);
xmlSchemaFree(schema);
xmlSchemaFreeParserCtxt(schemaParserCtxt);
xmlFreeDoc(doc);
return 1;
}
// Process XML data
// Free resources
xmlSchemaFreeValidCtxt(schemaValidCtxt);
xmlSchemaFree(schema);
xmlSchemaFreeParserCtxt(schemaParserCtxt);
xmlFreeDoc(doc);
return 0;
}