【问题标题】:Validation of optional attributes against xsd-schema in powershell在 powershell 中针对 xsd-schema 验证可选属性
【发布时间】:2017-06-08 19:34:43
【问题描述】:

我需要通过 powershell 脚本针对 xsd 验证 xml,并带有关于可选属性等的警告。 比如我的xsd:

    <?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:

<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>

我需要这样的消息:

警告:缺少可选属性“GroupCode”。

我使用 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 是否通过标准方法验证可选属性?

【问题讨论】:

    标签: xml powershell xsd


    【解决方案1】:

    为什么不创建一个模式的变体,其中属性是强制性的,然后根据它进行验证?

    架构是非黑即白的,事情要么有效,要么无效:没有太多的警告空间。但是您不必一直使用相同的架构,您可以在不同的阶段使用具有不同严格级别的架构进行验证。

    【讨论】:

      【解决方案2】:

      XmlReader 认为缺少的可选属性完全有效,因此不会引发错误(或警告)。

      自己获取此类信息的唯一方法是手动检查它们。您可以通过将文档读入 XmlDocument,然后通过 running XPath 查询进行检查来相当轻松地做到这一点。

      【讨论】:

      • 谢谢!但是这种方法不适合我的情况。
      猜你喜欢
      • 2011-01-24
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 2010-10-23
      • 2017-09-26
      • 1970-01-01
      相关资源
      最近更新 更多