xml - XSD 属性前缀命名空间

标签 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'.

最佳答案

默认情况下,complexType 内的元素和属性的“本地”声明不会采用架构的目标命名空间。您可以使用 xs:schema 上的 elementFormDefaultattributeFormDefault 属性更改此默认值,但如果您只想影响一个属性而不是所有属性然后您可以在特定属性声明上使用 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/

相关文章:

java - 使用 xjc 生成类文件时不支持绑定(bind)命名空间

c# - 如何使用 XmlSerializer 在大型文档中插入节点

c# - WebBrowser aspx 的命名空间

java - 如何在布局中将 TextView 与背景对齐 - Android

sql - 分解 SQL 表中每一行的 XML

xml - Solr - 查询全域最佳实践

xml - XJC - [错误]编译器无法支持此属性自定义

python-2.7 - Python 中的命名空间和作用域

Laravel:如何使用 Artisan Facade 在 Controller 和模型中调用终端命令

php - Luracast ReSTLer 3 RC6 : How to rename XML objects names