Another .dtd to xml-schema problem

E

Eric Lilja

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>
]>
<books>
<book title="Winter's Heart" isbn="123456789">
<author name="Robert Jordan"/>
</book>
<book title="Paradiset" isbn="987654321">
<author name="Liza Marklund"/>
</book>
</books>

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="unbounded">
<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_bookbooks' is invalid. Element 'element' is invalid,
misplaced, or occurs too often.

How do I fix this error?

- Eric
 
U

usenet

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="unbounded">
<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_bookbooks' 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="unbounded">
<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="unbounded">
<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: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="unbounded">
... other parts of your sequence...

...then as a separate global type...
<xs:complexType 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)
=============================================
 
E

Eric Lilja

Hello and thanks again, Pete! My comments are below

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="unbounded">
<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_bookbooks' 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="unbounded">
<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="unbounded">
<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>

Thanks again, Pete! I would've gone crazy without you. What you pasted
enabled my schema to validate and my xml-file that use it. The
complete schema now looks like this (probably won't look good on
screen, I'm still using the "russian doll"-style):
<?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="unbounded">
<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> <!-- authors complexType -->
</xs:element> <!-- authors -->
</xs:sequence> <!-- book sequence -->
<xs:attribute name="title" type="xs:string"
use="required"/>
</xs:complexType> <!-- book complexType -->
</xs:element> <!-- book -->
</xs:sequence> <!-- books sequence -->
</xs:complexType> <!-- books complexType -->
</xs:element> <!-- books -->
</xs:schema>

Looks OK?
(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.)

I am typing my schemas in emacs which gives syntax coloring and
indenting but not much else. I will try the VS xml editor as you
suggested.
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="unbounded">
... other parts of your sequence...

...then as a separate global type...
<xs:complexType 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

Yeah, I will probably need to adopt a new style soon.
HTH,

Pete.
--

Thanks so much, Pete!

- Eric
 
A

Arndt Jonasson

I am typing my schemas in emacs which gives syntax coloring and
indenting but not much else. I will try the VS xml editor as you
suggested.

Try nxml-mode. It has support for relax ng schemas to validate on-the-
fly.
I think XML Schema is included from the start.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top