如何写一个包含字符串类型和子元素的xsd元素?

How to write a xsd element including string type and child element?

我正在尝试为 xml 文件下方编写 xsd 文件。

<?xml version="1.0" ?>
<booklist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="p5_09_final.xsd">

  <book kind="novel"> Purpose-Driven Life
    <price>9000</price>
  </book>

  <book kind="computer"> XML and Ontology
    <price>5000</price>
  </book>
</booklist>

我试图为这个 xml 文件编写 xsd 文件。 但是我无法处理包含字符串类型和子元素价格的书籍元素。 如何为这个 xml 文件编写验证 xsd 文件?这是我写的 xsd 文件。无效

<?xml version="1.0" ?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <!-- 

     -->
    <xsd:element name="booklist">
      <xsd:complexType>
         <xsd:sequence>
           <xsd:element name="book" type="ctBook" minOccurs="0" maxOccurs="unbounded" />
         </xsd:sequence>
      </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="ctBook" >
      <xsd:sequence>
         <xsd:element name="price" type="xsd:int" />
      </xsd:sequence>
      <xsd:attribute name="kind" type="ctKind" />
    </xsd:complexType>

    <xsd:simpleType name="ctKind">
      <xsd:restriction base="xsd:string">
        <xsd:enumeration value="computer" />
        <xsd:enumeration value="novel" />
      </xsd:restriction>
    </xsd:simpleType>

</xsd:schema>

如果您希望能够插入文本和标签,则需要为图书使用 混合内容 类型。只需将 mixed 属性添加到 ctBook complexType

<xsd:complexType name="ctBook" mixed="true">

但是请注意,您无法控制文本出现的位置。它可以在 <price> 元素之前和之后(或两者)。