XML validation: Difference between revisions

Added FreeBASIC
m (syntax highlighting fixup automation)
(Added FreeBASIC)
 
(4 intermediate revisions by 3 users 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 187 ⟶ 242:
</pre>
<p>Changing <code>wrong</code> to a boolean, e. g. <code>true</code>, The result (without -w) is <pre>[[Error, 0]]</pre>
 
=={{header|FutureBasic}}==
FB has native XML validators.
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
include "Tlbx XML.incl"
 
// Sample XML courtesy https://www.w3schools.com/xml/schema_example.asp
local fn Sample1XMLDocument as CFStringRef
CFStringRef xmlDoc = @"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n¬
<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n¬
<xs:element name=\"shiporder\">\n¬
<xs:complexType>\n¬
<xs:sequence>\n¬
<xs:element name=\"orderperson\" type=\"xs:string\"/>\n¬
<xs:element name=\"shipto\">\n¬
<xs:complexType>\n¬
<xs:sequence>\n¬
<xs:element name=\"name\" type=\"xs:string\"/>\n¬
<xs:element name=\"address\" type=\"xs:string\"/>\n¬
<xs:element name=\"city\" type=\"xs:string\"/>\n¬
<xs:element name=\"country\" type=\"xs:string\"/>\n¬
</xs:sequence>\n¬
</xs:complexType>\n¬
</xs:element>\n¬
<xs:element name=\"item\" maxOccurs=\"unbounded\">\n¬
<xs:complexType>\n¬
<xs:sequence>\n¬
<xs:element name=\"title\" type=\"xs:string\"/>\n¬
<xs:element name=\"note\" type=\"xs:string\" minOccurs=\"0\"/>\n¬
<xs:element name=\"quantity\" type=\"xs:positiveInteger\"/>\n¬
<xs:element name=\"price\" type=\"xs:decimal\"/>\n¬
</xs:sequence>\n¬
</xs:complexType>\n¬
</xs:element>\n¬
</xs:sequence>\n¬
<xs:attribute name=\"orderid\" type=\"xs:string\" use=\"required\"/>\n¬
</xs:complexType>\n¬
</xs:element>\n¬
</xs:schema>"
end fn = xmlDoc
 
local fn ValidateXML( string as CFStringRef ) as BOOL
ErrorRef err = NULL
XMLDocumentRef xmlDoc = fn XMLDocumentWithXMLString( string, NSXMLDocumentValidate + NSXMLNodePreserveAll, err )
if err then NSLog( @"Error: %@", fn ErrorLocalizedDescription( err ) ) : exit fn
BOOL validXML = fn XMLDocumentValidate( xmlDoc, err )
if err then NSLog( @"Error: %@", fn ErrorLocalizedDescription( err ) ) else exit fn = validXML
end fn = NO
 
CFStringRef xmlDoc, xmlName
BOOL success
 
xmlDoc = fn Sample1XMLDocument
success = fn ValidateXML( xmlDoc )
xmlName = @"XML Sample No. 1"
if success then NSLog( @"%@: XML document is valid.\n", xmlName ) else NSLog( @"%@ XML document is invalid.\n", xmlName )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
XML Sample No. 1: XML document is valid.
</pre>
 
=={{header|Go}}==
Line 822 ⟶ 941:
is_valid_xml("<a>5<b>foobar</b></a>") : false
</pre>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Option explicit
 
Function fileexists(fn)
fileexists= CreateObject("Scripting.FileSystemObject").FileExists(fn)
End Function
 
Function xmlvalid(strfilename)
Dim xmldoc,xmldoc2,objSchemas
Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0")
If fileexists(Replace(strfilename,".xml",".dtd")) Then
xmlDoc.setProperty "ProhibitDTD", False
xmlDoc.setProperty "ResolveExternals", True
xmlDoc.validateOnParse = True
xmlDoc.async = False
xmlDoc.load(strFileName)
ElseIf fileexists(Replace(strfilename,".xml",".xsd")) Then
xmlDoc.setProperty "ProhibitDTD", True
xmlDoc.setProperty "ResolveExternals", True
xmlDoc.validateOnParse = True
xmlDoc.async = False
xmlDoc.load(strFileName)
'import xsd
Set xmlDoc2 = CreateObject("Msxml2.DOMDocument.6.0")
xmlDoc2.validateOnParse = True
xmlDoc2.async = False
xmlDoc2.load(Replace (strfilename,".xml",".xsd"))
'cache xsd
Set objSchemas = CreateObject("MSXML2.XMLSchemaCache.6.0")
objSchemas.Add "", xmlDoc2
Else
Set xmlvalid= Nothing:Exit Function
End If
Set xmlvalid=xmldoc.parseError
End Function
 
 
 
Sub displayerror (parserr) 'display the info returned by Msxml2
Dim strresult
If parserr is Nothing Then
strresult= "could not find dtd or xsd for " & strFileName
Else
With parserr
Select Case .errorcode
Case 0
strResult = "Valid: " & strFileName & vbCr
Case Else
strResult = vbCrLf & "ERROR! Failed to validate " & _
strFileName & vbCrLf &.reason & vbCr & _
"Error code: " & .errorCode & ", Line: " & _
.line & ", Character: " & _
.linepos & ", Source: """ & _
.srcText & """ - " & vbCrLf
End Select
End With
End If
WScript.Echo strresult
End Sub
 
'main
Dim strfilename
 
'testing validation with dtd
'strfilename="books.xml"
'displayerror xmlvalid (strfilename)
 
'testing validation with xsd
strfilename="shiporder.xml"
displayerror xmlvalid (strfilename)
</syntaxhighlight>
{{out}}
<small>
<pre>
Valid: shiporder.xml
</pre>
</small>
 
=={{header|Visual Basic .NET}}==
Line 958 ⟶ 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,010 ⟶ 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,152 ⟶ 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,171 ⟶ 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