XML validation: Difference between revisions
Added FreeBASIC
(Added FreeBASIC) |
|||
(One intermediate revision by one other user not shown) | |||
Line 108:
}
</syntaxhighlight>
=={{header|FreeBASIC}}==
{{libheader|libxml2}}
<syntaxhighlight lang="vbnet">#include once "crt/stdio.bi"
#include once "crt/stdlib.bi"
#include once "libxml/tree.bi"
#include once "libxml/parser.bi"
'' Load the XML document
dim as xmlDocPtr doc = xmlReadFile("document.xml", NULL, 0)
if doc = NULL then
print "Error opening XML document."
end 1
end if
'' Load the XSD schema
dim as xmlSchemaParserCtxtPtr ctxt = xmlSchemaNewParserCtxt("schema.xsd")
if ctxt = NULL then
print "Error opening XSD schema."
xmlFreeDoc(doc)
end 1
end if
'' Parse the XSD schema
dim as xmlSchemaPtr schema = xmlSchemaParse(ctxt)
if schema = NULL then
print "Error parsing XSD schema."
xmlSchemaFreeParserCtxt(ctxt)
xmlFreeDoc(doc)
end 1
end if
'' Create a validation context
dim as xmlSchemaValidCtxtPtr vctxt = xmlSchemaNewValidCtxt(schema)
if vctxt = NULL then
print "Error creating validation context."
xmlSchemaFree(schema)
xmlSchemaFreeParserCtxt(ctxt)
xmlFreeDoc(doc)
end 1
end if
'' Validate the XML document against the XSD schema
if xmlSchemaValidateDoc(vctxt, doc) = 0 then
print "The XML document is valid according to the XSD schema."
else
print "The XML document is not valid according to the XSD schema."
end if
'' Free resources
xmlSchemaFreeValidCtxt(vctxt)
xmlSchemaFree(schema)
xmlSchemaFreeParserCtxt(ctxt)
xmlFreeDoc(doc)
xmlCleanupParser()</syntaxhighlight>
=={{header|F_Sharp|F#}}==
Line 1,106 ⟶ 1,161:
{{trans|Nim}}
{{libheader|libxml2}}
Although Wren has
embedded program so we can ask the C host to communicate with Libxml2 for us.
<syntaxhighlight lang="
class Args {
Line 1,158 ⟶ 1,213:
<br>
We now embed this in the following C program, compile and run it.
<syntaxhighlight lang="c">/* built with: gcc
#include <stdio.h>
Line 1,300 ⟶ 1,355:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 1,319 ⟶ 1,374:
{{out}}
Using command <code>./
<pre>'shiporder.xml' validates.</pre>
Using a modified file “shiporder1.xml” where tag “orderperson” has been replaced by “orderperson1”:
|