views:

194

answers:

1

I use xerces to valid a xml instance against schema:

parser.setFeature("http://xml.org/sax/features/namespaces", true);
parser.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
      schemaLocation);
parser.setFeature("http://apache.org/xml/features/validation/schema", true);
parser.parse(new InputSource(xml));

Here is my xml instance:

<?xml version="1.0"?>
<eml:eml packageId="tao.12926.1" system="knb" xmlns:eml="eml://ecoinformatics.org/eml-2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="eml://ecoinformatics.org/eml-2.1.0 eml.xsd">
  <dataset>
  .......
  </dataset>
</eml:eml>

This xml is considered valid.

However, if i added prefix "eml" to element "dataset":

<?xml version="1.0"?>
<eml:eml packageId="tao.12926.1" system="knb" xmlns:eml="eml://ecoinformatics.org/eml-2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="eml://ecoinformatics.org/eml-2.1.0 eml.xsd">
  <eml:dataset>
  .......
  </eml:dataset>
</eml:eml>

It give me an error: cvc-complex-type.2.4.a: Invalid content starting with element 'eml:dataset'. The content must match '((("":access){0-1},(((("":dataset)|("":citation))|("":software))|("":protocol))),("":additionalMetadata){0-UNBOUNDED})'.

I couldn't understand this. "dataset" has the default the namespace during our schema definition. "dataset" just a abridged version of "eml:dataset". Why xerces doesn't like ?

Would you please give me some clue?

Thanks!

+1  A: 

I think any parser, not only xerces, would report an error regarding eml:dataset.
The reason is that the schema for eml:eml doesn't use the attribute elementFormDefault, which then defaults to "unqualified", hence requiring that "locally declared elements" names (such as dataset) do not receive a prefix (or an implied namespace by way of default namespace). Only global elements (such as eml, here), can have (in fact, require) a explicit namespace prefix.

To allow (or maybe to even require) that locally declared elements be prefixed, one would have to alter the schema, either by adding the elementFormDefault="qualified" attribute-value pair to the declaration of the underlying global element, or by adding a form="qualified" attribute to individual locally declared elements of the schema (dataset, for example).

All of this, and then some!, is explained in section 3.1, 3.2 of the W3C Schema Primer document.

mjv
Thank you mjv.I tried the way which you suggested. but still get the error:cvc-complex-type.2.4.a: Invalid content starting with element 'emlds:dataset'
Jing
@Jing I was totally off with my assessment that dataset should have been form another namespace. I read the XSD too fast... The target NS in the XSD is effectively "eml://ecoinformatics.org/eml-2.1.0", the other namespaces within the ecoinformatics.org are for the types, but the elements themselves have the "eml" NS. Now, I think I know why we cannot add the eml prefix to elements like dataset, it is because of the way "locally declared elements" are configured in the XSD. See my edited response.
mjv