Problem with Xml-Test

Topics: Developer Forum
Aug 6, 2012 at 7:26 PM
Edited Aug 6, 2012 at 7:27 PM

No matter what I do, the XML-Test function always returns true and i'm wondering what i'm doing wrong.

This is my XML

<html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///c:/DEV/S@/Developpement/Main/testValidator.xsd">  <head>Je suis un header</head></html>

 This is my XSD

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">  <xs:element name="html">    <xs:complexType>      <xs:sequence minOccurs="1">        <xs:element ref="head"/>      </xs:sequence>    </xs:complexType>  </xs:element>  <xs:element name="head" type="xs:string"/></xs:schema>

 

If i remove the head tag, i do not get an error, but if I remove a "<" it's blowing. It's like it's not taking into account my schema at all. I have the last version of the pscx and I try to validate my XMl that way in powershell

param (   [string]$cheminComplet = "C:\DEV\Main\test.xml",[string]$cheminCompletXsd = "C:\DEV\Main\testValidator.xsd"

)

Test-Xml $cheminComplet -SchemaPath $cheminCompletXsd -Verbose

Developer
Aug 7, 2012 at 12:40 AM

Weird. I don't have time to investigate right now, but try this validator:

 

   function Write-DebugTypeInfo {
      param($reader)
      switch($reader.SchemaType) {
         "XmlSchemaDatatype" {
            $value = $reader.ReadTypedValue()
            Write-Host ("{0}({1},{2}):{3}" -f $reader.NodeType, $reader.Name, $value.GetType().Name, $value)
         }
         "XmlSchemaSimpleType" {
            $value = $reader.ReadTypedValue()
            Write-Host ("{0}({1},{2}):{3}" -f $reader.NodeType, $reader.Name, $value.GetType().Name, $value)
         }
         "XmlSchemaComplexType" {
             Write-Host ("{0}({1},{2})" -f $reader.NodeType, $reader.Name, $reader.SchemaType.Name)
         }
         default {
             Write-Host ("{0}({1})" -f $reader.NodeType, $reader.Name)
         }
      }
   }

   function Validate-Xml {
      param($Path)

      $tr = New-Object System.Xml.XmlTextReader $Path
      try {
         $vr = New-Object System.Xml.XmlValidatingReader $tr

         $vr.ValidationType = "Schema"
         Register-ObjectEvent -InputObject $vr -SourceIdentifier SchemaValidator -EventName ValidationEventHandler -Action { 
            Write-Warning "***Validation error"
            Write-Warning ("`tSeverity:{0}" -f $eventArgs.Severity)
            Write-Warning ("`tMessage:{0}" -f $eventArgs.Message)
         }
      
         while($vr.Read())
         {
            Write-DebugTypeInfo $vr
            if($vr.NodeType -eq "Element")
            {
               while($vr.MoveToNextAttribute()) {
                  Write-DebugTypeInfo $vr
               }
            }
         }
         Write-Host "Validation finished"
      } finally {
         Write-Host "Cleaning up"
         Stop-Job -Name SchemaValidator 
         Remove-Job -Name SchemaValidator 
         $vr.Dispose()
         $tr.Dispose()
      }
   }
   
   Validate-Xml "C:\Users\Joel\Documents\WindowsPowershell\TestData\html.xml"
    

Coordinator
Aug 8, 2012 at 7:11 AM

On the PSCX 3.0 beta, I can't repro this.  Which version of PSCX are you using?

9> @'
<html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:noNamespaceSchemaLocation="file:///c:/users/keith/foo.xsd">
</html>
'@ > ~\foo.xml
#    <head>Je suis un header</head>
@'
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">  
    <xs:element name="html">    
        <xs:complexType>      
            <xs:sequence minOccurs="1">        
                <xs:element ref="head"/>      
            </xs:sequence>    
        </xs:complexType>  
    </xs:element>  
    <xs:element name="head" type="xs:string"/>
</xs:schema>
'@ > ~\foo.xsd
Test-Xml -Path foo.xml -SchemaPath foo.xsd
False 
10> @'
<html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:noNamespaceSchemaLocation="file:///c:/users/keith/foo.xsd">
    <head>Je suis un header</head>
</html>
'@ > ~\foo.xml


@'
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">  
    <xs:element name="html">    
        <xs:complexType>      
            <xs:sequence minOccurs="1">        
                <xs:element ref="head"/>      
            </xs:sequence>    
        </xs:complexType>  
    </xs:element>  
    <xs:element name="head" type="xs:string"/>
</xs:schema>
'@ > ~\foo.xsd

Test-Xml -Path foo.xml -SchemaPath foo.xsd
True

Aug 8, 2012 at 2:30 PM
Edited Aug 8, 2012 at 3:12 PM

I'm using the version 2.0 and your first function is working in my case. Should we use the BETA 3.0 instead of the production code ?

Coordinator
Aug 8, 2012 at 4:16 PM

If you're on PowerShell 2.0, please use the 2.1 Beta. I'm pretty sure I fixed a bug with XML validation in that drop.  If you're using PowerShell 3.0, please use the PSCX 3.0 Beta.  Click on the Downloads tab at the top of the page so you can see all the various releases of PSCX.

Aug 8, 2012 at 4:29 PM
Edited Aug 8, 2012 at 4:30 PM

I got this when I tried the 3.0 Beta

Import-Module : The 'C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Pscx.psd1' module cannot be imported because its manifest contains one or more members that are not valid. The valid manifest members are ('ModuleToProcess', 'NestedModules', 'GUID', 'Author', 'CompanyName', 'Copyright', 'ModuleVersion', 'Description', 'PowerShellVersion', 'PowerShellHostName', 'PowerShellHostVersion', 'CLRVersion', 'DotNetFrameworkVersion', 'ProcessorArchitecture', 'RequiredModules', 'TypesToProcess', 'FormatsToProcess', 'ScriptsToProcess', 'PrivateData', 'RequiredAssemblies', 'ModuleList', 'FileList', 'FunctionsToExport', 'VariablesToExport', 'AliasesToExport', 'CmdletsToExport'). Remove the members that are not valid ('RootModule'), then try to import the module again.At line:1 char:14+ Import-Module <<<<  Pscx    + CategoryInfo          : InvalidData: (C:\Program File...\Pscx\Pscx.psd1:String) [Import-Module], InvalidOperationException    + FullyQualifiedErrorId : Modules_InvalidManifestMember,Microsoft.PowerShell.Commands.ImportModuleCommand

Developer
Aug 8, 2012 at 6:19 PM
Which means you're not on PowerShell 3 yet.
--
Joel "Jaykul" Bennett
http://HuddledMasses.org
http://PowerShellGroup.org


On Wed, Aug 8, 2012 at 11:29 AM, jfrancois <notifications@codeplex.com> wrote:

From: jfrancois

I got this when I tried the 3.0 Beta

Import-Module : The 'C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Pscx.psd1' module cannot be imported because its manifest contains one or more members that are not valid. The valid manifest members are ('ModuleToProcess', 'NestedModules', 'GUID', 'Author', 'CompanyName', 'Copyright', 'ModuleVersion', 'Description', 'PowerShellVersion', 'PowerShellHostName', 'PowerShellHostVersion', 'CLRVersion', 'DotNetFrameworkVersion', 'ProcessorArchitecture', 'RequiredModules', 'TypesToProcess', 'FormatsToProcess', 'ScriptsToProcess', 'PrivateData', 'RequiredAssemblies', 'ModuleList', 'FileList', 'FunctionsToExport', 'VariablesToExport', 'AliasesToExport', 'CmdletsToExport'). Remove the members that are not valid ('RootModule'), then try to import the module again.At line:1 char:14+ Import-Module <<<< Pscx + CategoryInfo : InvalidData: (C:\Program File...\Pscx\Pscx.psd1:String) [Import-Module], InvalidOperationException + FullyQualifiedErrorId : Modules_InvalidManifestMember,Microsoft.PowerShell.Commands.ImportModuleCommand

Read the full discussion online.

To add a post to this discussion, reply to this email (Pscx@discussions.codeplex.com)

To start a new discussion for this project, email Pscx@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Coordinator
Aug 8, 2012 at 6:46 PM

As Jaykul says, you're running PowerShell 2.0, in that case grab PSCX 2.1 Beta from here: http://pscx.codeplex.com/releases/view/73566   This drop fixes the issue with Test-Xml schema validation not working.

Aug 15, 2012 at 2:41 PM

Thanks it's working great now !