xml架构具有属性的元素内的嵌套元素

时间:2022-12-14 17:17:19

I am making a school assignment This is my XML

我正在做学校作业这是我的XML

<lineup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="ComplexTypeDemo.xsd">
  <team teamName="Maple Leafs" city="Toronto">
    <visitor/>
      <player number="17">
        <name>John Doe</name>
        <position>Forward</position>
      </player>
      <!--Continue 20 Iterations-->
    </team>
    <team teamName="Rangers" city="New York">
     <home/>
      <player number="17">
        <name>John Doe</name>
        <position>Forward</position>
      </player>
      <!--Continue 20 Iterations-->
    </team>
</lineup>

here is my schema document

这是我的架构文档

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="lineup">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="team" minOccurs="2" maxOccurs="2">

          <xs:complexType>
            <xs:sequence>


            <xs:choice>
              <xs:element name="home"/>
              <xs:element name="visitor"/>
            </xs:choice>

              <xs:element name="player" minOccurs="20" maxOccurs="20">
                <xs:complexType>
                  <xs:all>
                    <xs:element name="name" />
                    <xs:element name="position"/>
                  </xs:all>
                </xs:complexType>

              </xs:element>

            </xs:sequence>
          </xs:complexType>


        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

i need to make a schema to validate this. but i can't figure out how to validate because it is nested but it has attributes. I can only seem to do one or the other, but not both....

我需要制作一个模式来验证这一点。但我无法弄清楚如何验证,因为它是嵌套但它有属性。我似乎只能做一个或另一个,但不是两个....

2 个解决方案

#1


3  

You are close. You can place attribute declarations after the xs:sequence or xs:all close tags but before the xs:complexType close tag. This update to your XSD will validate the XML document instance you provide (modulo actually including 20 iterations as noted):

你很亲密您可以在xs:sequence或xs:all close标记之后但在xs:complexType close标记之前放置属性声明。对XSD的此更新将验证您提供的XML文档实例(模数实际上包括20个迭代,如上所述):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="lineup">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="team" minOccurs="2" maxOccurs="2">
          <xs:complexType>
            <xs:sequence>
              <xs:choice>
                <xs:element name="home"/>
                <xs:element name="visitor"/>
              </xs:choice>
              <xs:element name="player" minOccurs="20" maxOccurs="20">
                <xs:complexType>
                  <xs:all>
                    <xs:element name="name" />
                    <xs:element name="position"/>
                  </xs:all>
                  <xs:attribute name="number" type="xs:integer"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="teamName"/>
            <xs:attribute name="city"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

#2


2  

If you have a complex type, you should place the attribute references (or declarations) after your groups (sequences, choices, etc.). For example:

如果您有复杂类型,则应将属性引用(或声明)放在组之后(序列,选项等)。例如:

<xs:element name="team">
    <xs:complexType>
        <xs:sequence>
            <xs:choice>
                <xs:element ref="home"/>
                <xs:element ref="visitor"/>
            </xs:choice>
            <xs:element ref="player" minOccurs="20" maxOccurs="20"/>
        </xs:sequence>
        <xs:attribute name="city" type="xs:string"/>
        <xs:attribute name="teamName" type="xs:string"/>
    </xs:complexType>
</xs:element>

If you have a simple type (for example, an empty element which requires an attribute, or one that has string contents), then you should declare your type as a complex type with simple content, and provide an extension of the simple content for your attributes. For example:

如果您有一个简单类型(例如,一个需要属性的空元素,或一个包含字符串内容的元素),那么您应该将类​​型声明为具有简单内容的复杂类型,并为您提供简单内容的扩展属性。例如:

<xs:element name="visitor">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="visitorName" type="xs:string"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

#1


3  

You are close. You can place attribute declarations after the xs:sequence or xs:all close tags but before the xs:complexType close tag. This update to your XSD will validate the XML document instance you provide (modulo actually including 20 iterations as noted):

你很亲密您可以在xs:sequence或xs:all close标记之后但在xs:complexType close标记之前放置属性声明。对XSD的此更新将验证您提供的XML文档实例(模数实际上包括20个迭代,如上所述):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="lineup">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="team" minOccurs="2" maxOccurs="2">
          <xs:complexType>
            <xs:sequence>
              <xs:choice>
                <xs:element name="home"/>
                <xs:element name="visitor"/>
              </xs:choice>
              <xs:element name="player" minOccurs="20" maxOccurs="20">
                <xs:complexType>
                  <xs:all>
                    <xs:element name="name" />
                    <xs:element name="position"/>
                  </xs:all>
                  <xs:attribute name="number" type="xs:integer"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
            <xs:attribute name="teamName"/>
            <xs:attribute name="city"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

#2


2  

If you have a complex type, you should place the attribute references (or declarations) after your groups (sequences, choices, etc.). For example:

如果您有复杂类型,则应将属性引用(或声明)放在组之后(序列,选项等)。例如:

<xs:element name="team">
    <xs:complexType>
        <xs:sequence>
            <xs:choice>
                <xs:element ref="home"/>
                <xs:element ref="visitor"/>
            </xs:choice>
            <xs:element ref="player" minOccurs="20" maxOccurs="20"/>
        </xs:sequence>
        <xs:attribute name="city" type="xs:string"/>
        <xs:attribute name="teamName" type="xs:string"/>
    </xs:complexType>
</xs:element>

If you have a simple type (for example, an empty element which requires an attribute, or one that has string contents), then you should declare your type as a complex type with simple content, and provide an extension of the simple content for your attributes. For example:

如果您有一个简单类型(例如,一个需要属性的空元素,或一个包含字符串内容的元素),那么您应该将类​​型声明为具有简单内容的复杂类型,并为您提供简单内容的扩展属性。例如:

<xs:element name="visitor">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="visitorName" type="xs:string"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>