XML validation: Difference between revisions
Content added Content deleted
(→{{header|Java}}: Tidy up a bit more, make more useful for others) |
(Added F# version) |
||
Line 35: | Line 35: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|F_Sharp|F#}}== |
|||
<p>Using an inline stylesheet:</p> |
|||
<lang fsharp>open System.Xml |
|||
open System.Xml.Schema |
|||
open System.IO |
|||
let xml = @"<root> |
|||
<!--Start of schema--> |
|||
<xs:schema id='an-element' targetNamespace='example' xmlns:mstns='example' xmlns='example' xmlns:xs='http://www.w3.org/2001/XMLSchema' attributeFormDefault='unqualified' elementFormDefault='qualified'> |
|||
<xs:element name='an-element'> |
|||
<xs:complexType> |
|||
<xs:sequence minOccurs='0' maxOccurs='unbounded'> |
|||
<xs:element name='another-element' nillable='true'> |
|||
<xs:complexType> |
|||
<xs:simpleContent> |
|||
<xs:extension base='xs:string'> |
|||
<xs:attribute name='an-attribute' form='unqualified' type='xs:boolean' /> |
|||
</xs:extension> |
|||
</xs:simpleContent> |
|||
</xs:complexType> |
|||
</xs:element> |
|||
</xs:sequence> |
|||
</xs:complexType> |
|||
</xs:element> |
|||
</xs:schema> |
|||
<!--End of schema--> |
|||
<an-element xmlns='example'> |
|||
<another-element an-attribute='false'>...</another-element> |
|||
<another-element an-attribute='wrong'>123</another-element> |
|||
</an-element> |
|||
</root>" |
|||
let validationData withWarnings = |
|||
let errors = ref 0 |
|||
let warnings = ref 0 |
|||
fun input -> |
|||
match input with |
|||
| Some(msg, severity) -> |
|||
if severity = XmlSeverityType.Error then |
|||
errors := !errors + 1 |
|||
printfn "Validation error: %s" msg |
|||
elif withWarnings then |
|||
warnings := !warnings + 1 |
|||
printfn "Validation warning: %s" msg |
|||
None |
|||
| None -> |
|||
if withWarnings then |
|||
Some(dict[XmlSeverityType.Error, !errors; XmlSeverityType.Warning, !warnings]) |
|||
else |
|||
Some(dict[XmlSeverityType.Error, !errors]) |
|||
[<EntryPoint>] |
|||
let main argv = |
|||
let withWarnings = argv.Length > 0 && argv.[0] = "-w" |
|||
let vData = validationData withWarnings |
|||
let validationEvent = new ValidationEventHandler(fun _ e -> |
|||
vData (Some(e.Message, e.Severity)) |> ignore) |
|||
let settings = new XmlReaderSettings() |
|||
settings.ValidationType <- ValidationType.Schema |
|||
settings.ValidationEventHandler.AddHandler(validationEvent) |
|||
settings.ValidationFlags <- settings.ValidationFlags ||| XmlSchemaValidationFlags.ProcessInlineSchema ||| XmlSchemaValidationFlags.ReportValidationWarnings |
|||
let reader = XmlReader.Create(new StringReader(xml), settings); |
|||
while reader.Read() do () |
|||
0 |
|||
</lang> |
|||
{{out}} |
|||
<pre style="white-space:pre-wrap">>RosettaCode |
|||
Validation error: The 'an-attribute' element is invalid - The value 'wrong' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:boolean' - The string 'wrong' is not a valid boolean value. |
|||
[[Error, 1]] |
|||
>RosettaCode -w |
|||
Validation warning: Could not find schema information for the element 'root'. |
|||
Validation error: The 'an-attribute' element is invalid - The value 'wrong' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:boolean' - The string 'wrong' is not a valid boolean value. |
|||
[[Error, 1]; [Warning, 1]] |
|||
</pre> |
|||
<p>Changing <code>wrong</code> to a boolean, e. g. <code>true</code>, The result (without -w) is <pre>[[Error, 0]]</pre> |
|||
=={{header|Java}}== |
=={{header|Java}}== |