Another .dtd to xml-schema problem

Eric Lilja

Usenet Archive
Joined
Oct 27, 2004
Messages
150
Reaction score
0
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
 

usenet

Usenet Archive
Joined
Aug 10, 2000
Messages
1,105
Reaction score
0
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.
 

Eric Lilja

Usenet Archive
Joined
Oct 27, 2004
Messages
150
Reaction score
0
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
 

Arndt Jonasson

Usenet Archive
Joined
Oct 11, 2004
Messages
255
Reaction score
0
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

No members online now.

Forum statistics

Threads
548,051
Messages
3,104,251
Members
248,135
Latest member
tobystanx

Latest Threads

Top