Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 限制XSD,使其不允许根元素上的属性_Xml_Xsd_Schema_Xsd Validation - Fatal编程技术网

Xml 限制XSD,使其不允许根元素上的属性

Xml 限制XSD,使其不允许根元素上的属性,xml,xsd,schema,xsd-validation,Xml,Xsd,Schema,Xsd Validation,我需要使用以下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="un

我需要使用以下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中的根元素具有“name”属性

<root name="rootElement">
<names name="abc"></names>
<root name="xyz"></root>
</root>

EG: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是否可行,如果可行,我们如何做到这一点?

定义不带名称属性的根类型:

<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”
。在嵌套根元素的本地元素声明中(如上所示),使用类型
rootTypeWithName


我还没有对此进行测试,因此可能会出现语法错误,但原则应该是合理的。

在继续问新问题之前,请返回您的旧问题和那些有帮助的答案。如果没有对你有帮助的答案,请阅读并回答。谢谢