如何在架构中正确声明扩展?

时间:2012-08-31 16:54:17

标签: java xml inheritance schema

在我下面的架构文件中,我有一个扩展Stat的元素Entity。据我所知I follow w3's example,但是当我通过java DocumentBuilderFactorySchemaFactory解析模式(以及使用模式的xml)时,我得到了这个例外:< / p>

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 个答案:

答案 0 :(得分:1)

您的架构存在两个问题。首先,您不要声明targetNamespace,因此您定义的EntityStat元素不在命名空间中。其次,类型不能扩展元素,只能扩展另一种类型。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:cg="http://www.schemas.theliraeffect.com/chargen/entity"
    targetNamespace="http://www.schemas.theliraeffect.com/chargen/entity"
    elementFormDefault="qualified">

    <xs:complexType name="EntityType">
        <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 name="Entity" type="cg:EntityType" />


    <xs:complexType name="StatType">
        <xs:complexContent>
            <xs:extension base="cg:EntityType">
                <xs:sequence>
                    <xs:element name="table" type="xs:string" minOccurs="0"
                           maxOccurs="unbounded"/>                        
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

    <xs:element name="Stat" type="cg:StatType" />
</xs:schema>

这里我定义了两个类型,其中一个扩展了另一个,以及相应类型的两个顶级元素。所有顶级类型和元素都定义在架构的targetNamespace中,table内的嵌套StatType元素也位于此命名空间中,因为elementFormDefault="qualified" - 如果没有这个,EntityStat元素将位于http://www.schemas.theliraeffect.com/chargen/entity命名空间中,但table元素将不在命名空间中。

相关问题