XSD属性前缀命名空间

时间:2015-04-07 08:10:03

标签: xml xsd namespaces attributes xml-validation

我正在尝试针对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'.

1 个答案:

答案 0 :(得分:3)

默认情况下,complexType内的元素和属性的“本地”声明不会占用架构的目标命名空间。您可以使用elementFormDefault上的attributeFormDefaultxs:schema属性更改此默认设置,但如果您只想影响一个属性而不是所有属性,那么您可以使用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>