Schema validation

XML documents that reference a schema can be validated.

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

PS>Get-Item $pshomemodulesISEen-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 $pshomeSchemasPSMaml.

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

$path = Resolve-Path "$pshomemodulesISEen-US*-help.xml" 
[Xml]$document = Get-Content $path -Raw 
$document.Schemas.Add( 
    'http://schemas.microsoft.com/maml/2004/10', 
    "$pshomeSchemasPSMamlmaml.xsd" 
) 
$document.Validate( { 
param($sender, $eventArgs) 
 
if ($eventArgs.Severity -in 'Error', 'Warning') { 
        Write-Host $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 first is that 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.149.241.250