简体   繁体   English

XSD-任何(未知)元素的未绑定数目,但定义了序列中的最后一个元素

[英]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 是否可以使用一个或多个XSD来验证以下xml结构

<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. xml中的所有元素都具有相同的名称空间(“”) ,我们无法更改。 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. 通常,更改xml是不可行的选择,因为它是由我们无法控制的外部系统生成的。

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. 在这里,我还了解了使用第二个命名空间的知识。 创建一个“灵活的” XML模式,但是由于我们无法更改xml,因此这似乎不是解决方案,或者我显然不正确地理解它。

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. 通过使用Java处理xml / xsd,xsd 驻留在类路径中 ,因此从xsd内部导入xs:import可能是一个问题。

If the answer is "This can't be done with xsd within these constraints" I'm fine with it. 如果答案是“在这些限制内使用xsd无法完成”,那么我很好。

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. 如果至少知道您希望在容器类型中使用的类型的名称,则可以将其命名为xs:anyType类型。 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. 更新:我不正确,您可以将容器设置为<xs:any/>但这将阻止您指定容器中必须有一个<data />元素。

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. xml的验证按预期并如上所述进行。 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. 但是直到知道没有时间找到更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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