java如何在模式中正确声明扩展?
在下面的模式文件中,我有一个元素Stat
,它扩展了Entity
。据我所知,I follow w3's example,但当我通过java的DocumentBuilderFactory
和SchemaFactory
解析模式(以及使用该模式的xml)时,我会遇到以下异常:
org.xml.sax.SAXParseException; systemId: file:/schema/characterschema.xsd;
src-resolve.4.2: Error resolving component 'cg:Entity'. It was detected that
'cg:Entity' is in namespace 'http://www.schemas.theliraeffect.com/chargen/entity',
but components from this namespace are not referenceable from schema document
'file:/home/andrew/QuasiWorkspace/CharacterGenerator/./schema/characterschema.xsd'.
If this is the incorrect namespace, perhaps the prefix of 'cg:Entity' needs
to be changed. If this is the correct namespace, then an appropriate 'import'
tag should be added to '/schema/characterschema.xsd'.
因此,我似乎无法在模式中看到我的名称空间。我是否需要将模式导入自身,还是完全误读了此异常?可能是我的模式声明不正确吗
这是我的模式供参考:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cg="http://www.schemas.theliraeffect.com/chargen/entity"
elementFormDefault="qualified">
<xs:element name="Entity">
<xs:complexType>
<xs:attribute name="id" type="xs:string" use="required"/>
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="description" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="Stat">
<xs:complexType>
<xs:complexContent>
<xs:extension base="cg:Entity">
<xs:sequence>
<xs:element name="table" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>
# 1 楼答案
你的模式有两个问题。首先,您没有声明
targetNamespace
,因此您定义的Entity
和Stat
元素不在名称空间中。其次,一个类型不能扩展一个元素,只能扩展另一个类型这里我定义了两个类型,其中一个扩展了另一个,以及相应类型的两个顶级元素。所有顶级类型和元素都定义在模式的
targetNamespace
中,并且StatType
中嵌套的table
元素也在这个名称空间中,因为elementFormDefault="qualified"
没有这个Entity
和Stat
元素将在http://www.schemas.theliraeffect.com/chargen/entity
名称空间中,但是table
元素将不在任何名称空间中