XSD中的嵌套complexTypes返回错误:不允许元素内容,因为类型定义很简单



我正在为这个xml构建一个xml模式:

<?xml version="1.0" encoding="UTF-8"?>
<groups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="basic.xsd">
<!--Comienza la descripción de los grupos sencillos-->
    <!--modificador-->
    <group tagName="A">
      <name>modificador</name>
      <type>simple</type>
      <words>
        <word>el<word>
        <word>este<word>
      </words>
    </group>
    <!--prefijos-->
    <group tagName="C">
      <name>prefijos</name>
      <type>simple</type>
      <words>
        <word>pre</word>
        <word>ante</word>
        <word>anti</word>
        <word>pro</word>
        <word>tri</word>
      </words>
    </group>
    <!--Composiciones de intérvalos-->
</groups>

这是我的xsd文件:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of simple elements -->
<xs:element name="name" type="xs:string"/>
<xs:element name="type" type="xs:string"/>
<xs:element name="word" type="xs:string"/> 
<!-- definition of attributes -->
<xs:attribute name="tagName" type="xs:ID"/>
<!-- definition of complex elements -->
<xs:element name="words">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="word" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
<xs:element name="group">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="name"/>
      <xs:element ref="type"/>
      <xs:element ref="words" />
    </xs:sequence>
    <xs:attribute ref="tagName" use="required"/>
  </xs:complexType>
</xs:element>
<xs:element name="groups">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="group" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

我正在使用nokogiri来验证我的xml,但我得到了以下错误:

元素"word":不允许元素内容,因为类型定义很简单。XML不是架构有效的

为什么这个词要复杂?我该怎么做,因为对我来说,这很好。提前感谢。

您不小心在中使用了打开标记而不是关闭标记

<word>el<word>
<word>este<word>

在您的输入XML文件中。由于没有结束标记,所以您的XML格式不正确。总是先检查格式是否正确是个好主意。

您会得到消息"元素内容不允许",因为看起来word元素中有word元素。这只适用于复杂类型。

将输入文件中受影响的行更改为

<word>el</word>
<word>este</word>

并且针对您的模式的验证将成功。

相关内容

最新更新