RedlightGreenLight
RedlightGreenLight

Reputation: 21

What's wrong with my XSD XML Schema?

I'd been 3 years since I messed with schemas and I remember them being very particular about name space prefixes and all. I also remember complex types not parsing so easily without unrolling when using the XML editor in Eclipse (I have Indigo).

So could one is you XML gurus look at my XML file and tell me what is wrong with the prepended XSD XML schema?

Thanks.

Ciao

+++++

Eclipse complains of:

The markup in the document following the root element must be well-formed.

I should point out I created the XML by hand and autogenerated the XML from it at:

http://www.xmlforasp.net/codebank/system_xml_schema/buildschema/buildxmlschema.aspx

(Seperate Complex Types: as I did manually 3 years ago with Amazon's Amazon Seller Central XML files manually)

If seems to me as ref should be used in one case but I'll defer to the experts.

+++++

<?xml version="1.0" encoding="utf-16"?>

<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="idioms" type="idiomsType" />
    <xsd:complexType name="idiomsType">
        <xsd:sequence>
            <xsd:element maxOccurs="unbounded" name="idiom" type="idiomType" />
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="idiomType">
        <xsd:sequence>
            <xsd:element name="phrase" type="xsd:string" />
            <xsd:element name="meaning" type="xsd:string" />
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

<idioms>
    <idiom>
        <phrase>A bit much</phrase>
        <meaning>Excessive or annoying.</meaning>
    </idiom>
    <idiom>
        <phrase>A bridge too far</phrase>
        <meaning>Act of overreaching.</meaning>
    </idiom>
</idioms>

OK, update. My shortcut of including XSD inline with XML is illegal. So I put it back in XSD file. I also learned that I can't start a prefix with xml so I changed it to 'tc'.

The XSD now is correct (in the same style as I unrolled Amazon Seller Central XSDs).

<?xml version="1.0" encoding="utf-16"?>

<xsd:schema xmlns:tcidioms="urn:tcidioms" targetNamespace="urn:tcidioms" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <xsd:element name="idioms" type="tcidioms:idiomsType"/>
     <xsd:complexType name="idiomsType">
         <xsd:sequence>
             <xsd:element maxOccurs="unbounded" name="idiom" type="tcidioms:idiomType" />
         </xsd:sequence>
     </xsd:complexType>
     <xsd:complexType name="idiomType">
         <xsd:sequence>
             <xsd:element name="phrase" type="xsd:string" />
             <xsd:element name="meaning" type="xsd:string" />
         </xsd:sequence>
     </xsd:complexType>
</xsd:schema>

But I have trouble trying to include this im my XML file which now looks like this (the XML & XSD are in the same directory):

It complains: No grammar constraints (DTD or XML schema) detected for the document.

Meaning the 2 line in my XML document isn't correct. I've tried several variants to include this file but none have worked. Keep in mine this XML (and XSD) is going to be read off a local file systems rather than via http. Will it still validate in this manner?

<?xml version="1.0" encoding="utf-16"?>

<idioms xmlns:tcidioms="urn:tcidioms" targetNamespace="urn:tcidioms ./EnglishIdioms.xsd">
    <idiom>
        <phrase>A bit much</phrase>
       <meaning>Excessive or annoying.
       </meaning>
    </idiom>
    <idiom>
         <phrase>A bridge too far</phrase>
   <meaning>Act of overreaching, going too far
        and getting into trouble
        or
        failing.
   </meaning>
    </idiom>
</idioms>

Visual Studio 2010 (it created the schema Russian Doll Style):

The XML file although set to use the schema in the VS2010 interface doesn't show that in the code?

<?xml version="1.0" encoding="utf-8"?>

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="idioms">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="idiom">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="phrase" type="xs:string" />
                            <xs:element name="meaning" type="xs:string" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

<?xml version="1.0" encoding="utf-8"?>

<idioms>
    <idiom>
        <phrase>A bit much</phrase>
        <meaning>Excessive or annoying.</meaning>
    </idiom>
    <idiom>
        <phrase>A bridge too far</phrase>
        <meaning>Act of overreaching.</meaning>
    </idiom>
    <idiom>
        <phrase>A chain is no stronger than its weakest link</phrase>
        <meaning>The weakest part of an object is it's strength.</meaning>
    </idiom>
</idioms>

Upvotes: 2

Views: 2828

Answers (3)

RedlightGreenLight
RedlightGreenLight

Reputation: 21

I used VS2010 to generate the XML schema from my XML. In doing this it seems to have ignored (private) namespaces but it did set the schema in the UI to be the name of the XML schema it just generated.

Also, it doesn't show in the XML itself that it has using or including this XML schema.

Is this because the XML and XSD files both have the same base name (EnglishIdioms.) and both of these files are in the same directory?

Upvotes: 0

sho222
sho222

Reputation: 722

From the XSD spec:

Element information items ·validated· by a top-level declaration must be qualified with the {target namespace} of that declaration (if this is ·absent·, the item must be unqualified).

Translation:

If you want to use elementFormDefault attribute set to qualified (did you choose that on purpose?), then you'll have to declare a targetNamespace element and set it to some URI, then give it XML that is also declared in that namespace. So, something like

<xsd:schema targetNamespace="urn:so:example" attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
...
</xsd:schema>

And use that namespace in the xml:

<idioms xmlns="urn:so:example">
  <idiom>
    <phrase>A bit much</phrase>
    <meaning>Excessive or annoying.</meaning>
  </idiom>
  <idiom>
    <phrase>A bridge too far</phrase>
    <meaning>Act of overreaching.</meaning>
  </idiom>
</idioms>

Or, I think you would be fine to just remove the elementFormDefault attribute from the scheme tag altogether. The default value for it is unqualified.

Upvotes: 1

leppie
leppie

Reputation: 117330

Your root element is not closed properly.

Use </idioms>, not <idioms> :)

Update:

A lack of a namespace could be the problem (in both the schema and xml).

Upvotes: 3

Related Questions