2017-01-24 6 views
0

でXSDスキーマに対するオプションの属性の検証、私はオプションの属性に関する警告でPowerShellスクリプトによってXSDに対してXMLを検証する必要があり、 例えば、私のxsd:などのPowerShell

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 


    <xs:complexType name="LocalizedValue"> 
    <xs:attribute name="Lang" type="xs:string" use="required"> 
     <xs:annotation> 
     <xs:documentation>Language Code</xs:documentation> 
     </xs:annotation> 
    </xs:attribute> 
    <xs:attribute name="Name" type="xs:string" use="required"> 
     <xs:annotation> 
     <xs:documentation>District Typr</xs:documentation> 
     </xs:annotation> 
    </xs:attribute> 
    <xs:attribute name="ShortName" type="xs:string" use="optional"> 
     <xs:annotation> 
     <xs:documentation>Аbbreviation</xs:documentation> 
     </xs:annotation> 
    </xs:attribute> 
    </xs:complexType> 

    <xs:complexType name="DistrictType"> 
    <xs:sequence> 
     <xs:element name="Localizations" minOccurs="1" maxOccurs="1"> 
     <xs:complexType> 
      <xs:sequence> 
      <xs:element name="Localization" type="LocalizedValue" minOccurs="1" maxOccurs="unbounded"> 
       <xs:annotation> 
       <xs:documentation>Local attributes</xs:documentation> 
       </xs:annotation> 
      </xs:element> 
      </xs:sequence> 
     </xs:complexType> 
     </xs:element> 
     <xs:element name="Branches"> 
     <xs:complexType> 
      <xs:sequence> 
      <xs:element name="Branch" minOccurs="0" maxOccurs="unbounded"> 
       <xs:complexType> 
       <xs:attribute name="Code" type="xs:int" use="required"> 
        <xs:annotation> 
        <xs:documentation>Branch Code</xs:documentation> 
        </xs:annotation> 
       </xs:attribute> 
       </xs:complexType> 
      </xs:element> 
      </xs:sequence> 
     </xs:complexType> 
     </xs:element> 
    </xs:sequence> 
    <xs:attribute name="Code" type="xs:int" use="required"> 
     <xs:annotation> 
     <xs:documentation>Dictionary code</xs:documentation> 
     </xs:annotation> 
    </xs:attribute> 
    <xs:attribute name="GroupCode" type="xs:int" use="optional"> 
     <xs:annotation> 
     <xs:documentation>Another Code</xs:documentation> 
     </xs:annotation> 
    </xs:attribute> 
    <xs:attribute name="IsDeleted" type="xs:boolean" use="optional" default="false"> 
     <xs:annotation> 
     <xs:documentation> 
     documentation 
     </xs:documentation> 
     </xs:annotation> 
    </xs:attribute> 
    </xs:complexType> 

    <xs:element name="DistrictType" type="DistrictType"> 
    <xs:annotation> 
     <xs:documentation>documentation</xs:documentation> 
    </xs:annotation> 
    </xs:element> 
</xs:schema> 

とXML:

警告:オプションの属性 "GroupCodeが" 不足している

<DistrictType Code="1" IsDeleted="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="./../Schema/flowGeoClassifier.DistrictType.xsd"> <Localizations> <Localization Lang="ru" Name="район" ShortName="р-н" /> </Localizations> <Branches> <Branch Code="1" /> </Branches> <Countries> <Country Code="1" /> <Country Code="483647" /> <Country Code="2147483647" /> </Countries> </DistrictType> 

は、私はそのようないくつかのメッセージを取得する必要があります。

私が検証するのPowerShellスクリプトを使用します。

$XmlFile = Get-Item($xmlFileName) 
    # Perform the XSD Validation 
    $readerSettings = New-Object -TypeName System.Xml.XmlReaderSettings 
    $readerSettings.Schemas.Add($compiledSchema) 
    $readerSettings.ValidationType = [System.Xml.ValidationType]::Schema 
    $readerSettings.ValidationFlags = [System.Xml.Schema.XmlSchemaValidationFlags]::ProcessInlineSchema -bor [System.Xml.Schema.XmlSchemaValidationFlags]::ProcessSchemaLocation 
    $readerSettings.add_ValidationEventHandler(
    { 
     # Triggered each time an error is found in the XML file 
     Write-Host $("ERROR line $($_.exception.LineNumber) position $($_.exception.LinePosition) in '$xmlFileName': " + $_.Message) -ForegroundColor Red 
     $script:errorCount++ 
    }); 
    $reader = [System.Xml.XmlReader]::Create($XmlFile.FullName, $readerSettings) 
    while ($reader.Read()) { } 
    $reader.Close() 

はValidationEventHandlerのスタンダールmethodesによってオプションの属性を検証していますか?

答えて

0

属性が必須であるスキーマのバリアントを作成し、それに対して検証するのはなぜですか?

スキーマは白黒です。有効かどうかは、警告の余地があまりありません。しかし、常に同じスキーマを使用する必要はなく、異なるレベルの厳密さを持つスキーマを使用して異なるステージで検証できます。

0

XmlReaderは、欠落しているオプションの属性が完全に有効であると見なすため、エラー(または警告)は発生しません。

この種の情報を自分で取得する唯一の方法は、手動でチェックすることです。ドキュメントをXmlDocumentに読み込んでからrunning XPathクエリをチェックすることで、これをかなり簡単に行うことができます。

+0

ありがとうございました!しかし、この方法は私の場合には適していません。 –

関連する問題