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 nothird XMLparty support whateverfor XML, eitherit built-indoes ornot (AFAIK)support viavalidation thirdagainst an XSD partiesschema. We therefore use an
embedded program so we can ask the C host to communicate with Libxml2 for us.
<syntaxhighlight lang="ecmascriptwren">/* xml_validationXML_validation.wren */
 
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 xml_validationXML_validation.c -o xml_validationXML_validation -I/usr/include/libxml2 -lxml2 -lwren -lm */
 
#include <stdio.h>
Line 1,300 ⟶ 1,355:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "xml_validationXML_validation.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 1,319 ⟶ 1,374:
 
{{out}}
Using command <code>./xml_validationXML_validation shiporder.xml shiporder.xsd</code>:
<pre>'shiporder.xml' validates.</pre>
Using a modified file “shiporder1.xml” where tag “orderperson” has been replaced by “orderperson1”:
2,130

edits