1
votes

I am trying to write xsd file for below the xml file.

<?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>

I tried to write xsd file for this xml file. But I can't handle the book element including string type and child element price. How can I write validate xsd file for this xml file?? This is the xsd file that i wrote. It's invalid

<?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>
1

1 Answers

1
votes

You need to use a mixed-content type for the book if you want to be able to insert text and tags. Just add the mixed attribute to the ctBook complexType

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

However, be aware that you can not control the place where your text will appear. It can before and after (or both) the <price> element.