473,231 Members | 1,634 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Convert very simple DTD-file to XSD

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

Feb 21 '07 #1
3 3075
On 20 Feb, 23:57, "Eric Lilja" <mindcoo...@gmail.comwrote:
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 '07 #2
Hi Pete and thanks for your reply! My answers are below... :-)

On 21 Feb, 10:41, use...@tech-know-ware.com wrote:
On 20 Feb, 23:57, "Eric Lilja" <mindcoo...@gmail.comwrote:
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

Feb 21 '07 #3
See namespace version below

On 21 Feb, 13:28, "Eric Lilja" <mindcoo...@gmail.comwrote:
Hi Pete and thanks for your reply! My answers are below... :-)

On 21 Feb, 10:41, use...@tech-know-ware.com wrote:
On 20 Feb, 23:57, "Eric Lilja" <mindcoo...@gmail.comwrote:
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

Feb 21 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Manuel Collado | last post by:
I would like to write simple, yet well structured documents with a really simple XML DTD (or schema). Either Docbook or SDocbook are overkill for this simple case. XHTML is simpler, but...
2
by: Matt | last post by:
Can we convert XML to XSD? or DTD to XSD? Can XML SPY or other XML Editors do that? Please advise. thanks!!
4
by: Tony G | last post by:
Hi - My experience with XML is limitted although I understand the basics, and I just recently for the first time created an XML document from a database query and styled it successfully with...
1
by: BenOne© | last post by:
Hi all, I'm learning CSS, and relearning HTML so I can build a website for myself, and I was wondering if someone could help me with what seems like a simple layout that I want to maintain no...
7
by: Tony LaPaso | last post by:
Hi All, I have a simple style sheet example below and I'm seeing different results in IE 6 vs. Firefox 1.0.3. I'm not sure which browser is rendering it correctly but I'm tending to think it's...
3
by: PJ | last post by:
Hi, How do we convert XML schema into DTD priyanshu
2
by: name | last post by:
The piece of code is for a Web Form Page. Who can tell me why? Thanks a lot! ------------------------------------------------ VB.Net Code: Protected Overrides Sub AddParsedSubObject(ByVal obj As...
1
by: PATRICIA THOMPSON | last post by:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta...
1
Death Slaught
by: Death Slaught | last post by:
I will be showing you how to make a very simple but effective three column layout. First we will begin with the HTML, or structure, of our three column layout. <!DOCTYPE html PUBLIC...
0
rnd me
by: rnd me | last post by:
Purpose: Allows you to create "presets" for text form inputs. "Lightweight and simple to setup, it adds a lot of convenience for ~1kb of code." Only one function, two parameters: First...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.