Domain: relaxng.org
Stories and comments across the archive that link to relaxng.org.
Comments · 11
-
Your example doesn't have to be verbose
The following in RelaxNG compact syntax might be more palatable to some:
element myelement { xsd:token { minLength="1" maxLength="20" } }
Great post. I heartily enjoyed it. Cheers! -
Make working with XML suck less...
"XML is really just data dressed up as a hooker."
XML does suck if you stick with some of the W3C standards and common tools. Suggestions to make it less painful:
- Ditch W3C's XML Schema
W3C Schema is painful; it forces object-oriented design concepts onto a hierarchical data model. Consider RELAX NG (an Oasis-approved standard) instead; it's delightful in comparison. Use the verbose XML syntax when communicating with the less technical - if you've seen XML before, it's pretty easy to comprehend:
<r:optional>
<r:element name="w3cSchemaDescription">
<r:choice>
<r:value>painful</r:value>
<r:value>ugly</r:value>
<r:value>inflexible</r:value>
</r:choice>
</r:element>
</r:optional>Switch to the compact syntax when you're among geeks:
element w3cSchemaDescription { "painful" | "ugly" | "inflexible" }?
There's validation support on major platforms, and even a tool (Trang) to convert between verbose/compact formats, and output to DTD and W3C Schemas. And, if you need to specify data types, it borrows the one technology W3C Schema got right: the Datatypes library.
- Don't use the W3C DOM
The W3C DOM attempts to be a universal API, which means it must conform to the lowest common denominator in the programming languages it targets. Consider the NodeList interface:
interface NodeList {
Node item(in unsigned long index);
readonly attribute unsigned long length;
};While similar to the native list/collection/array interfaces most languages provide, it's not an exact match. So, DOM implementers create an object that doesn't work quite like any other collection on the platform. In Java, this means writing:
for(int i = 0; i < nodeList.length(); i++)
{
Node node = nodeList.item(i); // Do something with node here...
}Instead of:
for(Node node : nodeList)
{ // Do something with node here...
}Dynamic languages allow an even more concise syntax. Consider this Ruby builder code to build a trivial XML document:
x.date {
x.year "2006"
x.month "01"
x.day "01"
}I thought about writing the W3C DOM equivalent of the above, but I'm not feeling masochistic tonight. Sorry.
The alternatives depend on your programming language, but plenty of choices exist for DOM-style traversal/manipulation.
- Forget document models entirely (maybe)
In-memory object models of large XML document can consume a lot of resources, but often, you only need part of the data. Consider using an XMLPull or StAX parser instead. Pull means you control the document traversal, only descending into (and fully parsing) sections of the XML that are of interest. SAX based parsers have equivalent capabilities, but the programming model is uncomfortable for many developers.
Even better, some Pull processors are wicked fast, even when using them to construct a DOM. In Winter 2006, I benchmarked an XML-heavy application, and found WoodStox to be an order of magnitude faster at constructing thousands of small DOM4J documents
- Ditch W3C's XML Schema
-
Choose a good schema languageIf you're using XMLSchema, then ditch that crap and use RelaxNG, which is actually readable. There's a compact syntax that is even more user-friendly. As an example, the schema for:
<addressBook>
looks like this:
<card>
<name>John Smith</name>
<email>js@example.com</email>
</card>
<card>
<name>Fred Bloggs</name>
<email>fb@example.net</email>
</card>
</addressBook>element addressBook {
Use your imagination to pretend that
element card {
element name { text },
element email { text }
}*
} /. preserves indentation. --- SER -
Choose a good schema languageIf you're using XMLSchema, then ditch that crap and use RelaxNG, which is actually readable. There's a compact syntax that is even more user-friendly. As an example, the schema for:
<addressBook>
looks like this:
<card>
<name>John Smith</name>
<email>js@example.com</email>
</card>
<card>
<name>Fred Bloggs</name>
<email>fb@example.net</email>
</card>
</addressBook>element addressBook {
Use your imagination to pretend that
element card {
element name { text },
element email { text }
}*
} /. preserves indentation. --- SER -
Re:A few problems with RelaxNG validation
There's a reason for that!
Here's a discussion about it on the Relaxng-user mailing list:
http://relaxng.org/pipermail/relaxng-user/2003-Oct ober/thread.html
>> I'm a relatively new "convert" (from XML Schema) to RELAX NG. I understand that there is no standard way to associate a RELAX NG schema with a document. I'm just wondering if there is any plan to make this possible.
> Not really. The theory is that you might want to validate a document against different schemas for different purposes, and no one schema is really preferred.
James Clark weighs in with his usual clarity:
>> In simpler words, the people who designed the technology don't see a consistent way to formally express an association that already exists, or didn't implement it yet.
> It's part of the general problem of specifying appropriate XML processing; an RNG-specific solution is neither particularly general nor, IMHO, particularly useful.
I would divide the problem of specifying appropriate XML processing for a document into:
(a) how to specify the process to be performed
(b) how to locate the appropriate processing specificationI see (b) as a special case of the problem of how to specify rules that, given an XML document, find a related resource. This is problem that the XML vocabulary that I've designed for nXML mode is intended to solve. It's not specific to RELAX NG or for that matter to schemas. You could use the same vocabulary to describe how to find the XSLT stylesheet to use to display an XML document.
[...]
Although it's important to be able to individually specify the schema to use for a particular document, it's also convenient to be able to specify rules that apply to classes of document. For example, on my system I have a rule that says when the namespace URI of the document element is http://relaxng.org/ns/structure/1.0, then the schema is
/home/jjc/schema/relaxng.rnc.James
> Hum, that's a place where I would expect the XML Catalogs to take a role in abstracting the file paths.
I think that's an independent issue. If you are in an environment that has a policy of using XML catalogs for URI remapping in XML-related contexts, then it would make sense to use them for remapping both URIs occuring in include/externalRef in schemas and URIs occurring in locating files. However I don't see any need to explicitly couple locating files to catalogs. My personal opinion is that, although XML catalogs are an appropriate solution to the problem of publicId-to-URI mapping, using XML catalogs to perform URI-to-URI mapping is an XML-specific solution to a non-XML-specific problem.
James
> Thus, for me the only reasonable choice is still to use the DOCTYPE declaration for all associations
If you want to use DOCTYPEs, the nXML method can accomodate you (by doctypePublicId rules). However, I find the problems of using DOCTYPEs worse by far than the problem of associations disappearing on a rename. And even with DOCTYPEs, you can still get problem of the association changing; you still have to associate your DOCTYPEs with schemas. If you force me to put something in the instance, I would much prefer a processing instruction.
There's no single right way to do the association. Different users will legitimately prefer different approaches. A solution needs to be flexible enough to accomodate them.
James
> My opinion is that this association should be obligatory once present and could not be overriden.
It's a basic tenet of RELAX NG that the schema is not inherent in the document and that validation is a process that has two independently-specifiable inputs. Section 8 of the spec says: "A conforming RELAX NG validator must be a
-
Re:A few problems with RelaxNG validation
There's a reason for that!
Here's a discussion about it on the Relaxng-user mailing list:
http://relaxng.org/pipermail/relaxng-user/2003-Oct ober/thread.html
>> I'm a relatively new "convert" (from XML Schema) to RELAX NG. I understand that there is no standard way to associate a RELAX NG schema with a document. I'm just wondering if there is any plan to make this possible.
> Not really. The theory is that you might want to validate a document against different schemas for different purposes, and no one schema is really preferred.
James Clark weighs in with his usual clarity:
>> In simpler words, the people who designed the technology don't see a consistent way to formally express an association that already exists, or didn't implement it yet.
> It's part of the general problem of specifying appropriate XML processing; an RNG-specific solution is neither particularly general nor, IMHO, particularly useful.
I would divide the problem of specifying appropriate XML processing for a document into:
(a) how to specify the process to be performed
(b) how to locate the appropriate processing specificationI see (b) as a special case of the problem of how to specify rules that, given an XML document, find a related resource. This is problem that the XML vocabulary that I've designed for nXML mode is intended to solve. It's not specific to RELAX NG or for that matter to schemas. You could use the same vocabulary to describe how to find the XSLT stylesheet to use to display an XML document.
[...]
Although it's important to be able to individually specify the schema to use for a particular document, it's also convenient to be able to specify rules that apply to classes of document. For example, on my system I have a rule that says when the namespace URI of the document element is http://relaxng.org/ns/structure/1.0, then the schema is
/home/jjc/schema/relaxng.rnc.James
> Hum, that's a place where I would expect the XML Catalogs to take a role in abstracting the file paths.
I think that's an independent issue. If you are in an environment that has a policy of using XML catalogs for URI remapping in XML-related contexts, then it would make sense to use them for remapping both URIs occuring in include/externalRef in schemas and URIs occurring in locating files. However I don't see any need to explicitly couple locating files to catalogs. My personal opinion is that, although XML catalogs are an appropriate solution to the problem of publicId-to-URI mapping, using XML catalogs to perform URI-to-URI mapping is an XML-specific solution to a non-XML-specific problem.
James
> Thus, for me the only reasonable choice is still to use the DOCTYPE declaration for all associations
If you want to use DOCTYPEs, the nXML method can accomodate you (by doctypePublicId rules). However, I find the problems of using DOCTYPEs worse by far than the problem of associations disappearing on a rename. And even with DOCTYPEs, you can still get problem of the association changing; you still have to associate your DOCTYPEs with schemas. If you force me to put something in the instance, I would much prefer a processing instruction.
There's no single right way to do the association. Different users will legitimately prefer different approaches. A solution needs to be flexible enough to accomodate them.
James
> My opinion is that this association should be obligatory once present and could not be overriden.
It's a basic tenet of RELAX NG that the schema is not inherent in the document and that validation is a process that has two independently-specifiable inputs. Section 8 of the spec says: "A conforming RELAX NG validator must be a
-
Re:XML is broken
pick one of the better alternative formats and convert to XML after editing
Well said. A good example is RELAX NG: write the source in the compact syntax and convert it to XML when/if you need it.
A simple compact syntax example:
start = element title { text }*
The equivalent XML (and the RELAX NG XML format is considered much better than, e.g., W3C XML Schema):
<grammar xmlns="http://relaxng.org/ns/structure/1.0">
_ <start>
_ _ <zeroOrMore>
_ _ _ <element name="title">
_ _ _ _ <text/>
_ _ _ </element>
_ _ </zeroOrMore>
_ </start>
</grammar>(Please ignore the underscores.)
Remember people: XML may be good for data interchange between different applications because every language ever created by the gods has an XML parser, but don't use it for anything else. To quote Phillip J. Eby: Some people, when confronted with a problem, think "I know, I'll use XML." Now they have two problems.
-
Re:I have to agree.Yes. I've done it using Relax NG and it was easy, simple and readable.
It also works really, really well with the nXML mode for emacs.
Finally, XML schemas in a way that are not verbose, ugly and unreadable. And if you do need one of the older schema languages there are translators from RelaxNG available.
-
XML mode and a little history
As for history, I started using EMACS in 1979 at about version 134, when it was written in TECO on DEC PDP-10s. When RMS stopped maintaining it to move on to the GNU project he had just started, I took over its maintance for a year or so. One of the things I added in 1981 was M-$ (which I called "correct word spelling") and m-x check buffer spelling (which is a much better name than the present m-x spell-buffer).
If you use XML you should try James Clark's NXML Mode which uses Relax NG schemas. It gives you on-the-fly validation and completion when editing XML. Relax NG is very easy to use, especially in its Compact RNC form, which looks a lot like BNF but is isomorphic to the XML representation of the RNG Schema.
At work, I find it easy to develop Emacs-lisp scripts or even simply keyboard macros to do large refactoring jobs in Emacs. I often find that I'm the only person who can accomplish tasks that cut across programming language boundaries.
I used Emacs for several years to take real-time minutes for various W3C working groups, and made great use of the dabbrev-expand function (which I bound to meta-space). I like to think of it as an approximation to a Hidden Markov Model. In fact, at a Face-to-Face meeting in Cambridge, MA, I was able to type an entire sentence that the person sitting next to me spoke by typing only Meta-Space (and judiciously using only one or two letters to redirect the completions), a feat that lead many to believe that some form of magic was involved...Thereafter when others asked how our group had such complete minutes, people always responded, "Oh, he uses Emacs." -
XML mode and a little history
As for history, I started using EMACS in 1979 at about version 134, when it was written in TECO on DEC PDP-10s. When RMS stopped maintaining it to move on to the GNU project he had just started, I took over its maintance for a year or so. One of the things I added in 1981 was M-$ (which I called "correct word spelling") and m-x check buffer spelling (which is a much better name than the present m-x spell-buffer).
If you use XML you should try James Clark's NXML Mode which uses Relax NG schemas. It gives you on-the-fly validation and completion when editing XML. Relax NG is very easy to use, especially in its Compact RNC form, which looks a lot like BNF but is isomorphic to the XML representation of the RNG Schema.
At work, I find it easy to develop Emacs-lisp scripts or even simply keyboard macros to do large refactoring jobs in Emacs. I often find that I'm the only person who can accomplish tasks that cut across programming language boundaries.
I used Emacs for several years to take real-time minutes for various W3C working groups, and made great use of the dabbrev-expand function (which I bound to meta-space). I like to think of it as an approximation to a Hidden Markov Model. In fact, at a Face-to-Face meeting in Cambridge, MA, I was able to type an entire sentence that the person sitting next to me spoke by typing only Meta-Space (and judiciously using only one or two letters to redirect the completions), a feat that lead many to believe that some form of magic was involved...Thereafter when others asked how our group had such complete minutes, people always responded, "Oh, he uses Emacs." -
Why O Why
- Apart from the verbiage, could they not give a formal schema in XML Schema and/or RelaxNG or at the very least provide XML Schema Datatypes for things like "date" or "number"?
- Could they not give a formal description of mapping from the general schema to a "simple" subset in which there are no defaults?
- Do they have to use ad hoc paraphrases like "the name of the property (without any namespace)" instead of the standard XML Namespaces "local part" (or at least be precise in that they want the name of the element, without the prefix , as it could never have the namespace in it because namespaces are URIs)?
- What does a <cf:group> with a label attribute but no element attribute group by and/or what does it allow one to filter by?
- Do people at MS not provide references?
- &c.
"This specification is designed to be as simple as possible."
Yup. Great. Sigh.