C# - XSD验证

时间:2017-01-26 17:43:28

标签: xml xsd xmlschemaset

我正在尝试构建XSD验证,我有03 XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.esocial.gov.br/schema/evt/evtAdmissao/v02_02_00" targetNamespace="http://www.esocial.gov.br/schema/evt/evtAdmissao/v02_02_00" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="xmldsig-core-schema.xsd"/>
  <xs:element name="eSocial">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="evtAdmissao">
          <xs:annotation>
            <xs:documentation>Evento Admissão</xs:documentation>
          </xs:annotation>
        </xs:element>
        <xs:element ref="ds:Signature"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>  

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.esocial.gov.br/schema/evt/evtCS/v02_02_00" targetNamespace="http://www.esocial.gov.br/schema/evt/evtCS/v02_02_00" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="xmldsig-core-schema.xsd"/>
  <xs:element name="eSocial">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="evtCS">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="infoCS">
                <xs:annotation>
                  <xs:documentation>Informações relativas às Contribuições Sociais devidas à Previdência Social e a Outras Entidades e Fundos.</xs:documentation>
                </xs:annotation>
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="natInfo">
                      <xs:simpleType>
                        <xs:annotation>
                          <xs:documentation>Natureza da informação</xs:documentation>
                        </xs:annotation>
                        <xs:restriction base="x:byte">
                          <xs:pattern value="\d"/>
                        </xs:restriction>
                      </xs:simpleType>
                    </xs:element>
                  </xs:sequence>                    
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="Id" type="xs:ID" use="required"/>
          </xs:complexType>
        </xs:element>                   
        <xs:element ref="ds:Signature"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>     
</xs:schema>  

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.esocial.gov.br/schema/evt/evtInsApo/v02_02_00" targetNamespace="http://www.esocial.gov.br/schema/evt/evtInsApo/v02_02_00" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="xmldsig-core-schema.xsd"/>
  <xs:element name="eSocial">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="evtInsApo">
          <xs:annotation>
            <xs:documentation>Insalubridade, periculosidade e aposentadoria especial</xs:documentation>
          </xs:annotation>
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ideEvento" type="TIdeEveTrab">
                <xs:annotation>
                  <xs:documentation>Informações de Identificação do Evento</xs:documentation>
                </xs:annotation>
              </xs:element>
              <xs:element name="ideEmpregador" type="TEmpregador">
                <xs:annotation>
                  <xs:documentation>Informações de identificação do empregador</xs:documentation>
                </xs:annotation>
              </xs:element>
              <xs:element name="ideVinculo" type="TIdeVincEstagCoop">
                <xs:annotation>
                  <xs:documentation>Informações de Identificação do Trabalhador e do Vínculo. Aplicar uma das seguintes regras, de acordo com a categoria do trabalhador.</xs:documentation>
                </xs:annotation>
              </xs:element>
              <xs:element name="insalPeric" minOccurs="0">
                <xs:annotation>
                  <xs:documentation>Informações sobre o ambiente de trabalho insalubre/periculoso.</xs:documentation>
                </xs:annotation>
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="iniInsalPeric" minOccurs="0">
                      <xs:annotation>
                        <xs:documentation>Condições do ambiente de trabalho insalubre/periculoso - Início</xs:documentation>
                      </xs:annotation>
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="dtIniCondicao">
                            <xs:simpleType>
                              <xs:annotation>
                                <xs:documentation>Data de Início da Condição</xs:documentation>
                              </xs:annotation>
                              <xs:restriction base="xs:date">
                              </xs:restriction>
                            </xs:simpleType>
                          </xs:element>
                          <xs:element name="infoAmb" type="TinfoAmb4" maxOccurs="99">
                            <xs:annotation>
                              <xs:documentation>Detalhamento dos ambientes de trabalho em que o trabalhador desempenha atividades insalubres/periculosa.</xs:documentation>
                            </xs:annotation>
                          </xs:element>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="altInsalPeric" minOccurs="0">
                      <xs:annotation>
                        <xs:documentation>Condições do ambiente de trabalho insalubre/periculoso - Alteração</xs:documentation>
                      </xs:annotation>
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="dtAltCondicao">
                            <xs:simpleType>
                              <xs:annotation>
                                <xs:documentation>Data de Alteração da Condição</xs:documentation>
                              </xs:annotation>
                              <xs:restriction base="xs:date">
                              </xs:restriction>
                            </xs:simpleType>
                          </xs:element>
                          <xs:element name="infoamb" type="TinfoAmb4" maxOccurs="99">
                            <xs:annotation>
                              <xs:documentation>Informações sobre as condições de trabalho insalubre/periculoso.</xs:documentation>
                            </xs:annotation>
                          </xs:element>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="fimInsalPeric" minOccurs="0">
                      <xs:annotation>
                        <xs:documentation>Condições do ambiente de trabalho insalubre/periculoso - Fim</xs:documentation>
                      </xs:annotation>
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="dtFimCondicao">
                            <xs:simpleType>
                              <xs:annotation>
                                <xs:documentation>Data Término da Condição Diferenciada</xs:documentation>
                              </xs:annotation>
                              <xs:restriction base="xs:date">
                              </xs:restriction>
                            </xs:simpleType>
                          </xs:element>
                          <xs:element name="infoAmb" maxOccurs="99">
                            <xs:annotation>
                              <xs:documentation>Detalha o ambiente de trabalho em que o trabalhador desempenha atividade insalubre/periculosa</xs:documentation>
                            </xs:annotation>
                            <xs:complexType>
                              <xs:sequence>
                                <xs:element name="codAmb">
                                  <xs:simpleType>
                                    <xs:annotation>
                                      <xs:documentation>Código do Ambiente de Trabalho</xs:documentation>
                                    </xs:annotation>
                                    <xs:restriction base="xs:string">
                                      <xs:minLength value="1"/>
                                      <xs:maxLength value="30"/>
                                    </xs:restriction>
                                  </xs:simpleType>
                                </xs:element>
                              </xs:sequence>
                            </xs:complexType>
                          </xs:element>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="aposentEsp" minOccurs="0">
                <xs:annotation>
                  <xs:documentation>Informações sobre o trabalho exercido em condições que ensejam a aposentadoria especial.</xs:documentation>
                </xs:annotation>
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="iniAposentEsp" minOccurs="0">
                      <xs:annotation>
                        <xs:documentation>Condições do ambiente de trabalho que ensejam aposentadoria especial - Início</xs:documentation>
                      </xs:annotation>
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="dtIniCondicao">
                            <xs:simpleType>
                              <xs:annotation>
                                <xs:documentation>Data de Início da Condição</xs:documentation>
                              </xs:annotation>
                              <xs:restriction base="xs:date">
                              </xs:restriction>
                            </xs:simpleType>
                          </xs:element>
                          <xs:element name="infoAmb" type="TinfoAmb4" maxOccurs="99">
                            <xs:annotation>
                              <xs:documentation>Informações relativas ao ambiente de trabalho</xs:documentation>
                            </xs:annotation>
                          </xs:element>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="altAposentEsp" minOccurs="0">
                      <xs:annotation>
                        <xs:documentation>Condições do ambiente de trabalho que ensejam aposentadoria especial - Alteração</xs:documentation>
                      </xs:annotation>
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="dtAltCondicao">
                            <xs:simpleType>
                              <xs:annotation>
                                <xs:documentation>Data de Alteração da Condição</xs:documentation>
                              </xs:annotation>
                              <xs:restriction base="xs:date">
                              </xs:restriction>
                            </xs:simpleType>
                          </xs:element>
                          <xs:element name="infoamb" type="TinfoAmb4" maxOccurs="99">
                            <xs:annotation>
                              <xs:documentation>Informações relativas ao ambiente de trabalho</xs:documentation>
                            </xs:annotation>
                          </xs:element>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                    <xs:element name="fimAposentEsp" minOccurs="0">
                      <xs:annotation>
                        <xs:documentation>Condições do ambiente de trabalho que ensejam aposentadoria especial - Término</xs:documentation>
                      </xs:annotation>
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="dtFimCondicao">
                            <xs:simpleType>
                              <xs:annotation>
                                <xs:documentation>Data Término da Condição Diferenciada</xs:documentation>
                              </xs:annotation>
                              <xs:restriction base="xs:date">
                              </xs:restriction>
                            </xs:simpleType>
                          </xs:element>
                          <xs:element name="infoAmb" maxOccurs="99">
                            <xs:annotation>
                              <xs:documentation>Informações relativas ao ambiente de trabalho</xs:documentation>
                            </xs:annotation>
                            <xs:complexType>
                              <xs:sequence>
                                <xs:element name="codAmb">
                                  <xs:simpleType>
                                    <xs:annotation>
                                      <xs:documentation>Código do Ambiente de Trabalho</xs:documentation>
                                    </xs:annotation>
                                    <xs:restriction base="xs:string">
                                      <xs:minLength value="1"/>
                                      <xs:maxLength value="30"/>
                                    </xs:restriction>
                                  </xs:simpleType>
                                </xs:element>
                              </xs:sequence>
                            </xs:complexType>
                          </xs:element>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="Id" type="xs:ID" use="required"/>
          </xs:complexType>
        </xs:element>
        <xs:element ref="ds:Signature"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="TIdeEveTrab">
    <xs:annotation>
      <xs:documentation>Identificação do evento</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="indRetif">
        <xs:simpleType>
          <xs:annotation>
            <xs:documentation>Indicativo de Retificação</xs:documentation>
          </xs:annotation>
          <xs:restriction base="xs:byte">
            <xs:pattern value="\d"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="nrRecibo" minOccurs="0">
        <xs:simpleType>
          <xs:annotation>
            <xs:documentation>Recibo arquivo a ser retificado</xs:documentation>
          </xs:annotation>
          <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="40"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="tpAmb">
        <xs:simpleType>
          <xs:annotation>
            <xs:documentation>Tipo de ambiente</xs:documentation>
          </xs:annotation>
          <xs:restriction base="xs:byte">
            <xs:pattern value="\d"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="procEmi">
        <xs:simpleType>
          <xs:annotation>
            <xs:documentation>Processo de emissão do evento</xs:documentation>
          </xs:annotation>
          <xs:restriction base="xs:byte">
            <xs:pattern value="\d"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="verProc">
        <xs:simpleType>
          <xs:annotation>
            <xs:documentation>Versão do processo de emissão do evento. </xs:documentation>
          </xs:annotation>
          <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="20"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="TEmpregador">
    <xs:sequence>
      <xs:element name="tpInsc">
        <xs:simpleType>
          <xs:annotation>
            <xs:documentation>Tipo de Inscrição, conforme tabela 5.</xs:documentation>
          </xs:annotation>
          <xs:restriction base="xs:byte">
            <xs:pattern value="\d"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="nrInsc">
        <xs:simpleType>
          <xs:annotation>
            <xs:documentation>Número de Inscrição</xs:documentation>
          </xs:annotation>
          <xs:restriction base="xs:string">
            <xs:pattern value="\d{8,15}"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="TIdeVincEstagCoop">
    <xs:annotation>
      <xs:documentation>Informações do vínculo - empregados, estagiários e cooperados</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="cpfTrab">
        <xs:simpleType>
          <xs:annotation>
            <xs:documentation>CPF do trabalhador</xs:documentation>
          </xs:annotation>
          <xs:restriction base="xs:string">
            <xs:length value="11"/>
            <xs:pattern value="\d{11}"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="nisTrab" minOccurs="0">
        <xs:simpleType>
          <xs:annotation>
            <xs:documentation>NIS</xs:documentation>
          </xs:annotation>
          <xs:restriction base="xs:string">
            <xs:length value="11"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="matricula" minOccurs="0">
        <xs:simpleType>
          <xs:annotation>
            <xs:documentation>Matricula</xs:documentation>
          </xs:annotation>
          <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="30"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="TinfoAmb" maxOccurs="99">
    <xs:annotation>
      <xs:documentation>Detalha o(s) Ambiente(s) de Trabalho em que o trabalhador desempenha atividades insalubres/periculosa.</xs:documentation>
    </xs:annotation>
    <xs:sequence>
      <xs:element name="codAmb">
        <xs:simpleType>
          <xs:annotation>
            <xs:documentation>Código do Ambiente de Trabalho</xs:documentation>
          </xs:annotation>
          <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
            <xs:maxLength value="30"/>
          </xs:restriction>
        </xs:simpleType>
      </xs:element>
      <xs:element name="fatRisco" maxOccurs="999">
        <xs:annotation>
          <xs:documentation>Fator de risco ao qual o trabalhador está exposto na atividade exercida no ambiente</xs:documentation>
        </xs:annotation>
        <xs:complexType>
          <xs:sequence>
            <xs:element name="codFatRis">
              <xs:simpleType>
                <xs:annotation>
                  <xs:documentation>Código do fator de risco</xs:documentation>
                </xs:annotation>
                <xs:restriction base="xs:string">
                  <xs:minLength value="4"/>
                  <xs:maxLength value="10"/>
                </xs:restriction>
              </xs:simpleType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

我尝试将其加载到XMLSchemaSet

    using (XmlReader rdSchema = XmlReader.Create(GetStreamResource(resource)))
    {

        schemaSet.Add(String.Format("http://www.esocial.gov.br/schema/evt/{0}/v02.02.00", evtESocial, versao), rdSchema);
    }

但是我为每个XML都有一堆错误 我得到的第一个

  

http://www.w3.org/2001/XMLSchema:element”元素不是   在这种情况下得到支持。

第二个我有

  

'base'属性的值无效 - 'x:byte'无效   'base'属性的值。

在第三部分我有

  

此上下文不支持'maxOccurs'属性。

我花了好几个小时试图解决这个问题,我在网上搜索过,但我还没有发现它是如何解决的。有人能帮助我吗?

由于

1 个答案:

答案 0 :(得分:0)

第一个XSD很好。

对于第二个XSD,请更改

<xs:restriction base="x:byte">

<xs:restriction base="xs:byte">

对于第三行XSD,在第42行,您错过了结束xs:sequence代码,而不是关闭xs:element的{​​{1}},您已经打开了一个新的eSocial。要解决第三个XSD的最后一个问题是,您已引用xs:element而未包含type="TinfoAmb4"类型的定义。定义TinfoAmb4或将TinfoAmb4的类型更改为声明的内容。