Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Another .dtd to xml-schema problem

Reply
Thread Tools

Another .dtd to xml-schema problem

 
 
Eric Lilja
Guest
Posts: n/a
 
      02-21-2007
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 xmlnss="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

 
Reply With Quote
 
 
 
 
usenet@tech-know-ware.com
Guest
Posts: n/a
 
      02-21-2007
On 21 Feb, 13:55, "Eric Lilja" <(E-Mail Removed)> wrote:
> 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 xmlnss="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>

(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)
=============================================

 
Reply With Quote
 
 
 
 
Eric Lilja
Guest
Posts: n/a
 
      02-21-2007
Hello and thanks again, Pete! My comments are below

On 21 Feb, 15:38, (E-Mail Removed) wrote:
> On 21 Feb, 13:55, "Eric Lilja" <(E-Mail Removed)> wrote:
>
>
>
> > 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 xmlnss="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 xmlnss="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


 
Reply With Quote
 
Arndt Jonasson
Guest
Posts: n/a
 
      02-22-2007
On Feb 21, 4:39 pm, "Eric Lilja" <(E-Mail Removed)> wrote:
> On 21 Feb, 15:38, (E-Mail Removed) wrote:
> > (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.


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to force a load of one control from another control on another form? et ASP .Net 1 06-29-2005 07:45 PM
Renaming a project with another project as the starting point? In another solution Maziar Aflatoun ASP .Net 1 01-22-2005 12:49 AM
How to access: a page from a User control, and another User controlfrom another one? qwerty ASP .Net 3 09-30-2004 05:32 PM
Access a control inside an usercontrol from another control inside another usercontrol nail ASP .Net 0 09-15-2004 03:55 PM
How do I write HTML tags for an applet that imports classes from another jar in another package? Ding Java 1 07-01-2004 03:41 AM



Advertisments