XML validation: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) m syntax highlighting fixup automation |
|||
Line 8:
{{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.
<syntaxhighlight lang="c">
#include <libxml/xmlschemastypes.h>
Line 69:
return 0;
}
</syntaxhighlight>
Output, files used from the W3 Schools page :
<pre>
Line 77:
=={{header|C sharp}}==
<
using System;
using System.Xml;
Line 107:
}
}
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
<p>Using an inline stylesheet:</p>
<
open System.Xml.Schema
open System.IO
Line 174:
printfn "%A" (Seq.toList (vData None).Value)
0
</syntaxhighlight>
{{out}}
<pre style="white-space:pre-wrap">>RosettaCode
Line 192:
<br>
This uses the w3schools test data linked to above.
<
import (
Line 241:
fmt.Println("Validation of", xmlfile, "against", xsdfile, "successful!")
}</
{{out}}
Line 252:
Solution:
<
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.SchemaFactory
Line 265:
false
}
}</
Test:
<
def docLoc = "http://venus.eas.asu.edu/WSRepository/xml/Courses.xml"
println "Document is ${validate(schemaLoc, docLoc)? 'valid' : 'invalid'}"</
=={{header|Java}}==
<
import java.net.MalformedURLException;
Line 345:
}
}
}</
=={{header|Julia}}==
<
const Xptr = LightXML.Xptr
Line 375:
xsdvalidatexml()
</syntaxhighlight>
=={{header|Kotlin}}==
Line 389:
</pre>
Next, you need to compile the following Kotlin program, linking against libxml_schemas.klib.
<
import kotlinx.cinterop.*
Line 455:
xmlCleanupParser()
xmlMemoryDump()
}</
Finally, the resulting .kexe file should be executed passing it similar command line arguments to the C entry to produce the following output.
<pre>
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”.
<
const LibXml = "libxml2.so"
Line 519:
else:
echo &"“{xmlFilename}” fails to validate."
validCtxt.xmlSchemaFreeValidCtxt()</
{{out}}
Line 530:
=={{header|Perl}}==
<
use 5.018_002;
use warnings;
Line 563:
}
};
}</
{{out}}
Line 578:
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.
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Validate_XML.exw
Line 619:
<span style="color: #000000;">xmlCleanupParser</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">xmlMemoryDump</span><span style="color: #0000FF;">()</span>
<!--</
{{out}}
<pre>
Line 633:
=={{header|PHP}}==
<
libxml_use_internal_errors(true);
Line 644:
echo 'success';
}
</syntaxhighlight>
{{out}}
<pre>
Line 674:
=={{header|Python}}==
<
from __future__ import print_function
import lxml
Line 702:
root = etree.fromstring("<a>5<b>foobar</b></a>", parser)
except lxml.etree.XMLSyntaxError as err:
print (err)</
{{out}}
Line 711:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku" line>
# Reference: https://github.com/libxml-raku/LibXML-raku
Line 733:
try { LibXML::Schema.new( string => $xsdschema ).validate( $x ) }
!$! ?? say "Valid." !! say $!.message() ;
}</
{{out}}
<pre>Valid.
Line 740:
=={{header|Scala}}==
<
import javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI
Line 786:
}
}
}</
=={{header|Sidef}}==
{{trans|Perl}}
<
func is_valid_xml(str, schema) {
Line 816:
[good_xml, bad_xml].each { |xml|
say "is_valid_xml(#{xml.dump}) : #{is_valid_xml(xml, xmlschema_markup)}"
}</
{{out}}
<pre>
Line 827:
{{works with|.NET Core|2.1}}
<
Option Explicit On
Option Infer On
Line 856:
If errors.Count = 0 Then Console.WriteLine("The document is valid.")
End Sub
End Module</
{{out}}
Line 864:
An alternative is to use XmlReader (like the C# and F# examples [as of 2019-08-06]).
<
GetValidationErrorsReader = New List(Of ValidationEventArgs)
Line 879:
Do While reader.Read() : Loop
End Using
End Function</
Creating the documents (same as F#) from strings (does not handle syntax errors):
<
Const SCHEMA As String =
"<?xml version='1.0'?>
Line 918:
Return XDocument.Parse(DOCUMENT)
End Function
End Module</
Alternatively, we can be cheeky and use VB's XML literals...
<
Function GetDocument() As XDocument
Return _
Line 953:
</xs:schema>
End Function
End Module</
=={{header|Wren}}==
Line 960:
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.
<
class Args {
Line 1,007:
System.print("'%(xmlFilename)' fails to validate.")
}
validCtxt.freeValidCtxt()</
<br>
We now embed this in the following C program, compile and run it.
<
#include <stdio.h>
Line 1,168:
free(script);
return 0;
}</
{{out}}
|