On 21 Feb, 13:55, "Eric Lilja" <mindcoo...@gma il.comwrote:
Hello again, I'm having a new problem converting a dtd to an xml
schema. This once is a little bit more complicated than in my previous
question.
The following file validates correctly:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE books [
<!ELEMENT books (book*)>
<!ELEMENT book (author)>
<!ATTLIST book title CDATA #REQUIRED isbn CDATA #REQUIRED>
<!ELEMENT author EMPTY>
<!ATTLIST author name CDATA #REQUIRED>
]>
...
Now I want to convert the DTD to an xml schema. Here's my failed
attempt:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="books">
<xs:complexType >
<xs:sequence>
<xs:element name="book" minOccurs="0"
maxOccurs="unbo unded">
<xs:complexType >
<xs:attribute name="title" type="xs:string "
use="required"/>
<xs:element name="authors" minOccurs="1"
maxOccurs="1">
<xs:complexType >
<xs:attribute name="name" type="xs:string "
use="required"/>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
My validator complains:
Location: 9:20
Description: s4s-elt-invalid-content.1: The content of
'#AnonType_book books' is invalid. Element 'element' is invalid,
misplaced, or occurs too often.
How do I fix this error?
The following bit looks a bit grim :-)
<xs:element name="book" minOccurs="0" maxOccurs="unbo unded">
<xs:complexType >
<xs:attribute name="title" type="xs:string " use="required"/
>
<xs:element name="authors" minOccurs="1" maxOccurs="1">
<xs:complexType >
<xs:attribute name="name" type="xs:string "
use="required"/>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
I think it should be more like this:
<xs:element name="book" minOccurs="0" maxOccurs="unbo unded">
<xs:complexType >
<xs:sequence>
<xs:element name="authors" minOccurs="1"
maxOccurs="1">
<xs:complexType >
<xs:attribute name="name" type="xs:string "
use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="title" type="xs:string " use="required"/
>
</xs:complexType>
</xs:element>
(This is where having a schema editor really helps. Visual Studio
2003/5 suffice and I guess there are others. I'm not sure if the
Express editions support schema editing.)
BTW - by nesting all your definitions you are adopting what is called
a Russian Doll design. That's not all bad, but after a while you end
up having to buy a wider monitor! You could define your schemas along
the lines of:
<xs:element name="book" type="Book" minOccurs="0"
maxOccurs="unbo unded">
... other parts of your sequence...
...then as a separate global type...
<xs:complexTy pe type="Book">
<xs:sequence>
<xs:element name="authors" minOccurs="1" maxOccurs="1">
<xs:complexType >
<xs:attribute name="name" type="xs:string "
use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="title" type="xs:string " use="required"/>
</xs:complexType>
This is called venetian blind. It makes things a lot flatter, and
it's usually easier to see the sorts of mistakes that you had earlier.
You can see examples of venetian blind design in my own little
tutorial at:
http://www.tech-know-ware.com/lmx/xsd-overview.html
HTH,
Pete.
--
=============== =============== ===============
Pete Cordell
Tech-Know-Ware Ltd
for XML to C++ data binding visit
http://www.tech-know-ware.com/lmx
(or
http://www.xml2cpp.com)
=============== =============== ===============