开发者

XSD - unbound number of any (unkown) elements but last element in sequence is defined

Is it possible using one or more XSDs to validate the following xml structure

<container>
    <unkownA />
    <unkownB />
    <unkownC />
    ...
    <data />
</container>

by those rules

  1. there is an unlimited number of unkown elements
  2. there is at least one of those unkown elements
  3. the last element is data
  4. data occurs only once
  5. data is validated according to provided rules

All the elements in the xml have the same namespace (""), which we can't change. We are most likely not able to change the order of the elements as well, though I know this is probably the easiest solution. Changing the xml in general is not an viable option, since it is generated by a external system we don't control.

I tried something like this

<xs:sequence>
   <xs:any minOccurs="1" maxOccurs="unbounded" processContents="lax" />
   <xs:element ref="data" minOccurs="1" />
</xs:sequence>

which of course, being ambiguous, violates the "Unique Particle Attribution".

I also r开发者_如何学JAVAead about the use of a second namespace here Creating a 'flexible' XML schema but since we can't change the xml this does not seem to be a solution or I plainly don't understand it properly.

By the we are using Java to process the xml/xsd, the xsd resides in the classpath, so xs:import from within an xsd might be a problem.

If the answer is "This can't be done with xsd within these constraints" I'm fine with it.

So any ideas?


The schema that you tried is valid in XSD 1.1 - try it again, using the latest release of Saxon or Xerces.


If you at least knew the names of the types you were expecting to be in your container type then you could make them xs:anyType type. But you need to know the list of possible type names otherwise what is the point of a schema to define them?

UPDATE: I was incorrect, you can make the container <xs:any/> however this will prevent you from specifying that there must be a <data /> element in the container.


What finally worked, even if it does not make me happy:

javax.xml.validation.SchemaFactory schemaFactory = SchemaFactory
        .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

schemaFactory
    .setFeature(
        "http://apache.org/xml/features/validation/schema-full-checking",
        false);

This seems to disable the validation of the schema itself. The validation of the xml works as expected and described above. And yes, I know: Disabling security/sanity features that are actived by default might not be a good idea. But till know there wasn't any time to find a better way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜