0

I just started learning XML today. I was trying to create a 'sample' XSD and populate it but.. I made up my data and it looks fine, but I can't make this schema work..

CREATE XML SCHEMA COLLECTION GenreTestSchema
AS
<?xml version = "1.0" encoding = "UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <!--create group for GENRELIST-->
  <xsd:group name="GENRELISTGROUP">
    <xsd:element name="GENRELIST">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="GENRE" minOccurs="0" maxOccurs="unbounded" />
          <xsd:element name="REFERENCE" minOccurs="0" maxOccurs="100" />
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>
  </xsd:group>
</xsd:schema>

Now If I remove this element

<xsd:element name="GENRELIST">

and all the code that goes with it so (element and complextype) then the schema works fine. I can't figure out what is wrong here and why it doesn't let me create this element?

Errorlog

Element <element> is not valid at location '/*:schema[1]/*:group[1]/*:element[1]'.

It talks about invalid location but I literally have no idea why?

Data sample

<GENRELIST xmlns="http://myGenres">
    <GENRE GenreNo="1">
        <GENRE>Fiction</GENRE>
        <REFERENCE>Alien</REFERENCE>
    </GENRE>
    <GENRE GenreNo="2">
        <GENRE>Tragedy</GENRE>
        <REFERENCE>Titanic</REFERENCE>
    </GENRE>
</GENRELIST>
SiwyJ
  • 29
  • 6

2 Answers2

0

The problem is that you're missing to take the namespace xmlns="http://myGenres" into account on the element GENRELIST.

So transform your anonymous complex type to a named type and use the namespace on it:

<?xml version = "1.0" encoding = "UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns:gl="http://myGenres" 
            targetNamespace="http://myGenres" 
            elementFormDefault="qualified">

    <xsd:complexType name="genrelist">                   <!-- named type 'genrelist' -->
        <xsd:sequence>
            <xsd:element name="GENRE" minOccurs="0" maxOccurs="unbounded" />
            <xsd:element name="REFERENCE" minOccurs="0" maxOccurs="100" />
        </xsd:sequence>
    </xsd:complexType>    

    <!--create group for GENRELIST-->
    <xsd:element name="GENRELIST" type="gl:genrelist" /> <!-- applying the 'gl' namespace to named type 'genrelist' -->

</xsd:schema>

This way your XML will be validated.
For the purpose of targetNamespace look here and for elementFormDefault look there.

zx485
  • 28,498
  • 28
  • 50
  • 59
0

I don't know why you're trying to create an xs:group (a "model group"). A model group is a reusable chunk of schema declarations that can be used in several places, handy when you have multiple elements with similar structure. It might sometimes make sense to have a group with only one reference to it, but it can't make sense to have a group with no references to it.

Now there's an additional (and related) problem). Typically when you start validating an instance document, the validator looks for a global element declaration whose name matches the root element of the instance. But you don't have a global element declaration in your schema; your declaration of GENRELIST isn't global because it is in a group.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164