【问题标题】:How to properly declare an extension within a schema?如何在模式中正确声明扩展?
【发布时间】:2012-09-01 01:28:01
【问题描述】:

在下面的架构文件中,我有一个元素 Stat,它扩展了 Entity。据我所知,I follow w3's example,但是当我通过 java 的 DocumentBuilderFactorySchemaFactory 解析架构(以及使用架构的 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>

【问题讨论】:

    标签: java xml inheritance schema


    【解决方案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 中,并且StatType 内的嵌套table 元素也在这个命名空间中,因为elementFormDefault="qualified" - 没有这个Entity 和@ 987654330@ 元素将位于 http://www.schemas.theliraeffect.com/chargen/entity 命名空间中,但 table 元素将不在命名空间中。

    【讨论】:

      猜你喜欢
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 2017-02-12
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      相关资源
      最近更新 更多