xml - 属性的 ref 不适用于 Netbeans 下的复杂类型

标签 xml netbeans xsd

我正在学习 XML 和 XSD,但遇到了问题。我想使用以下 XSD 文档验证以下 XML 文档。因为属性“id”用于两个不同的元素。我想将他的定义与 store 和 client 的定义分开,然后在其上使用 ref 。不幸的是,Netbeans 似乎忽略了属性上的引用,或者我以错误的方式执行此操作,因为当我检查文件 store.xml 时,出现以下错误:

XML validation started.

Checking file:/Users/toto/NetBeansProjects/Cookbook/src/java/store.xml...
Referenced entity at "file:/Users/strokyl/NetBeansProjects/Cookbook/src/java/store.xsd".
cvc-complex-type.3.2.2: Attribute 'id' is not allowed to appear in element 'client'. [14] 
cvc-complex-type.3.2.2: Attribute 'id' is not allowed to appear in element 'client'. [19] 
cvc-complex-type.3.2.2: Attribute 'id' is not allowed to appear in element 'client'. [24] 
cvc-complex-type.3.2.2: Attribute 'id' is not allowed to appear in element 'product'. [32] 
cvc-complex-type.3.2.2: Attribute 'id' is not allowed to appear in element 'product'. [39] 
cvc-complex-type.3.2.2: Attribute 'id' is not allowed to appear in element 'product'. [46]

当我替换<xs:attribute ref="id"/>时通过客户端和store上id属性的定义,xml正确有效!

提前感谢您的宝贵帮助,并对我糟糕的英语(我是法国人)表示歉意。

XML 文件 (store.xml)

<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : store.xml.xml
    Created on : 12 novembre 2013, 22:09
    Author     : strokyl
    Description:
        Purpose of the document follows.
-->
<store xmlns="http://etud.insa-toulouse.fr/~duzan/store"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation='http://etud.insa-toulouse.fr/~duzan/store store.xsd'>
    <clients>
        <client id="1">
            <first_name>Luc</first_name>
            <last_name>Duzan</last_name>
            <age>22</age>
        </client>
        <client id="2">
            <first_name>Adrien</first_name>
            <last_name>Gareau</last_name>
            <age>22</age>
        </client>
        <client id="3">
            <first_name>Gilles</first_name>
            <last_name>Roudière</last_name>
            <age>22</age>
        </client>
    </clients>

    <products>
        <product id="1">
            <name>Poster de Maxime Médard</name>
  <!-- You don’t have to use same convention that you use for relational database -->
            <categorie>Poster</categorie>
            <price>10</price>
            <number_in_stock>100</number_in_stock>
        </product>
        <product id="2">
            <name>Poster de Yannick Jauzion</name>
            <categorie>Poster</categorie>
            <price>10</price>
            <number_in_stock>200</number_in_stock>
        </product>

        <product id="3">
            <name>Drapeau du stade toulousain</name>
            <categorie>drapeau</categorie>
            <price>5</price>
            <number_in_stock>500</number_in_stock>
        </product>
    </products>
</store>

xml 架构文件 (store.xsd)

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://etud.insa-toulouse.fr/~duzan/store"
    xmlns="http://etud.insa-toulouse.fr/~duzan/store"
    elementFormDefault="qualified">

    <xs:element name="store">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="clients"/>
                <xs:element ref="products"/>
                <xs:any/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

<!-- Definition très exposé de clients -->

    <xs:element name="first_name" type="xs:string"/>
    <xs:element name="last_name" type="xs:string"/>

    <xs:element name="age">
        <xs:simpleType>
            <xs:restriction base="xs:integer">
                <xs:minInclusive value="0"/>
                <xs:maxInclusive value="120"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>

    <xs:attribute name="id" type="xs:integer"/>

    <xs:complexType name="client_type">
        <xs:sequence>
            <xs:element ref="first_name"/>
            <xs:element ref="last_name"/>
            <xs:element ref="age"/>
        </xs:sequence>
        <xs:attribute ref="id"/>
    </xs:complexType>

    <xs:element name="client" type="client_type"/>

    <xs:complexType name="clients_type">
        <xs:sequence>
            <xs:element ref="client" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="clients" type="clients_type"/>

<!-- Definition très condensé de product à part qu'on réutilise l'attribut id définit plus tôt -->
    <xs:element name="products">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="product" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string"/>
                            <xs:element name="categorie" type="xs:string"/>
                            <xs:element name="price">
                                <xs:simpleType>
                                    <xs:restriction base="xs:integer">
                                        <xs:minInclusive value="0"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>

                            <xs:element name="number_in_stock">
                                <xs:simpleType>
                                    <xs:restriction base="xs:integer">
                                        <xs:minInclusive value="0"/>
                                        <xs:maxInclusive value="1000"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>

                        </xs:sequence>
                        <xs:attribute ref="id"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

最佳答案

问题在于顶级属性声明采用架构的 targetNamespace 。因此,验证器正在寻找位于 http://etud.insa-toulouse.fr/~duzan/store 命名空间中名为 id 的属性,而不是您的文档包含一个名为 id 的属性,该属性位于 no 命名空间中(因为默认 xmlns 声明不适用于属性)。

要使现有架构有效,您需要将前缀绑定(bind)到命名空间并使用该前缀作为属性

<store xmlns="http://etud.insa-toulouse.fr/~duzan/store"
      xmlns:store="http://etud.insa-toulouse.fr/~duzan/store">
  <!-- ... -->
  <client store:id="1">

相比之下,复杂类型内的本地属性声明不在命名空间中(除非您在架构上指定attributeFormDefault或在具体声明)。因此,另一种选择可能是使用该属性声明一个基类型,然后让其他类型扩展它。

<xs:complexType name="identifiedType">
  <xs:attribute name="id" type="xs:integer"/>
</xs:complexType> 

<xs:complexType name="client_type">
  <xs:complexContent>
    <xs:extension base="identifiedType">
      <xs:sequence>
        <xs:element ref="first_name"/>
        <xs:element ref="last_name"/>
        <xs:element ref="age"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

id 属性现在是一个本地声明,而不是全局声明。

关于xml - 属性的 ref 不适用于 Netbeans 下的复杂类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19966077/

相关文章:

xml - 如何声明特殊的 XML 架构 (XSD) 案例

xml - 如何在复杂类型的 xsd 文件中使用唯一标记?

xml - 谁能推荐一个免费的 xslt 工具?

java - 基于spring xml的bean定义, `bean`元素的类属性

java - 使用 netbeans 7.1 自动开发 osgi 包

java - 导入 java.net.URLEncoder 时 NetBeans 7.4 找不到符号错误

c# - 在 XSD 中表示一对重复的 XML 元素

java - 在 ELEMENT_NODE 的情况下,node.getNodeType 无法按预期工作

java - 通过 POST 将 XML 文件发送到 Java 中的 RESTful 服务

java - 我们如何为我们的程序创建一个 jar 文件?