【问题标题】:XML with Schema not validating and giving me "content of element declaration must match"带有 Schema 的 XML 未验证并给我“元素声明的内容必须匹配”
【发布时间】:2018-08-08 03:14:39
【问题描述】:

我一直试图弄清楚为什么这个 .xsd 和 .xml 没有验证。我在第 17 行第 57 列收到一个错误,上面写着

"元素声明的内容必须匹配(注解?,(simpleType | complexType)?, (unique| key keyref)*)

奇怪的是,如果我删除 .xsd 和 .xml 中的第 17 行,由于某种原因,错误仍然会在同一个位置弹出。

这是我的 XML 代码:

<?xml version="1.0" encoding="UTF-8"?>
<cursos xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xs:noNamespaceSchemaLocation="reto4MarcasGrupo1Schema.xsd">
        <curso codigoCurso="AA2000">
          <nombreCurso>Manejo Ofimatica</nombreCurso>
          <numPlazas>20</numPlazas>
          <numHoras horario="diurno">300</numHoras>
          <fechaInicio>18-2-2018</fechaInicio>
          <fechaFin> 30-3-2018</fechaFin>
          <objetivos>Los objetivos de este curso es que las personas tengan un manejo basico de apliaciones de ofimatica</objetivos>
          <entidadColaboradora nombreEntidad="H"/>
          <diaSemana>L</diaSemana>
          <cuotas tipoCuotas="subvencionado100"/>
          <equipamiento>Ordeandores basicos</equipamiento>
          <dirigidoA tipoDirigido="desempleadosYempleados"/>
          <estado tipoEstado="aRealizar"/>
          <programa>
              <tema codigoTema="AAT001">
                <tituloTema>Introduccion</tituloTema>
                <impartidor>Ron Weasly</impartidor>
              </tema>
              <tema codigoTema="AAT002">
                <tituloTema>Introduccion</tituloTema>
                <impartidor>Jon Nieve</impartidor>
              </tema>
          </programa>
        </curso>
</cursos>

这是不同年级有机课的练习。

这是我的架构:

    <?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="cursos" type="tipoCursos"/>

  <xs:complexType name="tipoCursos">
    <xs:sequence>
      <xs:element name="curso" type="tipoCurso" minOccurs="1" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="tipoCurso">
    <xs:sequence>
      <xs:element name="nombreCurso" type="xs:string"/>
      <xs:element name="numPlazas" type="xs:nonNegativeInteger"/>
      <xs:element name="numHoras" type="tipoNumHoras"/>
      <xs:element name="fechaInicio" type="xs:date"/>
      <xs:element name="objetivos" type="xs:string"/>
      <xs:element name="entidadColaboradora" type="tipoEntidadColaboradora"/>
      <xs:element name="diaSemana" type="xs:string"/>
      <xs:element name="cuotas" type="cuotaTipo"/>
      <xs:element name="equipamiento" type="xs:string"/>
      <xs:element name="dirigidoA" type="tipoDirigido"/>
      <xs:element name="estado" type="estadoTipo"/>
      <xs:element name="programa" type="tipoPrograma"/>
    </xs:sequence>
    <xs:attribute name="codigoCurso" type="xs:ID"/>
  </xs:complexType>

  <xs:complexType name="tipoPrograma">
    <xs:sequence>
      <xs:element name="tema" type="tipoTema" minOccurs="1" maxOccurs="unbounded" />
  </xs:sequence>
  </xs:complexType>

  <xs:complexType name="tipoTema">
    <xs:sequence>
      <xs:element name="tituloTema" type="xs:string"/>
      <xs:element name="impartidor" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="codigoTema" type="xs:ID"/>
  </xs:complexType>

  <xs:complexType name="tipoNumHoras">
    <xs:sequence>
      <xs:element name="numHoras" type="xs:nonNegativeInteger"/>
    </xs:sequence>
    <xs:attribute name="horario" type="tipoHorario"/>
  </xs:complexType>

  <xs:simpleType name="cuotaTipo">
      <xs:restriction base="xs:string">
          <xs:enumeration value="subvencionado100"/>
          <xs:enumeration value="subvencionParcial"/>
          <xs:enumeration value="sinSubvencion"/>
      </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="estadoTipo">
      <xs:restriction base="xs:string">
          <xs:enumeration value="aplazado"/>
          <xs:enumeration value="realizado"/>
          <xs:enumeration value="aRealizar"/>
      </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="tipoDirigido">
      <xs:restriction base="xs:string">
          <xs:enumeration value="desempleadosYempleados"/>
          <xs:enumeration value="preferiblementeEmpleados"/>
          <xs:enumeration value="segunCursos"/>
      </xs:restriction>
  </xs:simpleType>


  <xs:simpleType name="tipoEntidadColaboradora">
      <xs:restriction base="xs:string">
          <xs:enumeration value="H"/>
          <xs:enumeration value="HC"/>
          <xs:enumeration value="S"/>
          <xs:enumeration value="I"/>
          <xs:enumeration value="D"/>
          <xs:enumeration value="E"/>
          <xs:enumeration value="J"/>
          <xs:enumeration value="M"/>
      </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="tipoHorario">
      <xs:restriction base="xs:string">
          <xs:enumeration value="diurno"/>
          <xs:enumeration value="nocturno"/>
      </xs:restriction>
  </xs:simpleType>

</xs:schema>

【问题讨论】:

  • 请在XSD中标出报错的行。否则你强迫我们计算你的代码 sn-ps 中的行数。
  • 您的架构无效。请修复问题中的架构。 tipoHorario 类型未在 XSD 中定义。
  • 您的 XML 无效,因为您在 curso 元素上有一个 codigoCurso 属性。您的架构在 cursos 元素上定义了这一点。
  • 马特是对的。如果您的数据不敏感,您可以使用liquid-technologies.com/online-xsd-validator 等在线工具
  • 您还定义了 tipoNumHoras,因此您的 XML 示例 numHoras 元素无效。

标签: xml validation schema


【解决方案1】:

错误消息表明架构无效。但我不认为它指的是您向我们展示的模式,因为那个很好。所以我认为你需要告诉我们更多关于你正在做什么的信息:例如你正在使用哪个模式处理器,以及你是如何调用它的。 (也许是xsi:schemaLocation 属性中引用的架构才是罪魁祸首?)

您的实例文档对架构无效。以下是撒克逊人报告的错误:

Validation error on line 6 column 50 of test.xml:
  FORG0001: The content model for element <numHoras> does not allow character content
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.3
Validation error on line 6 column 36 of test.xml:
  FORG0001: In content of element <numHoras>: Empty content is not allowed. Expected <numHoras>. 
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
Validation error on line 7 column 22 of test.xml:
  FORG0001: The content "18-2-2018" of element <fechaInicio> does not match the required
  simple type. Invalid date "18-2-2018" (Year is less than four digits)
  See http://www.w3.org/TR/xmlschema-2/#cvc-datatype-valid clause 1
Validation error on line 8 column 19 of test.xml:
  FORG0001: In content of element <curso>: The content model does not allow element
  <fechaFin> to appear immediately after element <fechaInicio>. Expected <objetivos>. 
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 2.4
Validation error on line 10 column 49 of test.xml:
  FORG0001: Attribute @nombreEntidad is not allowed on element <entidadColaboradora>
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 3
Validation error on line 10 column 49 of test.xml:
  FORG0001: The content "" of element <entidadColaboradora> does not match the required
  simple type. Value "" contravenes the enumeration facet "D, E, HC, M, H, I, J, S" of the
  type tipoEntidadColaboradora
  See http://www.w3.org/TR/xmlschema-2/#cvc-datatype-valid clause 1
Validation error on line 12 column 48 of test.xml:
  FORG0001: Attribute @tipoCuotas is not allowed on element <cuotas>
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 3
Validation error on line 12 column 48 of test.xml:
  FORG0001: The content "" of element <cuotas> does not match the required simple type.
  Value "" contravenes the enumeration facet "subvencionado100, subvencionPa..." of the type cuotaTipo
  See http://www.w3.org/TR/xmlschema-2/#cvc-datatype-valid clause 1
Validation error on line 14 column 59 of test.xml:
  FORG0001: Attribute @tipoDirigido is not allowed on element <dirigidoA>
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 3
Validation error on line 14 column 59 of test.xml:
  FORG0001: The content "" of element <dirigidoA> does not match the required simple type.
  Value "" contravenes the enumeration facet "preferiblementeEmpleados, segu..." of the type
  tipoDirigido
  See http://www.w3.org/TR/xmlschema-2/#cvc-datatype-valid clause 1
Validation error on line 15 column 41 of test.xml:
  FORG0001: Attribute @tipoEstado is not allowed on element <estado>
  See http://www.w3.org/TR/xmlschema-1/#cvc-complex-type clause 3
Validation error on line 15 column 41 of test.xml:
  FORG0001: The content "" of element <estado> does not match the required simple type.
  Value "" contravenes the enumeration facet "realizado, aRealizar, aplazado" of the type estadoTipo
  See http://www.w3.org/TR/xmlschema-2/#cvc-datatype-valid clause 1
Validation error on line 27 column 10 of test.xml:
  FORG0001: Twelve validation errors were reported
Validation of file test.xml completed: errors found

但这与您发布的错误无关。

【讨论】:

  • 事实证明,这个错误和我得到的错误几乎相同。我已经声明了带有属性的 simpleTypes,这在验证期间会导致错误,因为您不能这样做。我所做的是更改每个空元素的 XML 并从中删除属性,并将我在 XSD 中指定的值放在所述元素内。我将更新主题以展示我的解决方案。谢谢!!
猜你喜欢
  • 1970-01-01
  • 2015-06-26
  • 2017-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多