在xsd中不可能有同名的元素吗?

在xsd中不可能有同名的元素吗?,xsd,schema,xsd-validation,Xsd,Schema,Xsd Validation,我的XML看起来像。我有两个同名的元素。但我无法在XSD中获取这些元素 这里Book是出现两次但具有不同属性的元素。它们各自的Book元素中的所有属性都是必需的 错误是 元素书与元素书不一致 XML: 我的XSD看起来像: <?xml version="1.0" encoding="UTF-8"?> <xsd:schema....> <xsd:element name="TestRoot"> <xsd:complexType>

我的XML看起来像。我有两个同名的元素。但我无法在XSD中获取这些元素

这里Book是出现两次但具有不同属性的元素。它们各自的Book元素中的所有属性都是必需的

错误是

元素书与元素书不一致

XML:


我的XSD看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema....>
<xsd:element name="TestRoot">
    <xsd:complexType>
        <xsd:sequence minOccurs="1" maxOccurs="1">
            <xsd:element name="Test" minOccurs="1" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:choice>
                        <xsd:element name="Value">
                            <xsd:complexType>
                                <xsd:choice minOccurs="1" maxOccurs="1">
                                    <xsd:element name="Book">
                                        <xsd:complexType>
                                            <xsd:attribute name="Name" use="required">
                                                <xsd:simpleType>
                                                    <xsd:restriction base="xsd:string">
                                                        <xsd:maxLength value="40"/>
                                                        <xsd:enumeration value="Wolves"/>
                                                    </xsd:restriction>
                                                </xsd:simpleType>
                                            </xsd:attribute>
                                        </xsd:complexType>
                                    </xsd:element>
                                    <xsd:element name="Book">
                                        <xsd:complexType>
                                            <xsd:attribute name="Book" use="required">
                                                <xsd:simpleType>
                                                    <xsd:restriction base="xsd:string">
                                                        <xsd:maxLength value="40"/>
                                                        <xsd:enumeration value="Dogs"/>
                                                    </xsd:restriction>
                                                </xsd:simpleType>
                                            </xsd:attribute>
                                            <xsd:attribute name="Pages" use="required">
                                                <xsd:simpleType>
                                                    <xsd:restriction base="xsd:string">
                                                        <xsd:maxLength value="50"/>
                                                    </xsd:restriction>
                                                </xsd:simpleType>
                                            </xsd:attribute>
                                            <xsd:attribute name="Photos" use="required">
                                                <xsd:simpleType>
                                                    <xsd:restriction base="xsd:string">
                                                        <xsd:maxLength value="24"/>
                                                    </xsd:restriction>
                                                </xsd:simpleType>
                                            </xsd:attribute>
                                        </xsd:complexType>
                                    </xsd:element>
                                </xsd:choice>
                            </xsd:complexType>
                        </xsd:element>
                    </xsd:choice>
                    <xsd:attribute name="Shelf" use="optional">
                        <xsd:simpleType>
                            <xsd:restriction base="xsd:string">
                                <xsd:maxLength value="100"/>
                                <xsd:enumeration value="1"/>
                                <xsd:enumeration value="2"/>
                            </xsd:restriction>
                        </xsd:simpleType>
                    </xsd:attribute>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>
</xsd:schema>


不知道我错在哪里?有什么帮助吗?

试试这样的方法:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="TestRoot" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="TestRoot" msdata:IsDataSet="true" msdata:Locale="en-US">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Test">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Value" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <!-- the <Book> node is just of "BookType" type -->
                    <xs:element name="Book" type="BookType" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="Shelf" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element> 
  <!-- define the "BookType" which represents a single <Book> node -->
  <xs:complexType name="BookType">
      <xs:attribute name="Name" type="xs:string" use="required" />
      <xs:attribute name="Pages" type="xs:string" />
      <xs:attribute name="Photos" type="xs:string" />
  </xs:complexType>
</xs:schema>

这个XSD定义了一个新的复杂类型
BookType
,用于定义
中的
节点的外观

一般来说,我还将为您拥有的任何XML节点定义一个复杂类型——我将创建一个
ValueType
来处理
节点的组成方式,我还将为
创建一个
测试类型
,并为
创建一个
测试根类型


有了那些真正深入嵌套的XSD结构,就很难“掌握”正在发生的事情-为每个节点定义一个类型,然后将它们分配给节点,这样就更容易阅读和理解,在我看来

您需要定义一个复杂类型
BookType
,然后将两个元素都指定为该类型的元素,single
BookType
Hello Mark谢谢你的回答……但我对XSD不太熟悉。如果你能给出一个例子,那就太好了。好吧……但是如果两个Book元素都需要Book/@Name属性,而Book/@Pages和Book/@Photos只是第二个Book元素的必需属性,那又该怎么办呢?@SMA_JAVA:add
use=“Required”
到该属性-更新了我的response@Marc....I了解我必须在各自的情况下设置use=required/optional。但是根据您所做的更新,这里Book/@Name是必需的,这很好,但是对于第二个Book元素,其他属性也是必需的。如果我也要求它们,那么第一个元素将失败,对吗?@SMA_JAVA:您可以定义两种类型,但是您也需要使用两个不同的XML元素名称。您不能有一个
,它一次是
图书类型1
,另一次是
图书类型2
。。。。。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="TestRoot" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="TestRoot" msdata:IsDataSet="true" msdata:Locale="en-US">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Test">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Value" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <!-- the <Book> node is just of "BookType" type -->
                    <xs:element name="Book" type="BookType" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="Shelf" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element> 
  <!-- define the "BookType" which represents a single <Book> node -->
  <xs:complexType name="BookType">
      <xs:attribute name="Name" type="xs:string" use="required" />
      <xs:attribute name="Pages" type="xs:string" />
      <xs:attribute name="Photos" type="xs:string" />
  </xs:complexType>
</xs:schema>