1

I just began to learn XML and I can't find the solution to my error. I get this error: "The markup in the document following the root element must be well-formed.

Any help would be great. Thanks

    <?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="inventory">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="cd" type="cdType"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="cdType">
            <xsd:sequence>    
                <xsd:element name="title" type="xsd:string"/>
                <xsd:element name="artist" type="xsd:string"/>
                <xsd:element name="contact" type="xsd:string" minOccurs="0" maxOccurs="1"/>
            </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

<inventory>
  <cd>
    <title>American Beauty</title>
    <artist>Grateful Dead</artist>
    <contact>KC Daily</contact>
  </cd>

  <cd>
     <title>Dare to be Stupid</title>
     <artist>Al Yankovic</artist>
  </cd>

  <cd>
     <title>Skeletons from the Closet</title>
     <artist>Grateful Dead></artist>
     <contact>KC Daily</contact>
  </cd>
</inventory>
wcw25709275
  • 21
  • 1
  • 2

1 Answers1

0

Store the schema separately. It thinks there are two root elements.

The rules of well-formed XML are:

  1. There is only 1 root element.
  2. All elements are closed properly
  3. Elements are nested correctly

An example for rule 3:

<!-- WRONG Way -->
<b><i></b></i>

<!-- Right Way -->
<b><i></i></b>

For rule 1, the following is illegal:

<root1>
  <element />
</root1>
<!-- Notice that a new element has been opened at the top level -->
<root2>
</root2>

For rule 2, there are two ways to close elements:

If there is no content (like html's img element), you can close it like this:

<element />

Otherwise, you must use a separate close tag of the following format:

<element>
<!-- Precede the opening tag's name with a forward slash (/) -->
</element>

Note: Store the schema (the xsd:schema part) in a separate file with an extension of .xsd. Then reference it in the root element of your document.