Hoang
Hoang

Reputation: 1047

About XSD Element is invalid ( XML Schema)

I have a question about XSD formatting.
For examples , I have a XML document

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:media="http://search.yahoo.com/mrss/">
  <entry>
    <title>Examples</title>
    <id>1</id>
    <media:group>
      <media:thumbnail url="http://www.abc.com/image.jpg" />
      <trailer url="http://www.abc.com" />
      <info url="http://www.abc.com/xsd/info.jpg" />
      <media:description>1</media:description>
      <media:content name="video.mp4" type="video" duration="400" repeat="true"/>
    </media:group>
    <menu>
      <item uri="localhost:8080/xyzt" />
    </menu>
  </entry>
</feed>

And then, I use the website http://www.xmlforasp.net/codebank/system_xml_schema/buildschema/buildxmlschema.aspx for auto convert to XSD file( you can try it). However, when I use the website http://www.xmlforasp.net/schemavalidator.aspx for check XSD validator , at that time have a problem about XSD formatting
Problem :

Invalid 'name' attribute value 'media:group': 'The ':' character, hexadecimal value 0x3A, at position 5 within the name, cannot be included in a name.'.

    <xsd:element name="entry">
      <xsd:complexType>
        <xsd:sequence>
          <xsd:element name="title" type="xsd:string" />
          <xsd:element name="id" type="xsd:int" />
          <xsd:element name="media:group">             <!-- here -->
            <xsd:complexType>
              <xsd:sequence>

I do not understand ... Why the name of xsd:element can not include ":" character ??? If the above problem is Right , how can I fix it.

Please help me if you known that.
Thank you very much !

Upvotes: 0

Views: 2024

Answers (2)

13ren
13ren

Reputation: 12187

The text before the ":" is called a namespace. e.g. in "media:group", the namespace is "media".

To define an element in a namespace, you can't just say it like <xsd:element name="media:group"> as you'd expect.

Instead, you have to specify it as the targetNamespace for the whole schema, and then all elements defined in that schema will have that namespace. Like this (the qualified part means that your XML needs to use the namespace, as your XML does with "media"):

<xsd:schema targetNamespace="http://search.yahoo.com/mrss/"
            elementFormDefault="qualified" ...>
  ...
  <xsd:element name="group"> 

You can also have a schema whose elements have no namespace, by omitting targetNamespace:

<xsd:schema ...>
  ...
  <xsd:element name="entry"> 

Note: to define elements in two namespaces, you need two schemas. Normally, you can combine schema by importing one into another. But there's a problem with your XML: the references to namespaces are cyclic in that <entry> contains <media:group>, which contains <trailer>.

Sorry: I cannot solve this. My best attempt used a third schema (defining <trailer>), but this crashes xmllint... Can anyone else solve it?

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163645

Try a different validator. I don't know anything about this site, but from your evidence, they are parsing the XML using a non-namespace aware XML parser. That's a pretty bizarre thing to do.

Upvotes: 1

Related Questions