Schema validation

XML documents that reference a schema can be validated.

.NET Core and schema validation

.NET Core appears to be unwilling to expand include references in an XML schema. This apparent bug is exhibited in PowerShell Core. Windows PowerShell will produce schema validation errors; PowerShell Core will not at this time.

Windows PowerShell comes with a number of XML files with associated schema in the help files. For example, the help file for ISE is available:

PS> Get-Item C:WindowsSystem32WindowsPowerShellv1.0modulesISEen-USISE-help.xml 
 
    Directory: C:WindowsSystem32WindowsPowerShellv1.0modulesISEen-US 
 
Mode                LastWriteTime         Length Name                                                                 
----                -------------         ------ ----                                                                 
-a----         29/11/16     07:57          33969 ISE-help.xml

The schema documents used by the help content are saved in C:WindowsSystem32WindowsPowerShellv1.0SchemasPSMaml.

The following snippet may be used to load the schema files and then test the content of the document:

$path = 'C:WindowsSystem32WindowsPowerShellv1.0modulesISEen-USISE-help.xml'

$document = [Xml]::new()
$document.Load($path)

# Add the schema to the XmlDocument
$document.Schemas.Add(
'http://schemas.microsoft.com/maml/2004/10',
'C:WindowsSystem32WindowsPowerShellv1.0SchemasPSMamlmaml.xsd'
)

# Validate the document
$ErrorsAndWarnings = [System.Collections.Generic.List[String]]::new()
$document.Validate({
param ($sender, $eventArgs)

if ($eventArgs.Severity -in 'Error', 'Warning') {
$Global:ErrorsAndWarnings.Add($eventArgs.Message)
}
})

The argument for Validate is a script block that is executed each time an error is encountered. Write-Host is used to print a message to the console. A value cannot be directly returned as the script block is executed in the background.

Line number and line position information is not available using this technique for a number of reasons. The XmlDocument object is built from a string (returned by Get-Content) and not attached to the file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.68.236