Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I want to comment that the script in current accepted answer doesn't validate errors about incorrect orders of elements of <code>xs:sequence</code>. For example: test.xml</p> <pre><code>&lt;addresses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation='test.xsd'&gt; &lt;address&gt; &lt;street&gt;Baker street 5&lt;/street&gt; &lt;name&gt;Joe Tester&lt;/name&gt; &lt;/address&gt; &lt;/addresses&gt; </code></pre> <p>test.xsd</p> <pre><code>&lt;xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'&gt; &lt;xs:element name="addresses"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element ref="address" minOccurs='1' maxOccurs='unbounded'/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="address"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element ref="name" minOccurs='0' maxOccurs='1'/&gt; &lt;xs:element ref="street" minOccurs='0' maxOccurs='1'/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="name" type='xs:string'/&gt; &lt;xs:element name="street" type='xs:string'/&gt; &lt;/xs:schema&gt; </code></pre> <p>I wrote another version that can report this error:</p> <pre><code>function Test-XmlFile { &lt;# .Synopsis Validates an xml file against an xml schema file. .Example PS&gt; dir *.xml | Test-XmlFile schema.xsd #&gt; [CmdletBinding()] param ( [Parameter(Mandatory=$true)] [string] $SchemaFile, [Parameter(ValueFromPipeline=$true, Mandatory=$true, ValueFromPipelineByPropertyName=$true)] [alias('Fullname')] [string] $XmlFile, [scriptblock] $ValidationEventHandler = { Write-Error $args[1].Exception } ) begin { $schemaReader = New-Object System.Xml.XmlTextReader $SchemaFile $schema = [System.Xml.Schema.XmlSchema]::Read($schemaReader, $ValidationEventHandler) } process { $ret = $true try { $xml = New-Object System.Xml.XmlDocument $xml.Schemas.Add($schema) | Out-Null $xml.Load($XmlFile) $xml.Validate({ throw ([PsCustomObject] @{ SchemaFile = $SchemaFile XmlFile = $XmlFile Exception = $args[1].Exception }) }) } catch { Write-Error $_ $ret = $false } $ret } end { $schemaReader.Close() } } </code></pre> <p>PS C:\temp\lab-xml-validation> dir test.xml | Test-XmlFile test.xsd</p> <pre><code>System.Xml.Schema.XmlSchemaValidationException: The element 'address' has invalid child element 'name'. ... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload