my first xml schema - I think it's good. Is it really?

U

Ultrus

Hello XML gurus,
After much reading, I made my first XML Schema! May I trouble you all
for your thoughts? Perhaps there is a better way to go about it, or I
did something totally wrong without knowing.

My project goal is to make a randomly generated story generator using
recursive xml. There will be random elements within random elements,
within random elements, etc.,.


Here is sample xml:

<randomstory>
<settings />
<story>
Once upon a time...
<random>
<i>A man was born. No one knew what became of him.</i>
<i>
A man was born.
<random>
<i>He grew up, lived long, prospered, then died.</i>
<i>He joined the Navy, fell off the boat, then died.</i>
</random>
</i>
</random>
The end.
</story>
</randomstory>


Here is sample output:

Once upon a time... A man was born. He joined the Navy, fell off the
boat, then died. The end.


Here is the schema:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">

<xs:element name="randomstory">
<xs:complexType>
<xs:all>
<xs:element name="settings" minOccurs="0" maxOccurs="1" />
<xs:element name="show" minOccurs="1" maxOccurs="1">
<xs:complexType mixed="true">
<xs:all>
<xs:element name="random" type="randomType"
minOccurs="0" maxOccurs="unbounded"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>

<xs:complexType name="randomType">
<xs:sequence>
<xs:element name="i" type="iType" minOccurs="1"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="iType" mixed="true">
<xs:all>
<xs:element name="random" type="randomType" minOccurs="0"
maxOccurs="unbounded"/>
</xs:all>
</xs:complexType>

Pardon any odd line breaks. Any thoughts? Much appreciated! :)
 
P

Peter Flynn

Ultrus said:
Hello XML gurus,
After much reading, I made my first XML Schema! May I trouble you all
for your thoughts? Perhaps there is a better way to go about it, or I
did something totally wrong without knowing.

My project goal is to make a randomly generated story generator using
recursive xml. There will be random elements within random elements,
within random elements, etc.,.

It's very hard to understand what the structure of your document is.

1. Don't name things according to how they got there, but according to
their role in the document.

2. Don't leave text floating loose and unmarked within a structural
container (your "Once upon a time" inside <story>). Put it in a
functional element type like a paragraph.

3. Don't use presentational markup (<i>), especially not nested in
itself (what does <i> within <i> *mean*?)

4. If the generated text strings are intended as options in the
storyline, keep them separately identifiable.

5. You certainly could use a Schema for this, but a DTD is probably
simpler and easier.

<?xml version="1.0"?>
<!DOCTYPE story [
<!ELEMENT story (settings,body)>
<!ATTLIST story id ID #REQUIRED>
<!ELEMENT settings EMPTY>
<!ELEMENT body (para+)>
<!ELEMENT para (text|phrase)+>
<!ELEMENT text (#PCDATA)>
<!ELEMENT phrase (thread+)>
<!ELEMENT thread (text|phrase)+>
]>
<story id="ABC123">
<settings />
<body>
<para>
<text>Once upon a time...</text>
<phrase>
<thread>
<text>A man was born. No one knew what became of him.</text>
</thread>
<thread>
<text>A man was born.</text>
<phrase>
<thread>
<text>He grew up, lived long, prospered, then
died.</text>
</thread>
<thread>
<text>He joined the Navy, fell off the boat, then
died.</text>
</thread>
</phrase>
</thread>
</phrase>
</para>
<para>
<text>The end.</text>
</para>
</body>
</story>

///Peter
 
U

Ultrus

Peter,
That was very helpful! I can see how my initial draft could be
confusing. Your DTD was also very much smaller than the XML Schema.

While chatting, research, and posting, people are telling me that XML
Schema may replace DTD. What are your thoughts on this? To me this
doesn't matter right now as my xml interpreter will take DTD or XML
Schema for validation purposes. DTD does seam simpler in this case.
 
J

Joe Kesselman

Ultrus said:
While chatting, research, and posting, people are telling me that XML
Schema may replace DTD.

That's the W3C's intent. DTDs are inherently in conflict with
namespace-aware processing of XML. Schemas are fully namespace-aware.
And since namespaces are a basic part of XML these days...

Yes, Schema is more verbose. It's also more powerful.

Yes, there are other schema languages out there in competition with XML
Schema. They aren't getting a great deal of traction, even though they
may be better in some ways, simply because XML Schema is the W3C's
endorsed Recommendation (and therefore is going to be most widely
supported).

It's worth understanding both DTDs and XML Schema. But DTDs are slowly
becoming less useful.
 
D

David Carlisle

Joe said:
Yes, there are other schema languages out there in competition with XML
Schema. They aren't getting a great deal of traction, even though they
may be better in some ways, simply because XML Schema is the W3C's
endorsed Recommendation (and therefore is going to be most widely
supported).
I think that depends greatly on the type of documents being used. For
"data" formats XSD will win out because as you say, it's there and has a
w3c badge and tool support. But for more document related formats XSD is
pretty widely ignored (or tolerated at arms length, just mechanically
generating an xsd schema from relax or dtd when/if a tool requires it).
docbook, one of the more popular formats in this area is, in its latest
version, only described via relax ng, XHTML 2 drafts have appendices for
xsd, relax and dtd, but only the relax ones have any content.

David
 
J

Joe Kesselman

David said:
But for more document related formats XSD is
pretty widely ignored

Part of the problem is one of territoriality; XSD was strongly
influenced by the database folks and reflects their biases, while as you
say RelaxNG was more influenced by the document-markup folks and is
arguably better turned for those applications.

But DTDs are still pretty much dead in the water the moment you want to
work with namespaces -- and almost any modern XML application, no matter
what its problem domain, will want to be aware of namespaces. Yes, it's
possible to force-fit namespaces into DTDs by hardwiring them and/or by
using parameter entities -- I did it, years ago, when DTDs were all we
had -- but that really isn't a maintainable approach.

If you don't want to paint yourself into a corner, DTDs really are no
longer the best solution.
 
U

usenet

Hello XML gurus,
After much reading, I made my first XML Schema! May I trouble you all
for your thoughts? Perhaps there is a better way to go about it, or I
did something totally wrong without knowing.

My project goal is to make a randomly generated story generator using
recursive xml. There will be random elements within random elements,
within random elements, etc.,.

Here is sample xml:

<randomstory>
<settings />
<story>
Once upon a time...
<random>
<i>A man was born. No one knew what became of him.</i>
<i>
A man was born.
<random>
<i>He grew up, lived long, prospered, then died.</i>
<i>He joined the Navy, fell off the boat, then died.</i>
</random>
</i>
</random>
The end.
</story>
</randomstory>

Here is sample output:

Once upon a time... A man was born. He joined the Navy, fell off the
boat, then died. The end.

Here is the schema:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">

<xs:element name="randomstory">
<xs:complexType>
<xs:all>
<xs:element name="settings" minOccurs="0" maxOccurs="1" />
<xs:element name="show" minOccurs="1" maxOccurs="1">
<xs:complexType mixed="true">
<xs:all>
<xs:element name="random" type="randomType"
minOccurs="0" maxOccurs="unbounded"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>

<xs:complexType name="randomType">
<xs:sequence>
<xs:element name="i" type="iType" minOccurs="1"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="iType" mixed="true">
<xs:all>
<xs:element name="random" type="randomType" minOccurs="0"
maxOccurs="unbounded"/>
</xs:all>
</xs:complexType>

Pardon any odd line breaks. Any thoughts? Much appreciated! :)

Just a quick note on your schema...

An element within an xs:all can not have a maxOccurs greater than 1.

I think for what you want, you could change the xs:all to an
xs:choice.

HTH,

Pete.
--
=============================================
Pete Cordell
Codalogic Ltd
for XML Schema to C++ data binding visit
http://www.codalogic.com/lmx/
=============================================
 
U

Ultrus

Thanks all for your feedback!

I just realized that my interpreter will also use RelaxNG to validate
xml documents. I'm reading up on it now as well. It seems to read a
little easier than XML Schema. Joe noted that the database people
influenced XML Schema while the markup people influenced RelaxNG. I
don't see RelaxNG listed on the W3C's website. Is RelaxNG a dead end
in some way like DTDs are with namespaces?
 
J

Joseph Kesselman

Ultrus said:
don't see RelaxNG listed on the W3C's website. Is RelaxNG a dead end
in some way like DTDs are with namespaces?

RelaxNG is not part of the W3C's standard recommendations suite; it's a
competing tool. That isn't _necessarily_ a reason to avoid it; after
all, the SAX APIs didn't originate in the W3C either... but this is part
of the reason Relax is not more widely supported.

If you're concerned with portability of your data, stick with XML Schema
and/or DTD.
 
D

David Carlisle

Joe said:
Part of the problem is one of territoriality; XSD was strongly
influenced by the database folks and reflects their biases, while as you
say RelaxNG was more influenced by the document-markup folks and is
arguably better turned for those applications.

But DTDs are still pretty much dead in the water the moment you want to
work with namespaces -- and almost any modern XML application, no matter
what its problem domain, will want to be aware of namespaces. Yes, it's
possible to force-fit namespaces into DTDs by hardwiring them and/or by
using parameter entities -- I did it, years ago, when DTDs were all we
had -- but that really isn't a maintainable approach.

If you don't want to paint yourself into a corner, DTDs really are no
longer the best solution.

yes true, as far as it goes, of course it does leave me (as editor of
the mathml dtd with its ~2000 entity definitions with no viable schema
technology replacement) with something of a problem:)

David
 
J

Joe Kesselman

David said:
yes true, as far as it goes, of course it does leave me (as editor of
the mathml dtd with its ~2000 entity definitions with no viable schema
technology replacement) with something of a problem:)

Yeah, I know. Entities are the one thing DTDs did that schemas decided
not to. The current recommendations are either to use just enough of a
DTD to provide those and to use schema for the rest (ugh), or to use
some other macro facility or to define tags for the purpose (which
doesn't address languages that started out in the DTD world). Sigh.

XML was rushed into use before it was stable. The *right* way to do it
would have been to fully define the infoset, with types and namespaces,
and then to derive the schema language and syntax and so on from that...
but we didn't have that luxury; in order for XML to win acceptance as
quickly as it did, it had to be rolled out in stages, syntax first.
That's left us with a few warts. Someday there may be an XML 2.0 which
goes back and cleans everything up -- probably about the time XML is
ready to graduate from Recommendation to Standard -- but that isn't
likely to happen very soon.

XML is still a good thing; it could be better but we're already using it
productively and nobody really wants to delay that while we wait.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top