One part of the vision underlying RELAX NG is that validation should not be monolithic: it is not necessary or desirable to have one schema language that can handle every possible kind of validation you might want to do; it is better instead to have multiple specialized languages, each of which does one kind validation, really well. Consistent with this vision, RELAX NG provides only grammar-based validation. There's no implicit claim that other kinds of validation aren't useful and important.
One kind of validation that is clearly useful and important and that can't be done by grammars is checking of cross-references. One possibility is to use Schematron for this. The designers of RELAX NG anticipated that there would be a little schema language specialized to this, which would be created as part of the ISO DSDL effort (as part 6); this wouldn't be a million miles from the kind of thing that XSD provides with xs:key/xs:unique/xs:keyref. Unfortunately this hasn't happened yet.
Since DTDs provide ID/IDREF checking and we wanted people to be able to move easily from DTDs to RELAX NG, we felt we had to provide some transitional support for ID/IDREF checking while awaiting the ultimate "right" solution. We therefore provided a separate, optional spec called RELAX NG DTD Compatibility. Amongst other things, this defines a way in which RELAX NG processors can optionally provide DTD-compatible ID/IDREF checking based on the datatypes of attributes declared in the schema. Note that this can't handled by the XSD datatypes library for RELAX NG, because assignment of types in the schema to values in the instance is not part of the RELAX NG model of validation.
When defining RELAX NG DTD compatibility, we took a fairly hard line about being DTD-compatible. In particular, we made it a requirement that you should be able to generate a DTD subset from the RELAX NG schema that would perform the same type assignment that the process defined by the spec would perform. This creates some problems when you use DTD Compatibility in conjunction with wildcards (which of course aren't a DTD feature). For example:
start = element doc { p* } p = element p { id?, any* } id = attribute id { xsd:ID } any = element * { attribute * { text }*, (any|text)* }
will get a error about conflicting ID-types for p/@id. This is because the schema allows <p> to contain a <p> element with an id attribute that doesn't have type ID. Instead you would have to write:
start = element doc { p* } p = element p { id?, any* } id = attribute id { xsd:ID } any = element * - p { attribute * { text }*, (any|text)* }
Several years after the DTD compatibility spec was finished, the W3C came out with the xml:id Recommendation. The spec mentions RELAX NG in a non-normative appendix and encourages authors "to declare attributes named xml:id
with the type xs:ID
". Now on the face of it, this seems pretty reasonable advice. Unfortunately, from the point of the RELAX NG DTD Compatibility spec it's precisely the wrong thing to do. For example, this
start = element doc { p* } p = element p { id?, any* } id = attribute xml:id { xsd:NCName } any = element * { attribute * { text}*, (any|text)* }
will work perfectly with RELAX NG with or without DTD compatibility. The XML processor does the xml:id checking, and RELAX NG can ignore ID/IDREFs. But if instead you follow the xml:id Recommendation's suggestion and do:
start = element doc { p* } p = element p { id?, any* } id = attribute xml:id { xsd:ID } any = element * { attribute * { text}*, (any|text)* }
a RELAX NG validator that implements RELAX NG DTD compatibility will give you an error about conflicting ID-types p/@xml:id. You might think you could do
start = element doc { p* } p = element p { id?, any* } id = attribute xml:id { xsd:ID } any = element * { attribute * - xml:id { text}*, id?, (any|text)* }
but that won't work either, because although you can now write a DTD subset that does equivalent type assignment for p, you can't do it for the other elements.
(The xml:id Recommendation also says in the RELAX NG section that "A document that uses xml:id
attributes that have a declared type other than xs:ID
will always generate xml:id errors.". I don't see why: the xml:id processor is quite likely to be part of the XML parser, which doesn't know anything about RELAX NG, nor does RELAX NG know anything about xml:id.)
Back when RELAX NG DTD compatibility spec came out, I implemented support for the ID/IDREF checking part of DTD Compatibility in Jing. I also decided to make Jing enforce this by default. There's a -i switch to turn it off. Before xml:id came along, this seemed to work OK: if a schema author specifies ID/IDREF in a RELAX NG schema then they usually want ID/IDREFs to be checked and RELAX NG DTD Compatibility was the only thing that could do this checking. With xml:id this no longer works well: if you
- use xml:id
- declare xml:id attributes as type xsd:ID in the RELAX NG schema
- use wildcards in your RELAX NG schema
- don't use any special options to Jing
you are very likely to get an error from Jing.
At first, my plan was simply to change Jing not to enforce DTD Compatibility by default. However, Alex Brown pointed out that this isn't completely satisfactory: people who are coming from DTDs and aren't using xml:id lose the sensible ID/IDREF checking that they might reasonably expect to happen by default. So now I'm thinking that a better solution might be to add two boolean options to Jing, both of which would be enabled by default.
The first option would be to make it a warning rather than an error if the schema does not use ID/IDREF in a DTD-compatible way. (If the schema is DTD-compatible, then duplicate IDs or IDREFs to non-existent IDs would still be errors.)
The second option would tell Jing to be "xml:id aware". This would have several effects.
- It would require attributes named xml:id to be declared with type xsd:ID (or with the ID type from the datatype library defined by the DTD compatibility spec). This isn't strictly necessarily, but it would seem to minimize confusion and be in keeping with the spirit of the xml:id Recommendation. It's slightly tricky to decide what this means with various unusual RELAX NG wildcards. It is obvious that attribute xml:id { text} is an error. But the following are not all obvious to me:
- attribute xml:id|id { text }
- attribute * { text }
- attribute xml:* { text }
- attribute *|xml:id { text }
- When checking whether you can generate an equivalent DTD subset, xml:id attributes would be ignored. In the terms defined by the RELAX NG DTD Compatibility spec, you would ignore xml:id attributes when determining whether the schema is compatible with the ID/IDREF feature.
- When checking uniquess of IDs, and when checking IDREFs, an attribute named xml:id would always be treated as an ID attribute.
It might also be a good idea to revise the RELAX NG DTD compatibility spec to be xml:id aware in this way.
4 comments:
For example, [declaring xml:id attributes as NCNames] will work perfectly with RELAX NG with or without DTD compatibility. The XML processor does the xml:id checking, and RELAX NG can ignore ID/IDREFs.
That won't work, because xml:id processors do type assignment, not validation. They are encouraged to check for uniqueness of ids, but not for ID/IDREF consistency. So unless the xml:id is redundantly declared in the DTD and the XML processor is doing validation, the above strategy does not do any validation of xml:id at all.
That said, I think your proposed changes to Jing are quite reasonable. However, you don't really need a switch to disable xml:id-aware processing. Anyone who used the name "xml:id" in a way inconsistent with the xml:id Recommendation deserves to lose, because xml:* names are reserved to the W3C.
I think that the sentence "A document that uses xml:id attributes that have a declared type other than xs:ID will always generate xml:id errors" in Appendix D.3 was copied in error from Appendix D.2, where it does make sense. I have filed a proposed erratum with the XML Core WG to remove this sentence.
An xml:id-aware version of JING which did ID/IDREF type checking would be very welcome.
A question regarding the 'trang' utility: when automatically generating .rsd or .rng files from an original .xml I find that the order of elements is not always consistent:
>trang input.xml output1.xsd
>trang input.xml output2.xsd
The order of elements in output1 and output2 may differ. Of course, this doesn't make a difference to their use as schemas, but it causes trouble when trying to view diff between versions.
Since I see threads in the java code, I'm naively assuming this could be due to a race condition? Can you suggest a way to change this behavior?
Thanks very much,
Caleb Mattoon
(sorry for the off-topic post, this was the only contact information I found).
Please report issues with trang here:
http://code.google.com/p/jing-trang/issues/list
Post a Comment