Another .dtd to xml-schema problem

Discussion in 'XML' started by Eric Lilja, Feb 21, 2007.

  1. Eric Lilja

    Eric Lilja Guest

    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
     
    Eric Lilja, Feb 21, 2007
    #1
    1. Advertising

  2. Eric Lilja

    Guest

    On 21 Feb, 13:55, "Eric Lilja" <> 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 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>

    (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)
    =============================================
     
    , Feb 21, 2007
    #2
    1. Advertising

  3. Eric Lilja

    Eric Lilja Guest

    Hello and thanks again, Pete! My comments are below

    On 21 Feb, 15:38, wrote:
    > On 21 Feb, 13:55, "Eric Lilja" <> 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 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
     
    Eric Lilja, Feb 21, 2007
    #3
  4. On Feb 21, 4:39 pm, "Eric Lilja" <> wrote:
    > On 21 Feb, 15:38, 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.
     
    Arndt Jonasson, Feb 22, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,612
    Markus
    Nov 23, 2005
  2. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,296
    Stanimir Stamenkov
    Apr 25, 2005
  3. test
    Replies:
    2
    Views:
    2,086
    Oliver Wong
    Jul 28, 2006
  4. Replies:
    3
    Views:
    2,955
  5. pramodr
    Replies:
    3
    Views:
    853
    Peter Flynn
    Apr 5, 2009
Loading...

Share This Page