我正在尝试根据 XSD 验证 XML。我有一个带有前缀的属性,但我不知道如何使用 XSD 检查它。经过相当长的斗争,我弄清楚了如何检查元素的前缀,但现在我无法验证属性的前缀。我正在尝试类似于元素前缀验证的方法:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:p="http://www.xx.com/2014/p-Document" targetNamespace="http://www.xx.com/2014/p-Document" >
<xs:element name="EL" type="p:EL">
</xs:element>
<xs:complexType name="EL">
<xs:sequence>
<xs:element name="Group">
<xs:complexType>
<xs:attribute name="Table" type="p:Table" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="Table">
<xs:restriction base="xs:string">
</xs:restriction>
</xs:simpleType>
</xs:schema>
验证此类 xml 后:
<p:EL xmlns:p="http://www.xx.com/2014/p-Document">
<Group p:Table=""/>
</p:EL>
我收到错误:
Cvc-complex-type.3.2.2: Attribute 'p:Table' Is Not Allowed To Appear In Element 'Group'.. Line '2', Column '20'.
Cvc-complex-type.4: Attribute 'Table' Must Appear On Element 'Group'.. Line '2', Column '20'.
最佳答案
默认情况下,complexType
内的元素和属性的“本地”声明不会采用架构的目标命名空间。您可以使用 xs:schema
上的 elementFormDefault
和 attributeFormDefault
属性更改此默认值,但如果您只想影响一个属性而不是所有属性然后您可以在特定属性声明上使用 form
:
<xs:attribute name="Table" form="qualified" type="p:Table" use="required"/>
或者,在顶层声明属性(因为全局顶层元素和属性始终是限定的)并根据需要ref
它。
<xs:attribute name="Table" type="p:Table"/>
<xs:complexType name="EL">
<xs:sequence>
<xs:element name="Group">
<xs:complexType>
<xs:attribute ref="p:Table" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
关于xml - XSD 属性前缀命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29486830/