Convert very simple DTD-file to XSD

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

  1. Eric Lilja

    Eric Lilja Guest

    Hello, this is an xml-file with a nested DTD. It validates, test-1-
    with-dtd.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE persons [
    <!ELEMENT persons (person*)>

    <!ELEMENT person EMPTY>
    <!ATTLIST person name CDATA #REQUIRED>
    ]>
    <persons>
    <person name="Eric Lilja" />
    </persons>


    Now I want to use an XML Schema (located in a separate file) instead
    of a nested DTD. I've created the following schema (test-1.xsd):
    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="myns"
    xmlns="myns"
    elementFormDefault="unqualified">
    <xs:element name="persons">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="person">
    <xs:complexType>
    <xs:attribute name="name" type="xs:string"
    use="required"/>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>

    I want this schema to accomplish what my DTD above accomplishes. The
    schema itself validates, but this xml file that tries to use does not.
    test-1-with-xsd.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <persons xmlns="myns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
    instance" xsi:schemaLocation="myns test-1.xsd">
    <person name="Eric Lilja"/>
    </persons>

    The validator says:
    Location: 4:4
    Description: cvc-complex-type.2.4.a: Invalid content was found
    starting with element 'person'. One of '{person}' is expected.

    I'm very new with xml (second day, hehe) and this is my very first
    schema. What is the problem? I guess it's a problem with the XML
    schema even though it validates becuase the test-1-with-xsd.xml that
    uses it looks OK to me (and it should look like the one using the
    DTD).

    - Eric
     
    Eric Lilja, Feb 20, 2007
    #1
    1. Advertising

  2. Eric Lilja

    Guest

    On 20 Feb, 23:57, "Eric Lilja" <> wrote:
    > Hello, this is an xml-file with a nested DTD. It validates, test-1-
    > with-dtd.xml:
    >
    > <?xml version="1.0" encoding="utf-8"?>
    > <!DOCTYPE persons [
    > <!ELEMENT persons (person*)>
    >
    > <!ELEMENT person EMPTY>
    > <!ATTLIST person name CDATA #REQUIRED>
    > ]>
    > <persons>
    > <person name="Eric Lilja" />
    > </persons>
    >
    > Now I want to use an XML Schema (located in a separate file) instead
    > of a nested DTD. I've created the following schema (test-1.xsd):
    > <?xml version="1.0"?>
    > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    > targetNamespace="myns"
    > xmlns="myns"
    > elementFormDefault="unqualified">
    > <xs:element name="persons">
    > <xs:complexType>
    > <xs:sequence>
    > <xs:element name="person">
    > <xs:complexType>
    > <xs:attribute name="name" type="xs:string"
    > use="required"/>
    > </xs:complexType>
    > </xs:element>
    > </xs:sequence>
    > </xs:complexType>
    > </xs:element>
    > </xs:schema>
    >
    > I want this schema to accomplish what my DTD above accomplishes. The
    > schema itself validates, but this xml file that tries to use does not.
    > test-1-with-xsd.xml:
    > <?xml version="1.0" encoding="utf-8"?>
    > <persons xmlns="myns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
    > instance" xsi:schemaLocation="myns test-1.xsd">
    > <person name="Eric Lilja"/>
    > </persons>
    >
    > The validator says:
    > Location: 4:4
    > Description: cvc-complex-type.2.4.a: Invalid content was found
    > starting with element 'person'. One of '{person}' is expected.


    To clear up a simple things first, to match your DTD, the line:

    <xs:element name="person">

    should be:

    <xs:element name="person" minOccurs="0" maxOccurs="unbound">

    The main problem you have though is a bit more involved. In your XML
    instance you've defined the default namespace to be 'myns'. In your
    schema you've specified that local elements are unqualified. When the
    tag of the person element is read, the default namespace takes
    precedence over the 'no' namespace. So the name returned in
    effectively:

    { namespace="myns"; localName="person"; }.

    However, because the local elements are marked as unqualified, the
    processor is expecting:

    { namespace=""; localName="person"; }.

    As you can see, they don't match, hence the problem.

    To fix this, you can either set elementFormDefault="qualified", which
    is probably the right thing to do in a schema.

    Or you can say that DTDs are not namespace aware, and if you want to
    replicate your DTD exactly you should not associate your schema with a
    namespace either. In this case you would remove the
    targetNamespace="myns" and xmlns="myns" attributes from teh schema.

    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

    Hi Pete and thanks for your reply! My answers are below... :)

    On 21 Feb, 10:41, wrote:
    > On 20 Feb, 23:57, "Eric Lilja" <> wrote:
    >
    >
    >
    > > Hello, this is an xml-file with a nested DTD. It validates, test-1-
    > > with-dtd.xml:

    >
    > > <?xml version="1.0" encoding="utf-8"?>
    > > <!DOCTYPE persons [
    > > <!ELEMENT persons (person*)>

    >
    > > <!ELEMENT person EMPTY>
    > > <!ATTLIST person name CDATA #REQUIRED>
    > > ]>
    > > <persons>
    > > <person name="Eric Lilja" />
    > > </persons>

    >
    > > Now I want to use an XML Schema (located in a separate file) instead
    > > of a nested DTD. I've created the following schema (test-1.xsd):
    > > <?xml version="1.0"?>
    > > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    > > targetNamespace="myns"
    > > xmlns="myns"
    > > elementFormDefault="unqualified">
    > > <xs:element name="persons">
    > > <xs:complexType>
    > > <xs:sequence>
    > > <xs:element name="person">
    > > <xs:complexType>
    > > <xs:attribute name="name" type="xs:string"
    > > use="required"/>
    > > </xs:complexType>
    > > </xs:element>
    > > </xs:sequence>
    > > </xs:complexType>
    > > </xs:element>
    > > </xs:schema>

    >
    > > I want this schema to accomplish what my DTD above accomplishes. The
    > > schema itself validates, but this xml file that tries to use does not.
    > > test-1-with-xsd.xml:
    > > <?xml version="1.0" encoding="utf-8"?>
    > > <persons xmlns="myns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
    > > instance" xsi:schemaLocation="myns test-1.xsd">
    > > <person name="Eric Lilja"/>
    > > </persons>

    >
    > > The validator says:
    > > Location: 4:4
    > > Description: cvc-complex-type.2.4.a: Invalid content was found
    > > starting with element 'person'. One of '{person}' is expected.

    >
    > To clear up a simple things first, to match your DTD, the line:
    >
    > <xs:element name="person">
    >
    > should be:
    >
    > <xs:element name="person" minOccurs="0" maxOccurs="unbound">


    Ah, thanks for spotting that one! Changed to <xs:element name="person"
    minOccurs="0" maxOccurs="unbounded">

    >
    > The main problem you have though is a bit more involved. In your XML
    > instance you've defined the default namespace to be 'myns'. In your
    > schema you've specified that local elements are unqualified. When the
    > tag of the person element is read, the default namespace takes
    > precedence over the 'no' namespace. So the name returned in
    > effectively:
    >
    > { namespace="myns"; localName="person"; }.
    >
    > However, because the local elements are marked as unqualified, the
    > processor is expecting:
    >
    > { namespace=""; localName="person"; }.
    >
    > As you can see, they don't match, hence the problem.
    >
    > To fix this, you can either set elementFormDefault="qualified", which
    > is probably the right thing to do in a schema.
    >
    > Or you can say that DTDs are not namespace aware, and if you want to
    > replicate your DTD exactly you should not associate your schema with a
    > namespace either. In this case you would remove the
    > targetNamespace="myns" and xmlns="myns" attributes from teh schema.
    >
    > HTH,
    >
    > Pete.



    Oh, yes, that helps alot, Pete! Thanks so much! I've made a version
    that doesn't incorporate namespaces and it passes validation. It looks
    like this:
    persons.xsd:
    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="persons">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="person" minOccurs="0"
    maxOccurs="unbounded">
    <xs:complexType>
    <xs:attribute name="name" type="xs:string"
    use="required"/>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>

    persons.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="persons.xsd">
    <person name="Eric Lilja"/>
    </persons>

    Now I'm going to try to make a namespace version and when that works I
    will try to tackle some more complicated DTDs (but still simple).
    Thanks again, you've been a tremendous help!

    - Eric
     
    Eric Lilja, Feb 21, 2007
    #3
  4. Eric Lilja

    Eric Lilja Guest

    See namespace version below

    On 21 Feb, 13:28, "Eric Lilja" <> wrote:
    > Hi Pete and thanks for your reply! My answers are below... :)
    >
    > On 21 Feb, 10:41, wrote:
    >
    >
    >
    > > On 20 Feb, 23:57, "Eric Lilja" <> wrote:

    >
    > > > Hello, this is an xml-file with a nested DTD. It validates, test-1-
    > > > with-dtd.xml:

    >
    > > > <?xml version="1.0" encoding="utf-8"?>
    > > > <!DOCTYPE persons [
    > > > <!ELEMENT persons (person*)>

    >
    > > > <!ELEMENT person EMPTY>
    > > > <!ATTLIST person name CDATA #REQUIRED>
    > > > ]>
    > > > <persons>
    > > > <person name="Eric Lilja" />
    > > > </persons>

    >
    > > > Now I want to use an XML Schema (located in a separate file) instead
    > > > of a nested DTD. I've created the following schema (test-1.xsd):
    > > > <?xml version="1.0"?>
    > > > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    > > > targetNamespace="myns"
    > > > xmlns="myns"
    > > > elementFormDefault="unqualified">
    > > > <xs:element name="persons">
    > > > <xs:complexType>
    > > > <xs:sequence>
    > > > <xs:element name="person">
    > > > <xs:complexType>
    > > > <xs:attribute name="name" type="xs:string"
    > > > use="required"/>
    > > > </xs:complexType>
    > > > </xs:element>
    > > > </xs:sequence>
    > > > </xs:complexType>
    > > > </xs:element>
    > > > </xs:schema>

    >
    > > > I want this schema to accomplish what my DTD above accomplishes. The
    > > > schema itself validates, but this xml file that tries to use does not.
    > > > test-1-with-xsd.xml:
    > > > <?xml version="1.0" encoding="utf-8"?>
    > > > <persons xmlns="myns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
    > > > instance" xsi:schemaLocation="myns test-1.xsd">
    > > > <person name="Eric Lilja"/>
    > > > </persons>

    >
    > > > The validator says:
    > > > Location: 4:4
    > > > Description: cvc-complex-type.2.4.a: Invalid content was found
    > > > starting with element 'person'. One of '{person}' is expected.

    >
    > > To clear up a simple things first, to match your DTD, the line:

    >
    > > <xs:element name="person">

    >
    > > should be:

    >
    > > <xs:element name="person" minOccurs="0" maxOccurs="unbound">

    >
    > Ah, thanks for spotting that one! Changed to <xs:element name="person"
    > minOccurs="0" maxOccurs="unbounded">
    >
    >
    >
    >
    >
    > > The main problem you have though is a bit more involved. In your XML
    > > instance you've defined the default namespace to be 'myns'. In your
    > > schema you've specified that local elements are unqualified. When the
    > > tag of the person element is read, the default namespace takes
    > > precedence over the 'no' namespace. So the name returned in
    > > effectively:

    >
    > > { namespace="myns"; localName="person"; }.

    >
    > > However, because the local elements are marked as unqualified, the
    > > processor is expecting:

    >
    > > { namespace=""; localName="person"; }.

    >
    > > As you can see, they don't match, hence the problem.

    >
    > > To fix this, you can either set elementFormDefault="qualified", which
    > > is probably the right thing to do in a schema.

    >
    > > Or you can say that DTDs are not namespace aware, and if you want to
    > > replicate your DTD exactly you should not associate your schema with a
    > > namespace either. In this case you would remove the
    > > targetNamespace="myns" and xmlns="myns" attributes from teh schema.

    >
    > > HTH,

    >
    > > Pete.

    >
    > Oh, yes, that helps alot, Pete! Thanks so much! I've made a version
    > that doesn't incorporate namespaces and it passes validation. It looks
    > like this:
    > persons.xsd:
    > <?xml version="1.0"?>
    > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    > <xs:element name="persons">
    > <xs:complexType>
    > <xs:sequence>
    > <xs:element name="person" minOccurs="0"
    > maxOccurs="unbounded">
    > <xs:complexType>
    > <xs:attribute name="name" type="xs:string"
    > use="required"/>
    > </xs:complexType>
    > </xs:element>
    > </xs:sequence>
    > </xs:complexType>
    > </xs:element>
    > </xs:schema>
    >
    > persons.xml:
    > <?xml version="1.0" encoding="utf-8"?>
    > <persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > xsi:noNamespaceSchemaLocation="persons.xsd">
    > <person name="Eric Lilja"/>
    > </persons>
    >
    > Now I'm going to try to make a namespace version and when that works I
    > will try to tackle some more complicated DTDs (but still simple).
    > Thanks again, you've been a tremendous help!
    >
    >


    Here's my namespace version, does it look ok? It passes validation.
    persons.xsd:
    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="myns" xmlns="myns" elementFormDefault="qualified">
    <xs:element name="persons">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="person" minOccurs="0"
    maxOccurs="unbounded">
    <xs:complexType>
    <xs:attribute name="name" type="xs:string"
    use="required"/>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>

    persons.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <persons xmlns="myns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-
    instance" xsi:schemaLocation="myns persons.xsd">
    <person name="Eric Lilja"/>
    </persons>

    All input appreciated!

    - Eric
     
    Eric Lilja, Feb 21, 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. Replies:
    1
    Views:
    912
    Martin Honnen
    Jan 14, 2004
  2. Manuel Collado

    Looking for very simple document DTD

    Manuel Collado, May 31, 2004, in forum: XML
    Replies:
    5
    Views:
    355
    Manuel Collado
    Jun 2, 2004
  3. Matt
    Replies:
    3
    Views:
    37,334
    MKNDK
    Sep 11, 2008
  4. test
    Replies:
    2
    Views:
    2,175
    Oliver Wong
    Jul 28, 2006
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,376
Loading...

Share This Page