gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:34:35 33 4
gpt4 key购买 nike

我正在学习 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"/>通过client和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。的架构。因此验证器正在寻找一个名为 id 的属性。那是在http://etud.insa-toulouse.fr/~duzan/store命名空间,但您的文档包含一个名为 id 的属性不在命名空间中(因为默认的 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 或在特定声明上指定 form)。因此,另一种选择可能是声明一个带有属性的基类型,然后让您的其他类型扩展它。
<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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com