xml - 限制 XSD,使其不允许根元素上的属性
问题描述
我需要使用以下 XSD 验证 XML
<xs:element name="root" type="rootType"/>
<xs:element name="names" type="nameType"/>
<xs:complexType name="rootType" mixed="true">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="names" minOccurs="0" maxOccurs="unbounded" type="nameType"/>
<xs:element name="root" minOccurs="0" maxOccurs="unbounded" type="rootType"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="name" type="xs:string"/>
</xs:complexType>
<xs:complexType name="nameType" mixed="true">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="names" minOccurs="0" maxOccurs="unbounded" type="nameType"/>
<xs:element name="root" minOccurs="0" maxOccurs="unbounded" type="rootType"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="name" type="xs:string"/>
</xs:complexType>
这样,XSD 不应允许 XML 中的根元素具有“名称”属性。
例如:XSD 应该认为以下 XML 是有效的
<root>
<names name="abc"></names>
<root name="xyz"></root>
</root>
并且以下内容无效,因为 xml 的根元素具有 name 属性。
<root name="rootElement">
<names name="abc"></names>
<root name="xyz"></root>
</root>
但是,如果同一个元素作为子元素出现,那么它可以具有 name 属性。请让我们知道这是否可以使用 XSD,如果可以,我们该怎么做?
解决方案
定义不带名称属性的 rootType:
<xs:complexType name="rootTypeWithoutName" mixed="true">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="names" minOccurs="0" maxOccurs="unbounded" type="nameType"/>
<xs:element name="root" minOccurs="0" maxOccurs="unbounded" type="rootTypeWithName"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
然后定义一个允许名称的扩展类型:
<xs:complexType name="rootTypeWithName" mixed="true">
<xs:extension base="rootTypeWithoutName">
<xs:attribute name="name" type="xs:string"/>
</xs:extension>
</xs:complexType>
在根的全局元素声明中,使用type="rootTypeWithoutName"
. 在嵌套根元素的局部元素声明中(如上所示),使用 type rootTypeWithName
。
这个我没有测试过,所以可能有语法错误,但原理应该是合理的。
推荐阅读
- python - 无法将命令代理到远程服务器:套接字挂起
- javascript - 无法正确查看事件,在反应大日历中创建于上午 12:00
- spring - 在 Spring 上部署 WAR 文件 - Tomcat 9
- crafter-cms - CRAFTER:是否可以更改特定组件的特定属性的值?
- python - 集群数量小幅增加后 Power BI Python kMeans scikit-learn 执行超时
- spring-boot - Springboot调度器运行2次结束
- grpc - 在 Gatling 加载脚本中等待 gRPC 消息流中的特定消息
- powershell - 在创建防火墙规则的 DevOps 管道中遇到问题
- android-studio - ANDROID:在我的应用中实现谷歌地图方向
- r - 我的 CRS 指定的地图单位是什么?