XML validation: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 8: | Line 8: | ||
{{libheader|LibXML}} |
{{libheader|LibXML}} |
||
At the time of writing, the XML and XSD files at the URLs used in the other examples were inaccessible. The files from the W3 Schools page were used for tests. |
At the time of writing, the XML and XSD files at the URLs used in the other examples were inaccessible. The files from the W3 Schools page were used for tests. |
||
<syntaxhighlight lang="c"> |
|||
<lang C> |
|||
#include <libxml/xmlschemastypes.h> |
#include <libxml/xmlschemastypes.h> |
||
Line 69: | Line 69: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output, files used from the W3 Schools page : |
Output, files used from the W3 Schools page : |
||
<pre> |
<pre> |
||
Line 77: | Line 77: | ||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
< |
<syntaxhighlight lang="csharp"> |
||
using System; |
using System; |
||
using System.Xml; |
using System.Xml; |
||
Line 107: | Line 107: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
<p>Using an inline stylesheet:</p> |
<p>Using an inline stylesheet:</p> |
||
< |
<syntaxhighlight lang="fsharp">open System.Xml |
||
open System.Xml.Schema |
open System.Xml.Schema |
||
open System.IO |
open System.IO |
||
Line 174: | Line 174: | ||
printfn "%A" (Seq.toList (vData None).Value) |
printfn "%A" (Seq.toList (vData None).Value) |
||
0 |
0 |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre style="white-space:pre-wrap">>RosettaCode |
<pre style="white-space:pre-wrap">>RosettaCode |
||
Line 192: | Line 192: | ||
<br> |
<br> |
||
This uses the w3schools test data linked to above. |
This uses the w3schools test data linked to above. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 241: | Line 241: | ||
fmt.Println("Validation of", xmlfile, "against", xsdfile, "successful!") |
fmt.Println("Validation of", xmlfile, "against", xsdfile, "successful!") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 252: | Line 252: | ||
Solution: |
Solution: |
||
< |
<syntaxhighlight lang="groovy">import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI |
||
import javax.xml.transform.stream.StreamSource |
import javax.xml.transform.stream.StreamSource |
||
import javax.xml.validation.SchemaFactory |
import javax.xml.validation.SchemaFactory |
||
Line 265: | Line 265: | ||
false |
false |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="groovy">def schemaLoc = "http://venus.eas.asu.edu/WSRepository/xml/Courses.xsd" |
||
def docLoc = "http://venus.eas.asu.edu/WSRepository/xml/Courses.xml" |
def docLoc = "http://venus.eas.asu.edu/WSRepository/xml/Courses.xml" |
||
println "Document is ${validate(schemaLoc, docLoc)? 'valid' : 'invalid'}"</ |
println "Document is ${validate(schemaLoc, docLoc)? 'valid' : 'invalid'}"</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; |
||
import java.net.MalformedURLException; |
import java.net.MalformedURLException; |
||
Line 345: | Line 345: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using LightXML |
||
const Xptr = LightXML.Xptr |
const Xptr = LightXML.Xptr |
||
Line 375: | Line 375: | ||
xsdvalidatexml() |
xsdvalidatexml() |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Line 389: | Line 389: | ||
</pre> |
</pre> |
||
Next, you need to compile the following Kotlin program, linking against libxml_schemas.klib. |
Next, you need to compile the following Kotlin program, linking against libxml_schemas.klib. |
||
< |
<syntaxhighlight lang="scala">// Kotlin Native v0.6 |
||
import kotlinx.cinterop.* |
import kotlinx.cinterop.* |
||
Line 455: | Line 455: | ||
xmlCleanupParser() |
xmlCleanupParser() |
||
xmlMemoryDump() |
xmlMemoryDump() |
||
}</ |
}</syntaxhighlight> |
||
Finally, the resulting .kexe file should be executed passing it similar command line arguments to the C entry to produce the following output. |
Finally, the resulting .kexe file should be executed passing it similar command line arguments to the C entry to produce the following output. |
||
<pre> |
<pre> |
||
Line 468: | Line 468: | ||
Our program is based on the C version with many differences. In particular, we use the function “xmlSchemaValidateFile” rather than the function “xmlSchemaValidateDoc”. |
Our program is based on the C version with many differences. In particular, we use the function “xmlSchemaValidateFile” rather than the function “xmlSchemaValidateDoc”. |
||
< |
<syntaxhighlight lang="nim">import os, strformat |
||
const LibXml = "libxml2.so" |
const LibXml = "libxml2.so" |
||
Line 519: | Line 519: | ||
else: |
else: |
||
echo &"“{xmlFilename}” fails to validate." |
echo &"“{xmlFilename}” fails to validate." |
||
validCtxt.xmlSchemaFreeValidCtxt()</ |
validCtxt.xmlSchemaFreeValidCtxt()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 530: | Line 530: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/env perl -T |
||
use 5.018_002; |
use 5.018_002; |
||
use warnings; |
use warnings; |
||
Line 563: | Line 563: | ||
} |
} |
||
}; |
}; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 578: | Line 578: | ||
The libxml.e wrapper was penned specifically for this task and is just about as |
The libxml.e wrapper was penned specifically for this task and is just about as |
||
bare-bones as it could ever possibly be, and win32-only, for now. |
bare-bones as it could ever possibly be, and win32-only, for now. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(notonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
<span style="color: #000080;font-style:italic;">-- |
||
-- demo\rosetta\Validate_XML.exw |
-- demo\rosetta\Validate_XML.exw |
||
Line 619: | Line 619: | ||
<span style="color: #000000;">xmlCleanupParser</span><span style="color: #0000FF;">()</span> |
<span style="color: #000000;">xmlCleanupParser</span><span style="color: #0000FF;">()</span> |
||
<span style="color: #000000;">xmlMemoryDump</span><span style="color: #0000FF;">()</span> |
<span style="color: #000000;">xmlMemoryDump</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 633: | Line 633: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php"> |
||
libxml_use_internal_errors(true); |
libxml_use_internal_errors(true); |
||
Line 644: | Line 644: | ||
echo 'success'; |
echo 'success'; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 674: | Line 674: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">#!/bin/python |
||
from __future__ import print_function |
from __future__ import print_function |
||
import lxml |
import lxml |
||
Line 702: | Line 702: | ||
root = etree.fromstring("<a>5<b>foobar</b></a>", parser) |
root = etree.fromstring("<a>5<b>foobar</b></a>", parser) |
||
except lxml.etree.XMLSyntaxError as err: |
except lxml.etree.XMLSyntaxError as err: |
||
print (err)</ |
print (err)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 711: | Line 711: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
<syntaxhighlight lang="raku" line> |
|||
<lang perl6> |
|||
# Reference: https://github.com/libxml-raku/LibXML-raku |
# Reference: https://github.com/libxml-raku/LibXML-raku |
||
Line 733: | Line 733: | ||
try { LibXML::Schema.new( string => $xsdschema ).validate( $x ) } |
try { LibXML::Schema.new( string => $xsdschema ).validate( $x ) } |
||
!$! ?? say "Valid." !! say $!.message() ; |
!$! ?? say "Valid." !! say $!.message() ; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Valid. |
<pre>Valid. |
||
Line 740: | Line 740: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">import java.net.URL |
||
import javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI |
import javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI |
||
Line 786: | Line 786: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">require('XML::LibXML') |
||
func is_valid_xml(str, schema) { |
func is_valid_xml(str, schema) { |
||
Line 816: | Line 816: | ||
[good_xml, bad_xml].each { |xml| |
[good_xml, bad_xml].each { |xml| |
||
say "is_valid_xml(#{xml.dump}) : #{is_valid_xml(xml, xmlschema_markup)}" |
say "is_valid_xml(#{xml.dump}) : #{is_valid_xml(xml, xmlschema_markup)}" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 827: | Line 827: | ||
{{works with|.NET Core|2.1}} |
{{works with|.NET Core|2.1}} |
||
< |
<syntaxhighlight lang="vbnet">Option Compare Binary |
||
Option Explicit On |
Option Explicit On |
||
Option Infer On |
Option Infer On |
||
Line 856: | Line 856: | ||
If errors.Count = 0 Then Console.WriteLine("The document is valid.") |
If errors.Count = 0 Then Console.WriteLine("The document is valid.") |
||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 864: | Line 864: | ||
An alternative is to use XmlReader (like the C# and F# examples [as of 2019-08-06]). |
An alternative is to use XmlReader (like the C# and F# examples [as of 2019-08-06]). |
||
< |
<syntaxhighlight lang="vbnet"> Function GetValidationErrorsXmlReader(doc As XDocument, schemaSet As XmlSchemaSet, warnings As Boolean) As IList(Of ValidationEventArgs) |
||
GetValidationErrorsReader = New List(Of ValidationEventArgs) |
GetValidationErrorsReader = New List(Of ValidationEventArgs) |
||
Line 879: | Line 879: | ||
Do While reader.Read() : Loop |
Do While reader.Read() : Loop |
||
End Using |
End Using |
||
End Function</ |
End Function</syntaxhighlight> |
||
Creating the documents (same as F#) from strings (does not handle syntax errors): |
Creating the documents (same as F#) from strings (does not handle syntax errors): |
||
< |
<syntaxhighlight lang="vbnet">Module Constants |
||
Const SCHEMA As String = |
Const SCHEMA As String = |
||
"<?xml version='1.0'?> |
"<?xml version='1.0'?> |
||
Line 918: | Line 918: | ||
Return XDocument.Parse(DOCUMENT) |
Return XDocument.Parse(DOCUMENT) |
||
End Function |
End Function |
||
End Module</ |
End Module</syntaxhighlight> |
||
Alternatively, we can be cheeky and use VB's XML literals... |
Alternatively, we can be cheeky and use VB's XML literals... |
||
< |
<syntaxhighlight lang="vbnet">Module Constants |
||
Function GetDocument() As XDocument |
Function GetDocument() As XDocument |
||
Return _ |
Return _ |
||
Line 953: | Line 953: | ||
</xs:schema> |
</xs:schema> |
||
End Function |
End Function |
||
End Module</ |
End Module</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Line 960: | Line 960: | ||
Wren has no XML support whatever, either built-in or (AFAIK) via third parties. We therefore use an |
Wren has no XML support whatever, either built-in or (AFAIK) via third parties. We therefore use an |
||
embedded program so we can ask the C host to communicate with Libxml2 for us. |
embedded program so we can ask the C host to communicate with Libxml2 for us. |
||
< |
<syntaxhighlight lang="ecmascript">/* xml_validation.wren */ |
||
class Args { |
class Args { |
||
Line 1,007: | Line 1,007: | ||
System.print("'%(xmlFilename)' fails to validate.") |
System.print("'%(xmlFilename)' fails to validate.") |
||
} |
} |
||
validCtxt.freeValidCtxt()</ |
validCtxt.freeValidCtxt()</syntaxhighlight> |
||
<br> |
<br> |
||
We now embed this in the following C program, compile and run it. |
We now embed this in the following C program, compile and run it. |
||
< |
<syntaxhighlight lang="c">/* built with: gcc xml_validation.c -o xml_validation -I/usr/include/libxml2 -lxml2 -lwren -lm */ |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 1,168: | Line 1,168: | ||
free(script); |
free(script); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |