我有一个关于只验证XML文档的子树的问题。考虑以下输入格式:
<root>
<configurationA>...</configurationA>
<configurationB>...</configurationB>
<configurationC>...</configurationC>
<configurationD>...</configurationD>
</root>其想法是在一个大型XML文件中配置多个配置。
每个组件只能解析和验证自己的配置,不需要知道任何信息(甚至不需要知道标记的名称)。
例如,对于组件B,最好有一个类似于以下内容的xsd文件:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbounded" processContents="skip"/>
<xs:element name="configurationB" type="ConfigurationType" minOccurs="1" maxOccurs="1"/>
<xs:any minOccurs="0" maxOccurs="unbounded" processContents="skip"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>不幸的是,由于要素的模糊性,这是行不通的。
我还试图为xs:sequence定义发生范围,或者使用xs:choice --在任何情况下,我都会得到一些错误(来自在线验证器)。
Cos-nonambig: ConfigurationB和WC##any违反了“唯一粒子属性”。在对此架构进行验证期间,将为这两个粒子创建歧义。
我听说XMLSchema1.1有一些解决方案,但是我正在从事的项目是在C++中使用C++,它只支持Schema1.0。更改解析器当前不是一个选项。
是否有一种解决方案可以用来实现这一目标?
进一步描述组件B的XSD应该能够匹配哪些内容的其他示例:
<root>
<configurationB>...</configurationB> <!-- config for B available exactly once -->
</root><root>
<configurationA>...</configurationA>
<configurationB>...</configurationB> <!-- config for B available exactly once -->
</root><root>
<configurationC>...</configurationC>
<configurationB>...</configurationB> <!-- config for B available exactly once -->
</root><root>
<configurationB>...</configurationB> <!-- config for B available exactly once -->
<configurationA>...</configurationA>
</root>以下例子应该失败:
<root>
<!-- missing config for B -->
</root><root>
<configurationA>...</configurationA>
<!-- missing config for B -->
<configurationC>...</configurationC>
</root><root>
<configurationB>...</configurationB>
<configurationB>...</configurationB> <!-- duplicate config for B -->
</root>发布于 2020-08-26 13:08:47
你可以试试xmlSchemaValidateOneElement。如果这不起作用,则可以使用树API将元素移动到自己的文档中。
https://stackoverflow.com/questions/63363104
复制相似问题