简体   繁体   中英

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 read 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?

您尝试的模式在XSD 1.1中有效-使用最新版本的Saxon或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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM